飞机大战基础

矩形对象的创建

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

用途:

通过矩形对象来存储数据

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

动画效果实现的原理

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

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

让英雄移动

实现方式

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

问题,出现了残影

1554859231931

解决办法

重新绘制图片

重新绘制飞机

游戏时钟

创建游戏时钟对象

时钟对象  = 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 {})>]

继承的复习

使用继承的场景

如果需要类会频繁的用到另一个类的属性或方法时,可以考虑继承

目的是减少代码重复

子类继承父类的情况

  1. 父类的方法,子类可以直接使用

  2. 如果子类的方法与父类方法完全不一样,那么我们可以重写子类中的方法

  3. 如果子类的方法会在父类方法中有新增和拓展,保留父类中的方法的功能,调用父类的方法

    super().父类方法名(参数)
    

猜你喜欢

转载自blog.csdn.net/lyq562551775/article/details/89192175