本次增加的性能就是对我的项目的收尾工作了:减少游戏完结界面,历史记录、游戏得分、从新开始与完结游戏按钮。(当玩家三条命都用完后触发)
同样的,先上图:

本次步骤所需的资源其实就两个按钮的图片了,(文字的font我无奈放上来,能够间接下一个font)

或者间接下载整个我的项目的包,包含源码以及资源,都很具体:https://download.csdn.net/dow...

本次变动的中央还是在main.py外面,其实就是最初的一些小性能的欠缺,当你曾经写到这一步的时候,这些性能其实本人都能够增加了,办法都是一样的,所以我的项目做到这里就算小小的完结了。
其实教程外面还有增加中型、大型飞机来着,还有游戏难度递增,但其实跟原有写法没太大差异,我就没有持续写了,都是下面那个包还是都蕴含了。

最初:我本人有些写的逻辑以及办法跟教程不一样,也倡议大家本人看个大抵的逻辑就行,而后以本人的形式去实现,这样学到的货色会更多。
而后便是代码模块了,这回的代码我会把我写的全副放上来。

main.py:渲染各个组件,运行逻辑等等
bullet.py:次要是子弹的管制,包含子弹各种属性、以及子弹的重置
enemy.py:敌机类,蕴含敌机的属性、运行、重置等
myplane.py:次要是玩家飞机的管制,包含玩家飞机各种属性、飞机的上下左右挪动,以及飞机的新生
supply.py:次要是补给的管制,包含补给的挪动以及重置,其实写法都差不多
record.txt:保留玩家最高记录

main.py

import pygameimport sysimport tracebackfrom pygame.locals import *from random import *import myplaneimport enemyimport bulletimport supply# 初始化pygame.init()# 设置窗口大小bg_size = width, height = 400, 700  # 实际上是元组screen = pygame.display.set_mode(bg_size)  # 设置窗口pygame.display.set_caption("飞机大战")  # 窗口题目# 加载背景图片,对于一般图像的显示成果有没有convert都是一样的,然而 应用 convert 能够转换格局,进步 blit 的速度background = pygame.image.load("images/background.png").convert()# 设置黑、绿、红、百几种色彩对应值,前面会用到BLACK = (0, 0, 0)GREEN = (0, 255, 0)RED = (255, 0, 0)WHITE = (255, 255, 255)# 生成敌方小型飞机def add_small_enemy(small_enemies, enemiesGroup, num):    for i in range(num):        smallenemy = enemy.SmallEnemy(bg_size)        # 精灵组来实现多个图像,很适宜解决精灵列表,有增加,移除,绘制,更新等办法        # Group.sprites 精灵组        # Group.copy 复制        # Group.add 增加        # Group.remove 移除        # Group.has 判断精灵组成员        # Group.update 更新        # Group.draw 位块显示        # Group.clear - 绘制背景        # Group.empty 清空        # 将这一组敌机都增加上小型飞机属性,相当于对立解决,对立赋值        small_enemies.add(smallenemy)        enemiesGroup.add(smallenemy)def main():    # 创立时钟对象(能够管制游戏循环频率)    clock = pygame.time.Clock()    # 生成玩家飞机    me = myplane.MyPlane(bg_size)    # 寄存所有敌方飞机,这个飞机组蕴含了小型飞机、中型飞机、大型飞机的各种属性,只有用于解决碰撞    # 当程序中有大量的实体的时候,操作这些实体将会是一件相当麻烦的事    # 应用pygame.sprite.Group()函数能够创立一个精灵组,从而对立治理,这里创立了一个敌机组    enemiesGroup = pygame.sprite.Group()    # 生成中央小型飞机,敌方小型飞机也是一个组,进行对立解决    small_enemies = pygame.sprite.Group()    add_small_enemy(small_enemies, enemiesGroup, 15)    # 生成一般子弹,这里是四颗子弹循环    bullet1s = []    # 标记产生的哪颗子弹    bullet1s_index = 0    # 子弹数目    bullet1_num = 4    for i in range(bullet1_num):        # 把玩家飞机的地位发给子弹类        bullet1s.append(bullet.Bullet1(me.rect.midtop))    # 生成增强子弹,这里是八颗子弹循环,左右各四颗    bulletspro = []    # 标记产生的哪颗子弹    bulletspro_index = 0    # 子弹数目    bulletspro_num = 8    # 左右各压入四颗子弹,//2示意的整除,其实用/2也一样    for i in range(bulletspro_num // 2):        # 这里(me.rect.centerx - 33, me.rect.centery)是指元组地位,centerx代表x轴,centery代表y轴        bulletspro.append(bullet.Bullet2((me.rect.centerx - 33, me.rect.centery)))        bulletspro.append(bullet.Bullet2((me.rect.centerx + 33, me.rect.centery)))    # 初始化增强子弹补给,超级炸弹补给    bullet_supply = supply.Bullet_Supply(bg_size)    bomb_supply = supply.Bomb_Supply(bg_size)    # 设置无敌工夫事件,pygame.USEREVENT代表事件1,pygame.USEREVENT+1代表事件2,以此类推,这里相当于定义了一个事件    invincible_event = pygame.USEREVENT    # 设置补给工夫事件    bullet_time_supply = pygame.USEREVENT + 1    # 设置增强子弹定时器事件,即增强子弹buff继续事件    bulletpro_time = pygame.USEREVENT + 2    # 设置定时器,8秒钟发放一次补给    pygame.time.set_timer(bullet_time_supply, 8 * 1000)    # 标记是否应用超级子弹    is_double_bullet = False    # 玩家三条命    life_num = 3    life_image = pygame.image.load('images/life.png').convert_alpha()    life_rect = life_image.get_rect()    # 玩家带有超级炸弹数量    bomb_num = 3    # 绘制超级炸弹    bomb_image = pygame.image.load('images/bomb.png').convert_alpha()    # 超级炸弹图片地位    bomb_rect = bomb_image.get_rect()    # 超级炸弹数量字体    bomb_font = pygame.font.Font('font/font.ttf', 48)    # 游戏暂停,默认为非暂停状态    paused = False    # 暂停图片    pause_nor_image = pygame.image.load('images/pause_nor.png').convert_alpha()    pause_pressed_image = pygame.image.load('images/pause_pressed.png').convert_alpha()    # 持续图片    resume_nor_image = pygame.image.load('images/resume_nor.png').convert_alpha()    resume_pressed_image = pygame.image.load('images/resume_pressed.png').convert_alpha()    # 设置默认图片    paused_image = pause_nor_image    # 暂停按钮地位    paused_rect = pause_nor_image.get_rect()    paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10    # 管制玩家飞机图片切换,展现突突突的成果    switch_image = True    # 切换延时    delay = 100    # 游戏分数    score = 0    # 设定玩家分数字体款式,从一个字体文件创建一个 Font 对象    score_font = pygame.font.Font('font/font.ttf', 36)    # 游戏完结界面    gameover_font = pygame.font.Font('font/font.TTF', 48)    # 从新开始按钮图片    again_image = pygame.image.load('images/again.png').convert_alpha()    again_rect = again_image.get_rect()    # 游戏完结按钮图片    gameover_image = pygame.image.load('images/gameover.png').convert_alpha()    gameover_rect = gameover_image.get_rect()    # 飞机爆炸的图片下标,顺次为小型敌机,中型敌机,大型敌机,玩家飞机的爆炸的图片的下标,切换下标来扭转爆炸图片    e1_destory_index = 0    e2_destory_index = 0    e3_destory_index = 0    me_destory_index = 0    running = True    while running:        # 获取事件        for event in pygame.event.get():            # 完结事件触发完结操作            if event.type == QUIT:                pygame.quit()                sys.exit()            # 在触发碰撞的时候,写了pygame.time.set_timer(invincible_event, 3*1000)            # 意思就是3秒后将会执行invincible_event事件,这里捕捉了invincible_event事件,执行后,将勾销这个计时器,避免循环反复执行,期待下一次触发            elif event.type == invincible_event:                # 解除无敌状态                me.invincible = False                pygame.time.set_timer(invincible_event, 0)            # 触发补给事件            elif event.type == bullet_time_supply:                # 随机进行一次判断,如果是True,就发放强化子弹补给                if choice([True, False]):                    # 发放强化子弹补给                    bullet_supply.reset()                else:                    # 发放超级炸弹补给                    bomb_supply.reset()            # 增强子弹buff到时事件            elif event.type == bulletpro_time:                # 子弹切换为一般子弹                is_double_bullet = False                # 事件进行循环,期待下一次触发                pygame.time.set_timer(bulletpro_time, 0)            # 捕捉按键操作            elif event.type == KEYDOWN:                # 如果按下的是空格,触发大招,超级炸弹清空屏幕内的飞机                if event.key == K_SPACE:                    if bomb_num > 0:                        bomb_num -= 1                        # 判断所有在场的敌机,是否在游戏屏幕内                        for ei in enemiesGroup:                            if ei.rect.bottom > 0:                                ei.active = False            # 鼠标挪动事件            elif event.type == MOUSEMOTION:                # 鼠标移入到暂停的矩形框内,检测鼠标是否在矩形里,是则返回True,否则返回False ; pos – 就是鼠标地位                if paused_rect.collidepoint(event.pos):                    # 如果是暂停状态                    if paused:                        paused_image = resume_pressed_image                    else:                        paused_image = pause_pressed_image                else:                    if paused:                        paused_image = resume_nor_image                    else:                        paused_image = pause_nor_image            # 鼠标左键点击暂停按钮            elif event.type == MOUSEBUTTONDOWN:                # 如果是左键而且鼠标点击地位在暂停框内                if event.button == 1 and paused_rect.collidepoint(event.pos):                    # 切换暂停状态                    paused = not paused                # 如果点击暂停按钮后,游戏是暂停状态                if paused:                    # 绘制暂停按钮图片                    paused_image = resume_pressed_image                    # 暂停补给定时器                    pygame.time.set_timer(bullet_time_supply, 0)                # 如果点击暂停按钮后,游戏是持续状态                else:                    # 绘制持续按钮图片                    paused_image = pause_pressed_image                    # 持续补给定时器                    pygame.time.set_timer(bullet_time_supply, 8 * 1000)        # 检测用户键盘操作,别离为上下左右        key_pressed = pygame.key.get_pressed()        if key_pressed[K_w] or key_pressed[K_UP]:            me.moveUp()        if key_pressed[K_s] or key_pressed[K_DOWN]:            me.moveDown()        if key_pressed[K_a] or key_pressed[K_LEFT]:            me.moveLeft()        if key_pressed[K_d] or key_pressed[K_RIGHT]:            me.moveRight()        # 在屏幕下面绘制背景图像,并指定地位        screen.blit(background, (0, 0))        # 绘制子弹补给、炸弹补给、敌机、玩家飞机等等各种元素        # 未暂停且生命大于0        if paused == False and life_num > 0:            # 绘制子弹补给,如果子弹补给状态为真,即触发子弹补给操作            if bullet_supply.active:                # 子弹开始静止,并且渲染子弹子弹补给图片                bullet_supply.move()                screen.blit(bullet_supply.image, bullet_supply.rect)                # 着落过程中如果跟玩家飞机碰撞,阐明玩家飞机拾取到子弹补给                # pygame.sprite.collide_mask:两个精灵之间的像素遮罩检测,接管两个精灵作为参数,返回值是一个bool变量                if pygame.sprite.collide_mask(bullet_supply, me):                    # 扭转子弹补给状态,包含暗藏以及不能再拾取了                    bullet_supply.active = False                    # 飞机子弹变为增强子弹                    is_double_bullet = True                    # 设置增强子弹buff继续事件,这里继续4s                    pygame.time.set_timer(bulletpro_time, 4 * 1000)            # 绘制超级炸弹补给,如果超级炸弹状态为真,即触发超级炸弹补给操作            if bomb_supply.active:                # 超级炸弹补给开始静止,并且渲染补给图片                bomb_supply.move()                screen.blit(bomb_supply.image, bomb_supply.rect)                # 玩家飞机拾取到补给                if pygame.sprite.collide_mask(bomb_supply, me):                    # 扭转补给状态,包含暗藏以及不能再拾取了                    bomb_supply.active = False                    # 判断超级炸弹数量,不能大于3                    if bomb_num < 3:                        bomb_num += 1            # 绘制小型敌机,这里是因为下面定义了小型飞机组,飞机组add了小型飞机属性(速度、地位等),这时候地图上就生成了飞机            # 如果这些飞机属于小型敌机类,即一起解决            for ei in small_enemies:                # 敌机是活的,未被击毁                if ei.active == True:                    # 绘制小型敌机,并且敌机开始静止                    screen.blit(ei.image, ei.rect)                    ei.samll_enemy_move()                # 小型敌机被捣毁(被玩家击毁或者与玩家碰撞)                else:                    # 这里设置delay % 4是指爆炸画面为4帧(集体猜想),了解为爆炸停留时间,可自行设置                    if not (delay % 4):                        # 用于播放爆炸声音,每一架敌机只有一次                        if e1_destory_index == 0:                            print("播放敌机爆炸声音")                        # 绘制敌机撞击爆炸画面                        screen.blit(ei.destory_images[e1_destory_index], ei.rect)                        # 切换爆炸图片下标,从而切换爆炸图片                        e1_destory_index = (e1_destory_index + 1) % 4                        # 经验完一轮爆炸的敌机,能够将其销毁,也能够新生,都是不能不解决,不然会始终爆炸、爆炸                        # 这里抉择将其新生                        if e1_destory_index == 0:                            ei.reset()                            score += 100            # 做碰撞检测,pygame.sprite.spritecollide(sprite,sprite_group,bool):一个组中的所有精灵都会一一地对另外一个单个精灵进行冲突检测,发生冲突的精灵会作为一个列表返回。            # 第一个参数就是单个精灵,第二个参数是精灵组,第三个参数是一个bool值,最初这个参数起了很大的作用。当为True的时候,会删除组中所有抵触的精灵,False的时候不会删除抵触的精灵            # 第四个参数是:两个精灵之间的像素遮罩检测            enemy_collide = pygame.sprite.spritecollide(me, enemiesGroup, False, pygame.sprite.collide_mask)            # 碰撞解决,如果不是无敌状态下产生碰撞            if enemy_collide and not me.invincible:                # 玩家飞机触发捣毁状态                me.active = False                # enemy_collide是一个列表,存储所有跟玩家飞机产生碰撞的敌机,而后把碰撞的敌机状态置为捣毁状态                for ei in enemy_collide:                    ei.active = False            # 绘制玩家飞机,如果飞机为激活状态            if me.active:                # 在屏幕上绘制玩家飞机,switch_image为是否切换图片                if switch_image:                    screen.blit(me.image1, me.rect)                # 切换一下航行图片                else:                    screen.blit(me.image2, me.rect)            # 代表飞机受到碰撞,激活爆炸事件            else:                if not (delay % 4):                    # 用于播放爆炸声音,每一架敌机只有一次                    if me_destory_index == 0:                        print("玩家飞机爆炸声音")                    # 绘制玩家撞击爆炸画面                    screen.blit(me.destory_image[me_destory_index], me.rect)                    # 切换爆炸图片下标,从而切换爆炸图片                    me_destory_index = (me_destory_index + 1) % 4                    # 爆炸画面播放完之后飞机新生                    if me_destory_index == 0:                        # 生命减一条,如果见到0,会主动跳过上一级循环                        life_num -= 1                        # 重置状态                        me.reset()                        # 无敌工夫设置为3秒,3秒后,触发无敌工夫事件,pygame.time.set_timer:就是每隔一段时间(这里是3毫秒 * 1000 = 3s),去执行一些动作                        pygame.time.set_timer(invincible_event, 3 * 1000)            # 每10个单位工夫发射一颗子弹            if not(delay % 10):                # 如果是一般子弹                if is_double_bullet == False:                    bullets = bullet1s                    # 先定子弹0的地位                    bullets[bullet1s_index].reset(me.rect.midtop)                    # 切换到下一颗子弹                    bullet1s_index = (bullet1s_index + 1) % bullet1_num                # 如果是超级子弹                else:                    # 则子弹切换为增强子弹                    bullets = bulletspro                    # 左右增强子弹重置地位,逻辑其实跟一般子弹一样的,只是地位变了                    bullets[bulletspro_index].reset((me.rect.centerx - 33, me.rect.centery))                    bullets[bulletspro_index + 1].reset((me.rect.centerx + 33, me.rect.centery))                    # 切换到下一组子弹                    bulletspro_index = (bulletspro_index + 2) % bulletspro_num            # 绘制子弹            for bul in bullets:                if bul.active:                    # 子弹如果是激活状态的话,就能够挪动加绘制了                    bul.move()                    screen.blit(bul.image, bul.rect)                    # 子弹与敌机的碰撞,子弹与敌机组之间的碰撞,失常状况下其实就是1对1的碰撞                    enemy_hit = pygame.sprite.spritecollide(bul, enemiesGroup, False, pygame.sprite.collide_mask)                    # 如果存在被子弹击中的敌机                    if enemy_hit:                        # 击中敌机的子弹先标为未激活状态,下一次循环到这个子弹的时候其实会重置的,又会显示进去                        bul.active = False                        for ei in enemy_hit:                            ei.active = False            # 绘制分数文字,pygame.font.Font.render():在一个新 Surface 对象上绘制文本            # pygame.font.Font.render(text, antialias, color, background=None)            # text:要显示的文字  antialias: 为True时文本图像显示更润滑,为False时文本图像显示有锯齿状            # color:字体色彩 background:背景色彩(可选参数),默认为小黑屏            score_text = score_font.render('Score : %s' % str(score), True, WHITE)            screen.blit(score_text, (10, 5))            # 绘制超级炸弹图片以及数量显示            bomb_text = bomb_font.render('* %d' % bomb_num, True, WHITE)            bomb_text_rect = bomb_text.get_rect()            screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))            screen.blit(bomb_text, (bomb_rect.width + 20, height - 5 - bomb_text_rect.height))            # 绘制玩家残余生命数            if life_num > 0:                for i in range(life_num):                    screen.blit(life_image, (width - 10 - (i+1)*life_rect.width, height - 10 - life_rect.height))            delay -= 1            if delay == 0:                delay = 100            # 每5帧切换一下航行图片款式            if delay % 5 == 0:                switch_image = not switch_image        # 生命小于0,代表游戏完结,启动游戏完结事件处理        elif life_num == 0:            # 进行产生补给            pygame.time.set_timer(bullet_time_supply, 0)            # 关上历史记录文件,读取历史最高分            with open('record.txt', 'r') as f:                record_score = int(f.read())            # 如果玩家分数大于历史最高分,则写入            if score > record_score:                with open('record.txt', 'w') as f:                    f.write(str(score))            # 绘制完结界面            # 绘制历史最高分数            record_text_score = score_font.render('Best : %d' % int(record_score), True, WHITE)            screen.blit(record_text_score, (100, 50))            # 本次游戏所得分数题目            gameover_text1 = gameover_font.render('Your Score', True, WHITE)            gameover_text1_rect = gameover_text1.get_rect()            # 让横坐标居中            screen.blit(gameover_text1, ((width - gameover_text1_rect.width) // 2, (height // 2 - 100)))            # 本次游戏所得分数            gameover_text2 = gameover_font.render(str(score), True, WHITE)            gameover_text2_rect = gameover_text2.get_rect()            # 让横坐标居中            screen.blit(gameover_text2, ((width - gameover_text2_rect.width) // 2, (height // 2 - 30)))            # 绘制从新开始            again_rect.left, again_rect.top = (width - again_rect.width) // 2, \                                              (height // 2 + 90)            screen.blit(again_image, again_rect)            # 绘制完结游戏            gameover_rect.left, gameover_rect.top = (width - gameover_rect.width) // 2, \                                                    (height // 2 + 150)            screen.blit(gameover_image, gameover_rect)            # 检测用户的鼠标操作            # 如果用户按下鼠标左键            if pygame.mouse.get_pressed()[0]:                # 获取鼠标坐标                pos = pygame.mouse.get_pos()                # 如果用户点击从新开始                if again_rect.left < pos[0] < again_rect.right and \                        again_rect.top < pos[1] < again_rect.bottom:                    # 从新开始游戏                    main()                # 如果用户点击完结游戏                elif gameover_rect.left < pos[0] < gameover_rect.right and \                        gameover_rect.top < pos[1] < gameover_rect.bottom:                    # 完结游戏                    pygame.quit()                    sys.exit()        # 绘制暂停按钮图片        screen.blit(paused_image, paused_rect)        # 更新整个待显示的  Surface 对象到屏幕上,将内存中的内容显示到屏幕上        pygame.display.flip()        # 通过时钟对象指定循环频率,每秒循环60次        # 帧速率是指程序每秒在屏幕山绘制图        clock.tick(60)if __name__ == "__main__":    try:        main()    # 服务失常退出    except SystemExit:        print("游戏失常退出!")        # pass疏忽谬误并持续往下运行,其实这里以及退出了        pass    # 服务呈现其余的异样    except:        # 间接将谬误打印进去        traceback.print_exc()        pygame.quit()

bullet.py

import pygame# 子弹1class Bullet1(pygame.sprite.Sprite):    # 这里的position其实是玩家飞机的地位,因为飞机的地位是变动的,所有子弹也是变动的    def __init__(self, position):        # 这里还是一样,”基类的初始化“,具体看enemy.py外面有介绍        pygame.sprite.Sprite.__init__(self)        self.image = pygame.image.load('images/bullet1.png').convert_alpha()        # get_rect()是一个解决矩形图像的办法,返回值蕴含矩形的各属性,这里返回子弹的地位,能够获取图片的宽低等属性        self.rect = self.image.get_rect()        # 定义子弹的地位        self.rect.left = position[0]        self.rect.top = position[1]        # 判断子弹是否激活状态        self.active = True        # 子弹速度        self.bulletSpeed = 12        # 碰撞检测,会疏忽掉图片中红色的背景局部        self.mask = pygame.mask.from_surface(self.image)    # 子弹挪动    def move(self):        self.rect.top -= self.bulletSpeed        # 超出屏幕以外,定义为未激活状态,同时kill掉,不然耗费资源        if self.rect.top < 0:            self.active = False    # 子弹重置    def reset(self, position):        self.active = True        self.rect.left = position[0]        self.rect.top = position[1]# 子弹2,即增强子弹class Bullet2(pygame.sprite.Sprite):    # 这里的position其实是玩家飞机的地位,因为飞机的地位是变动的,所有增强子弹也是变动的    def __init__(self, position):        pygame.sprite.Sprite.__init__(self)        # 增强子弹图片        self.image = pygame.image.load('images/bullet2.png').convert_alpha()        # get_rect()是一个解决矩形图像的办法,返回值蕴含矩形的各属性,这里返回子弹的地位,能够获取图片的宽低等属性        self.rect = self.image.get_rect()        # 定义子弹的地位        self.rect.left = position[0]        self.rect.top = position[1]        # 判断子弹是否激活状态        self.active = True        # 增强子弹速度        self.bulletSpeed = 14        # 碰撞检测,会疏忽掉图片中红色的背景局部        self.mask = pygame.mask.from_surface(self.image)    # 增强子弹挪动    def move(self):        self.rect.top -= self.bulletSpeed        # 超出屏幕以外,定义为未激活状态        if self.rect.top < 0:            self.active = False    # 子弹重置    def reset(self, position):        self.active = True        self.rect.left = position[0]        self.rect.top = position[1]

enemy.py

import pygamefrom random import *# 小型飞机类# pygame.sprite.Sprite就是Pygame外面用来实现精灵的一个类,应用时,并不需要对它实例化,只须要继承他,而后按需写出本人的类就好了,因而非常简单实用# 所有精灵在建设时都是从pygame.sprite.Sprite中继承的class SmallEnemy(pygame.sprite.Sprite):    def __init__(self, bg_size):        # 这里波及到”基类的初始化“建用百度好好了解一下。        # 艰深来说,SmallEnemy类继承了父类pygame.sprite.Sprite,子类父类都有__init__()这个函数,如果子类不实现这个init()函数,        # 那么初始化时间接调用父类的初始化函数,如果子类实现这个init()函数,就笼罩了父类的这个函数,既然继承父类,就要在这个函数里显式调用一下父类的__init__()        pygame.sprite.Sprite.__init__(self)        # 敌机图片        self.image = pygame.image.load('images/enemy1.png').convert_alpha()        # 敌机捣毁图片        self.destory_images = []        self.destory_images.extend([            pygame.image.load('images/enemy1_down1.png').convert_alpha(),            pygame.image.load('images/enemy1_down2.png').convert_alpha(),            pygame.image.load('images/enemy1_down3.png').convert_alpha(),            pygame.image.load('images/enemy1_down4.png').convert_alpha()        ])        # 定义屏幕宽高        self.width = bg_size[0]        self.height = bg_size[1]        # get_rect()是一个解决矩形图像的办法,返回值蕴含矩形的各属性,这里返回飞机图片1的地位,能够获取图片的宽低等属性        self.rect = self.image.get_rect()        # 随机生成飞机的地位,randint(a,b)即生成a<=n<=b,即在屏幕宽度,以及负5倍的高度下随机生成        self.rect.left = randint(0, self.width - self.rect.width)        self.rect.top = randint(-5 * self.height, 0)        # 小型敌机速度        self.speed = 2        # 敌机是否存活状态        self.active = True        # 飞机碰撞检测,会疏忽掉图片中红色的背景局部        self.mask = pygame.mask.from_surface(self.image)    # 小型敌机向下挪动    def samll_enemy_move(self):        # 飞机还未飞出屏幕外,就向下静止(这里能够本人批改飞机的航行轨迹,比方x轴的随机左右航行等,怎么浪怎么来)        if self.rect.top < self.height:            self.rect.top += self.speed        else:            self.reset()    # 飞机飞出屏幕外后,别节约,重置其地位(或者捣毁也行,反正得解决,不然内存会炸)    def reset(self):        # 这里写不写这个感觉都行,因为都是存活状态,写了更保险        self.active = True        # 随机重置飞机的地位,跟下面生成得一样        self.rect.left = randint(0, self.width - self.rect.width)        self.rect.top = randint(-5 * self.height, 0)

supply.py

import pygamefrom random import *# 子弹补给class Bullet_Supply(pygame.sprite.Sprite):    def __init__(self, bg_size):        pygame.sprite.Sprite.__init__(self)        # 子弹补给的图片        self.image = pygame.image.load('images/bullet_supply.png').convert_alpha()        # 定义屏幕宽高        self.width = bg_size[0]        self.height = bg_size[1]        # get_rect()是一个解决矩形图像的办法,返回值蕴含矩形的各属性,这里返回飞机图片1的地位,能够获取图片的宽低等属性        self.rect = self.image.get_rect()        # 随机子弹补给的地位,randint(a,b)即生成a<=n<=b,即在屏幕宽度,以及2倍的高度下随机生成        self.rect.left = randint(0, self.width - self.rect.width)        self.rect.top = randint(-2 * self.height, 0)        # 补给的存活状态,即是否显示以及是否触碰        self.active = False        # 补给降落状态        self.speed = 5    # 子弹补给静止    def move(self):        # 还未降落到最底部        if self.rect.top < self.height:            # 则持续向下静止            self.rect.top += self.speed        else:            # 降落到最底部后,状态变为未激活状态            self.active = False    # 子弹补给重置    def reset(self):        # 状态重置        self.active = True        # 随机子弹补给的地位,randint(a,b)即生成a<=n<=b,即在屏幕宽度,以及2倍的高度下随机生成        self.rect.left = randint(0, self.width - self.rect.width)        self.rect.top = randint(-2 * self.height, 0)# 超级炸弹补给class Bomb_Supply(pygame.sprite.Sprite):    def __init__(self, bg_size):        pygame.sprite.Sprite.__init__(self)        # 超级炸弹图片        self.image = pygame.image.load('images/bomb_supply.png').convert_alpha()        # 定义屏幕宽高        self.width = bg_size[0]        self.height = bg_size[1]        # get_rect()是一个解决矩形图像的办法,返回值蕴含矩形的各属性,这里返回飞机图片1的地位,能够获取图片的宽低等属性        self.rect = self.image.get_rect()        # 超级炸弹补给的地位,randint(a,b)即生成a<=n<=b,即在屏幕宽度,以及2倍的高度下随机生成        self.rect.left = randint(0, self.width - self.rect.width)        self.rect.top = randint(-2 * self.height, 0)        # 补给的存活状态,即是否显示以及是否触碰        self.active = False        # 补给降落状态        self.speed = 5    # 超级炸弹挪动    def move(self):        # 还未降落到最底部        if self.rect.top < self.height:            # 则持续向下静止            self.rect.top += self.speed        else:            # 降落到最底部后,状态变为未激活状态            self.active = False# 子弹补给重置    def reset(self):        # 状态重置        self.active = True        # 超级炸弹补给的地位,randint(a,b)即生成a<=n<=b,即在屏幕宽度,以及2倍的高度下随机生成        self.rect.left = randint(0, self.width - self.rect.width)        self.rect.top = randint(-2 * self.height, 0)

myplane.py

import pygame# 玩家飞机类,pygame.sprite模块外面蕴含了一个名为Sprite类,他是pygame自身自带的一个精灵。class MyPlane(pygame.sprite.Sprite):    def __init__(self, bg_size):        # convert_alpha()更改图像的像素格局,包含每个像素的alpha,相当于图片背景变为通明        self.image1 = pygame.image.load('images/me1.png').convert_alpha()        self.image2 = pygame.image.load('images/me2.png').convert_alpha()        # 飞机捣毁图片,以数字模式保留        self.destory_image = []        self.destory_image.extend([            pygame.image.load('images/me_destroy_1.png').convert_alpha(),            pygame.image.load('images/me_destroy_2.png').convert_alpha(),            pygame.image.load('images/me_destroy_3.png').convert_alpha(),            pygame.image.load('images/me_destroy_4.png').convert_alpha()        ])        # 定义屏幕宽高        self.width = bg_size[0]        self.height = bg_size[1]        # get_rect()是一个解决矩形图像的办法,返回值蕴含矩形的各属性,这里返回飞机图片1的地位,能够获取图片的宽低等属性        self.rect = self.image1.get_rect()        # 飞机的初始化地位,//是整除,地位居中以及高度为图片下框离屏幕最下方60        self.rect.left = (self.width - self.rect.width)//2        self.rect.top = self.height - self.rect.height - 60        # 设置飞机的速度        self.myPlaneSpeed = 10        self.active = True        # 设置飞机是否是无敌状态(新生3秒内无敌)        self.invincible = False        # 飞机碰撞检测,会疏忽掉图片中红色的背景局部,从指定 Surface 对象中返回一个 Mask        # 用于疾速实现完满的碰撞检测,Mask 能够准确到 1 个像素级别的判断。        # Surface 对象中通明的局部设置为 1,不通明局部设置为 0。        self.mask = pygame.mask.from_surface(self.image1)    # 玩家飞机向上挪动    def moveUp(self):        # 阐明还没定格,即还未达到游戏界面上边界        if self.rect.top > 0:            self.rect.top -= self.myPlaneSpeed        # 阐明挪动达到上边界了        else:            self.rect.top = 0    # 玩家飞机向下挪动    def moveDown(self):        # 底部须要划出60的高度用来展现其余数据(炸弹数,生命数等)        if self.rect.bottom < self.height - 60:            # self.rect.bottom指的是飞机图片下边界            self.rect.bottom += self.myPlaneSpeed        else:            self.rect.bottom = self.height - 60    # 玩家飞机向左挪动    def moveLeft(self):        if self.rect.left > 0:            self.rect.left -= self.myPlaneSpeed        else:            self.rect.left = 0    # 玩家飞机向右挪动    def moveRight(self):        if self.rect.right < self.width:            self.rect.right += self.myPlaneSpeed        else:            self.rect.right = self.width    # 玩家飞机新生    def reset(self):        self.active = True        # 新生时处于无敌状态        self.invincible = True        # 新生飞机的初始化地位,//是整除,地位居中以及高度为图片下框离屏幕最下方60        self.rect.left = (self.width - self.rect.width) // 2        self.rect.top = self.height - self.rect.height - 60