Python学习的二十二天 (飞机大战(矩形对象,动画的实现,时钟,事件监听))

矩形对象的创建

矩形对象 = pygame.Rect(x,y,width,heigh)

用途:

通过矩形对象来存储数据

需要修改数据时,直接对矩形对象进行操作

动画效果实现的原理

实际上是多张图片快速的翻页

翻页速度越快,动画越流畅

让英雄移动

实现方式

在循环中,修改飞机的x与y的数据

问题,出现了残影

在这里插入图片描述

解决办法

重新绘制图片

重新绘制飞机

游戏时钟

创建游戏时钟对象

时钟对象  = pygame.time.Clock()

控制帧率

时钟对象.tick(帧率)

显示当前的时间戳

time.time()

练习让飞机不越界

思路,进行边界判断

flag_x = True  # 控制x方向的开关
flag_y = True  # 控制y方向的开关

while True:
    # print(hero_rect.x, '英雄的x')

    # 绘制背景图,更新显示
    screen.blit(bg, (0,0))

    # 对象.属性 = 值
    if flag_x == True:
        hero_rect.x += 2
    else:
        hero_rect.x -= 2

    if flag_y == True:
        hero_rect.y += 2
    else:
        hero_rect.y -= 2

    if hero_rect.x >= 400-69:
        flag_x = False
    elif hero_rect.x <=0:
        flag_x = True

    if hero_rect.y >= 600-99:
        flag_y = False
    elif hero_rect.y <=0:
        flag_y = True

    # 绘制飞机
    screen.blit(hero, (hero_rect.x, hero_rect.y))
    # 更新显示
    pygame.display.update()

    clock.tick(60)

事件

获得事件列表

事件列表 = pygame.event.get()

事件类型

鼠标左击

[<Event(5-MouseButtonDown {'pos': (134, 489), 'button': 1, 'window': None})>, <Event(6-MouseButtonUp {'pos': (134, 489), 'button': 1, 'window': None})>, <Event(4-MouseMotion {'pos': (142, 481), 'rel': (8, -8), 'buttons': (0, 0, 0), 'window': None})>]

鼠标右击

[<Event(5-MouseButtonDown {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(6-MouseButtonUp {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(5-MouseButtonDown {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(6-MouseButtonUp {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(5-MouseButtonDown {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(6-MouseButtonUp {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(5-MouseButtonDown {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(6-MouseButtonUp {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(5-MouseButtonDown {'pos': (58, 430), 'button': 3, 'window': None})>, <Event(6-MouseButtonUp {'pos': (58, 430), 'button': 3, 'window': None})>]

点击空格

[<Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(2-KeyDown {'unicode': ' ', 'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(2-KeyDown {'unicode': ' ', 'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(2-KeyDown {'unicode': ' ', 'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(2-KeyDown {'unicode': ' ', 'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(2-KeyDown {'unicode': ' ', 'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(3-KeyUp {'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>, <Event(2-KeyDown {'unicode': ' ', 'key': 32, 'mod': 0, 'scancode': 57, 'window': None})>]

关闭按钮

[<Event(12-Quit {})>, <Event(12-Quit {})>, <Event(12-Quit {})>]

update方法的作用

在这里插入图片描述

update最后再调用一次就好

游戏循环游戏时钟

在这里插入图片描述

游戏循环

进入到游戏循环,意味着游戏的正式开始

在这里插入图片描述

在这里插入图片描述

游戏时钟

在这里插入图片描述

在这里插入图片描述

英雄动画效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

事件监听

在这里插入图片描述

在这里插入图片描述

监听退出事件

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/abcdhulei/article/details/89188372