Pygame(十)作业

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()

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,我了解到OpenGL大作业涉及到光照、粒子系统、模型等综合项目。在模型中使用了大量的纹理贴图来充实场景的细节,并通过矩阵转换实现模型的运动和视角转换。此外,还涉及到世界视角和视觉视角的矩阵转换、投影模型过程以及对纹理和光照属性的设置来呈现不同的效果。 如果你想了解关于OpenGL大作业中雪的内容,我可以给你提供一个简单的例子: ```python import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * def draw_snow(): glBegin(GL_POINTS) glColor3f(1.0, 1.0, 1.0) # 设置雪花颜色为白色 for i in range(1000): x = random.uniform(-1, 1) y = random.uniform(-1, 1) z = random.uniform(-1, 1) glVertex3f(x, y, z) glEnd() def main(): pygame.init() display = (800, 600) pygame.display.set_mode(display, DOUBLEBUF | OPENGL) gluPerspective(45, (display[0] / display[1]), 0.1, 50.0) glTranslatef(0.0, 0.0, -5) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() glRotatef(1,3, 1, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_snow() pygame.display.flip() pygame.time.wait(10) if __name__ == '__main__': main() ``` 这个例子使用了Pygame和OpenGL库来绘制一个简单的雪花场景。通过设置雪花的坐标和颜色,然后使用OpenGL的绘制函数来绘制雪花。在主循环中,通过旋转和清除缓冲区来实现雪花的动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱编程的章老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值