欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Pygame(十)作业

程序员文章站 2022-06-08 18:34:21
...

Pygame(十)作业

作业详解

第一题

题目

随心圆:以鼠标左键点击为圆心,画一个半径50 ,颜色随机的圆

分析

需求分析:

  1. 鼠标左键点事件
  2. 获取鼠标的位置
  3. 颜色随机
  4. 画圆

代码

  1. 鼠标左键点击事件
  2. 获取鼠标的位置
if event.type == pygame.MOUSEBUTTONDOWN:
    if event.button == 1:
        pos = event.pos
  1. 颜色随机
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
c = r, g, b

4.画圆

pygame.draw.circle(s, c, pos, 50,width = 1)

完整代码

方法一:

def homework1_1():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    pos = event.pos
                    r = randint(0, 255)
                    g = randint(0, 255)
                    b = randint(0, 255)
                    c = r, g, b
                    s.fill((0, 0, 0))
                    pygame.draw.circle(s, c, pos, 50, width=1)
                    pygame.display.update()
        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()

方法二:

def homework1_2():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()

        # 处理鼠标事件
        mouse = pygame.mouse.get_pressed()  # 获取鼠标点击状态元组 三个元素分别代表左键 ,中键 ,右键的点击状态.点了就是True否则为False
        if mouse[0]:  # 判断左键有没有点击
            pos = pygame.mouse.get_pos()  # 点击了左键就获取鼠标的位置作为圆的圆心
            r = randint(0, 255)
            g = randint(0, 255)
            b = randint(0, 255)
            c = r, g, b
            s.fill((0, 0, 0))
            pygame.draw.circle(s, c, pos, 50, width=1)
            pygame.display.update()

与键盘的按键响应一样,鼠标的响应也可以用mouse模块来专门处理.这样会比用event事件直接处理来的简洁明了.

第二题

题目

点不中的矩形心:在屏幕中初始化一个矩形,当鼠标点击这个矩形时,矩形移动到鼠标没有点到的地方.
备注:判断一下点有没有在矩形内,可以用矩形的collidepoint(x,y) 当点(x,y)在矩形内时返回True,否则返回False

需求分析

  1. 初始在屏幕中心画一个矩形形(大小没说,可以自定义)
  2. 鼠标获取鼠标点击事件
  3. 获取鼠标的位置
  4. 判断位置是否在矩形内
  5. 改变矩形的位置直到鼠标的位置不在矩形内为止

代码

  1. 初始在屏幕中心画一个矩形
pygame.draw.rect(s, c, rect, width = 1)
  1. 鼠标点击事件
  2. 获取鼠标的位置
mouse = pygame.mouse.get_pressed()
if mouse[0]:
    pos = pygame.mouse.get_pos()
  1. 判断位置是否在矩形内
  2. 改变矩形的位置直到鼠标的位置不在矩形内为止
while rect.collidepoint(pos[0], pos[1]):
    rect.left = randint(0, 700)
    rect.top = randint(0, 500)

完整代码

def homework2():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(350, 250, 100, 100)
    color = 0, 255, 0
    pygame.draw.rect(s, color, rect, width=1)
    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        mouse = pygame.mouse.get_pressed()  # 获取鼠标点击状态元组 三个元素分别代表左键 ,中键 ,右键的点击状态.点了就是True否则为False
        if mouse[0]:  # 判断左键有没有点击
            pos = pygame.mouse.get_pos()  # 点击了左键就获取鼠标的位置作为圆的圆心
            while rect.collidepoint(pos[0], pos[1]):
                rect.left = randint(0, 700)
                rect.top = randint(0, 500)
            s.fill((0, 0, 0))
            pygame.draw.rect(s, color, rect, width=1)
            pygame.display.update()
            
        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()

第四题

题目

复制与粘贴:屏幕上初始一个矩形, 当鼠标框选中矩形时,按CTRL + C 复制 这个矩形,然后在鼠标下一次点击时以鼠标点击处为矩形的中心粘贴这个矩形

需求分析

代码

  1. 屏幕上初始画一个矩形,大小颜色自定义
rect = pygame.Rect(350, 250, 100, 100)
color = 0, 255, 0
pygame.draw.rect(s, color, rect, width = 1)
  1. 鼠标框选:
            if event.type == pygame.MOUSEBUTTONDOWN and not have_rect:
                if event.button == 1:
                    mouse_flag = True
                    start_pos = event.pos
            if event.type == pygame.MOUSEMOTION and mouse_flag:
                end_pos = event.pos
                left = start_pos[0] if start_pos[0] < end_pos[0] else end_pos[0]
                top = start_pos[1] if start_pos[1] < end_pos[1] else end_pos[1]
                width = abs(start_pos[0] - end_pos[0])
                height = abs(start_pos[1] - end_pos[1])
                new_rect = pygame.Rect(left, top, width, height)
                s.fill((0, 0, 0))
                pygame.draw.rect(s, color, rect, width=1)
                pygame.draw.rect(s, color, new_rect, width=1)
                pygame.display.update()
  1. 判断框选的范围是不是把矩形整个包含进去了,如果包含了整个矩形,就复制一下这个矩形
            if event.type == pygame.MOUSEBUTTONUP and mouse_flag:
                if event.button == 1:
                    mouse_flag = False
                    if new_rect.contains(rect):
                        can_copy = True
                        s.fill((0, 0, 0))
                        pygame.draw.rect(s, (255, 0, 0), rect, width = 5)  # 框选成功加粗显示
                        pygame.display.update()
  1. 复制
 keys = pygame.key.get_pressed()
            if keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL] and keys[pygame.K_v] and can_copy:
                copy_rect = rect.move(0,0)
                print(copy_rect)
                can_copy = False
                have_rect = True

5.粘贴

            if event.type == pygame.MOUSEBUTTONDOWN and have_rect:
                if event.button == 1:
                    have_rect = False
                    pos = event.pos
                    copy_rect.center = pos
                    s.fill((0, 0, 0))
                    pygame.draw.rect(s, color, rect, width=1)
                    pygame.draw.rect(s, color, copy_rect, width=1)
                    pygame.display.update()

完整代码:

def homework4():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(350, 250, 100, 100)
    color = 0, 255, 0
    pygame.draw.rect(s, color, rect, width=1)
    pygame.display.update()
    mouse_flag = False # 鼠标第一次点击的标识
    have_rect = False  # 有没有复制的标记
    can_copy = False  # 能不能复制的标记
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN and not have_rect:
                if event.button == 1:
                    mouse_flag = True
                    start_pos = event.pos
            if event.type == pygame.MOUSEMOTION and mouse_flag:
                end_pos = event.pos
                left = start_pos[0] if start_pos[0] < end_pos[0] else end_pos[0]
                top = start_pos[1] if start_pos[1] < end_pos[1] else end_pos[1]
                width = abs(start_pos[0] - end_pos[0])
                height = abs(start_pos[1] - end_pos[1])
                new_rect = pygame.Rect(left, top, width, height)
                s.fill((0, 0, 0))
                pygame.draw.rect(s, color, rect, width=1)
                pygame.draw.rect(s, color, new_rect, width=1)
                pygame.display.update()
            if event.type == pygame.MOUSEBUTTONUP and mouse_flag:
                if event.button == 1:
                    mouse_flag = False
                    if new_rect.contains(rect):
                        can_copy = True
                        s.fill((0, 0, 0))
                        pygame.draw.rect(s, (255, 0, 0), rect, width = 5)  # 框选成功加粗显示
                        pygame.display.update()
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL] and keys[pygame.K_v] and can_copy:
                copy_rect = rect.move(0,0)
                print(copy_rect)
                can_copy = False
                have_rect = True
            if event.type == pygame.MOUSEBUTTONDOWN and have_rect:
                if event.button == 1:
                    have_rect = False
                    pos = event.pos
                    copy_rect.center = pos
                    s.fill((0, 0, 0))
                    pygame.draw.rect(s, color, rect, width=1)
                    pygame.draw.rect(s, color, copy_rect, width=1)
                    pygame.display.update()

        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()