Pygame:飞机大战7(详细解读)

霸气de小男生 提交于 2020-02-17 18:36:28


🚴大家好!我是近视的脚踏实地,虽然近视,但是脚踏实地。这一篇继续要完善飞机大战的游戏,这篇主要完成的内容是给添加给玩家三条生命值的功能,以及游戏结束画面的处理

(一)完成给玩家三次游戏机会的功能

在这里插入图片描述
很多游戏都会给玩家提供多次尝试的机会,因此我们也添加这么一个功能,玩家总共会有三次机会,在游戏界面的右下角通过显示多少个小飞机来提示玩家还剩下多少次机会,那这张图片其实就是玩家飞机的缩小版

1️⃣在屏幕右下角绘制玩家生命数

myplane.py 👇

#myplane.py
import pygame

class MyPlane(pygame.sprite.Sprite):
     def __init__(self,bg_size):
          pygame.sprite.Sprite.__init__(self)

          self.image1 = pygame.image.load("images/me1.png").convert_alpha()
          self.image2 = pygame.image.load("images/me2.png").convert_alpha()
          self.destroy_images = []
          self.destroy_images.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.rect = self.image1.get_rect()
          self.width,self.height = bg_size[0],bg_size[1]
          self.rect.left,self.rect.top = \
                                   (self.width - self.rect.width )// 2,\
                                   self.height - self.rect.height - 60
          self.speed = 10
          self.active = True
          self.invincible = False
          self.mask = pygame.mask.from_surface(self.image1)

     #向上走
     def moveUp(self):
          if self.rect.top > 0:
               self.rect.top -= self.speed
          else:
               self.rect.top = 0
               
     #向下走
     def moveDown(self):
          if self.rect.bottom < self.height - 60:
               self.rect.top += self.speed
          else:
               self.rect.bottom = self.height - 60
               
     #向左走
     def moveLeft(self):
          if self.rect.left > 0:
               self.rect.left -= self.speed
          else:
               self.rect.left = 0

     #向右走
     def moveRight(self):
          if self.rect.right < self.width:
               self.rect.right += self.speed
          else:
               self.rect.right = self.width
               
     # 玩家复活
     def reset(self):
          self.rect.left, self.rect.top = \
                        (self.width - self.rect.width) // 2, \
                        self.height - self.rect.height - 60
          self.active = True
          self.invincible = True

在这里插入图片描述
那么想要玩家的飞机可以reset,要可以复活,所以我们在myplane.py中来添加一个reset方法, 那么放里边就首先是将飞机仍然初始化在页面底部的中央,然后self.active = True 将玩家的active属性设置为True,我们是通过active属性来判断他是是否还活着,self.invincible = True 这个属性就标致玩家的飞机是否无敌,具体用到后边再来

在main.py中进行修改 👇
在这里插入图片描述

那首先之前我们的逻辑就是,先检测玩家的飞机是否被撞,如果被撞,就把active属性设置为False,接着下边检测到玩家的对象的active属性为False时,就来绘制他毁灭的画面,绘制完后就打印了Game Over,那么现在我们想让玩家有三次尝试的机会,所以我们要在main模块中加多一个lief_num的变量来表示还有多少次机会,那么首先也是先把小飞机的图片加载进去,然后获取它的矩形对象,设置他的生命数量为3
在这里插入图片描述
接着就是如果检测到玩家的active属性为False,绘制毁灭画面后就life_num -= 1 ,生命数量减1,接着me.reset() 再来重生

那么接下来就是在屏幕右下角绘制生命数量的图片,在绘制左下角的全屏炸弹后绘制就好了,for i in range(life_num): 那么就是来迭代life_num这个变量,如果还有剩的话就screen.blit(life_image, (width-10-(i+1)*life_rect.width, height-10-life_rect.height)) 在右下角绘制出来,参数就是要来计算偏移了 x方向就是屏幕的宽度width -10 再减去剩下多少条命乘以一张小飞机的宽度,y方向就是屏幕的高度减10再减小飞机矩形对象的height

在这里插入图片描述
因为游戏的主流层,在我们没有限制生命的时候,我们是按下恢复了他才执行的,按下暂停的时候这个主流层里边的循环就不执行,所以这里需要多加一个条件,就是说只有当玩家还有剩余声明的时候且游戏不暂停的时候,才执行这个主流层,这个主循环的内容

在这里插入图片描述
那么如果玩家没有剩余声明了的话,就来绘制结束画面,elif life_num == 0: 这里不能只是else,因为你else的话玩家按下暂停也会直接跑到游戏结束的界面,如果玩家剩余生命为0了,我们现在先打印,一步一步来,我们后边再来绘制出来,那么先来运行测试一下

测试结果如下: 👇
在这里插入图片描述

那么其实这里有个小细节,就当玩家打到后边很多飞机的时候就会发现,每次我方飞机牺牲后,如果说你你诞生的这个位置,就是底部中央的位置,恰好有一辆敌机,那么会导致玩家飞机一诞生就牺牲的惨状发生,因此我们要设定每次牺牲会有3秒钟的安全期,也就是无敌状态

2️⃣完成玩家复活后三秒真男人状态

myplane.py

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

那么就是首先在myplane模块多加一个self.invincible = False,然后在检测碰撞的时候,检测到这个属性是True还是False就可以了,那么之前我们已经在myplane模块里添加好了,然后在reset方法中也添加好了,重生时,就把它的属性值设置为True,表示开启无敌状态

main.py
在这里插入图片描述
那么无敌的时间只会保持3秒,太久的话对敌机不公平,所以这里多加一个计时器INVINCIBLE_TIME = USEREVENT + 2 这里表示我们的第三个自定义事件
在这里插入图片描述
在这里插入图片描述
然后当玩家飞机发生重生的时候,那我们就来调用这个计时器,然后在检测让他们是否相撞的时候,就not me.invincible: 不是无敌状态下才把active属性设置为False。也就说当me.invincible: 玩家飞机的这个属性为True的时候,那么not True就是Fasle,那么这个条件就不会成立,敌方就撞不到我们,就不会产生后面的事件。

在这里插入图片描述

那么接着3秒钟到后,我们就来响应INVINCIBLE_TIME 这个事件来解除无敌状态,使得me.invincible = False ,然后pygame.time.set_timer(INVINCIBLE_TIME, 0) 取消这个计时器就可以了,下面来测试一下

测试结果如下: 👇
在这里插入图片描述

(二)绘制游戏结束画面

在这里插入图片描述在这里插入图片描述
也就是说当life_num的值为0的时候,说明玩家已经输掉了游戏,但是玩家没有输掉人生,所以这个游戏结束的画面就要求上门最历史最高的分数,然后历史最高分是存在record.txt文档里边,然后中间显示本次游戏的分数,如果这个分数比历史最高分还高就要进行一个存档,下面有重新开始和结束游戏的按钮可以进行选择。

在这里插入图片描述
结束的时候我们应该做一些收尾的工作,比如说,背景音乐要去掉,音效也应该去掉,如果没有去掉,大飞机出来就会噜噜噜的响,永远不会停,所以应该把音效给去掉,然后把补给的定时器也应该给取消,停止,不然隔30秒还能听到补给的音效

在这里插入图片描述
那么首先因为整个一个while循环就是一帧,如果当游戏life_num等于0的时候,事实上就是不断地,像刚刚第一个测试截图,会看到很多个GAME OVER,那就是不断在刷除了主流层里被暂定的代码的以外的代码,那么不断地刷,整个文件就会不会不断地打开,不断关闭,那就非常消耗资源,也很有可能出错,所以这里加多了变量来限制他来限制打开一次就可以了
在这里插入图片描述

那么就是recorded 刚开始的默认值是Fasle,那么就是if not recorded: ,判断如果没有打开,就执行里面的内容,进到后就把recorded = True,设置为True,那以后他都不会进来了,就只执行一次,接着读取里面的最高分啊,然后玩家的分数比最高分还高就存档啊,这里就是文件读取的一些基本内容。然后这里要注意的就是这里并没有进行这个文件存不存在的判断,最省事的办法就是自己去这些源文件所在的当前文件夹创建出一个,比较专业的就是先来判断,是否有这个文件,没有就先创建一个,然后给出分数是0,文章最后我会给出自己改进的方法

在这里插入图片描述
接着就是来绘制结束的界面,这里就是加载图片,获取他们的矩形对象等,设置字体,这些都已经熟悉得不能再熟悉了,就不赘述了
在这里插入图片描述
接着就是来把结束画面个画出来,就是一些打印字体啊,先渲染成surface对象,然后调整好贴上去的位置好了,都是之前的知识了,就不一一啰嗦了

在这里插入图片描述
然后就是要响应玩家点下的按钮对应的操作了,也很容易理解,认真看一遍就好了
在这里插入图片描述

最后我们需要把绘制得分的部分放到主流层中,否则结束画面也会把左上角的得分显示出来的

1️⃣附上自己修改第一次游戏结束没有record.text文件报的文件不存在异常的解决方案

那么如果你的当前文件夹里没有record.text 这个文件,游戏结束后就会闪退,报错的,所以还不够完善,将来给别人用户,别人可没有意识要先去创建个文本来保存最高分。下面给出自己想的办法,路过的大佬有别的办法也可以指点指点,必须很多事情都有各种不同的解决方案
在这里插入图片描述
那我就是先去上面定义了一个record_score 变量,然后他的默认值就给0

if not recorded:
                recorded = True
                # 读取历史最高得分
                try:
                    with open("record.txt", "r") as f:
                        record_score = int(f.read())
                except FileNotFoundError:
                    with open("record.txt", "w") as f:
                        f.write(str(record_score))


                # 如果玩家得分高于历史最高得分,则存档
                if score > record_score:
                    with open("record.txt", "w") as f:
                        f.write(str(score))

在这里插入图片描述

接着我就是用了处理异常的方法,如果第一次打开,不存在record.text 这个文件,就跳到处理异常的地方,以**"w"的形式open**这个文件,那么如果没有,他就会在源代码默认的文件夹里自动帮你创建出这个文件夹,然后 f.write(str(record_score)) 往里面写入刚刚定义好的默认历史最高分0,因为写入要以字符串的形式写入,所以这里要强转一下,那么创建好之后,以后就存在这个文件了,用户分数大于历史分数时就会写入覆盖越来的分数,直接读就好了,就不会再有文件不存在的异常了

测试结果如下: 👇
在这里插入图片描述
在这里插入图片描述

2️⃣附上这个飞机大战项目的所有源码,加油!

1️⃣子弹模块----bullet.py

bullet.py

# bullet.py
import pygame

#普通子弹
class Bullet1(pygame.sprite.Sprite):
     def __init__(self,position):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.image.load("images/bullet1.png").convert_alpha()
          self.rect = self.image.get_rect()
          self.rect.left,self.rect.top = position
          self.speed = 12
          self.active = False
          self.mask = pygame.mask.from_surface(self.image)


     def move(self):
          self.rect.top -= self.speed

          if self.rect.top < 0:
               self.active = False

     def reset(self,position):
          self.rect.left,self.rect.top = position
          self.active = True
          

#超级子弹
class Bullet2(pygame.sprite.Sprite):
     def __init__(self,position):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.image.load("images/bullet2.png").convert_alpha()
          self.rect = self.image.get_rect()
          self.rect.left,self.rect.top = position
          self.speed = 14
          self.active = False
          self.mask = pygame.mask.from_surface(self.image)


     def move(self):
          self.rect.top -= self.speed

          if self.rect.top < 0:
               self.active = False

     def reset(self,position):
          self.rect.left,self.rect.top = position
          self.active = True

2️⃣敌机模块----enemy.py

enemy.py

#enemy.py
import pygame
from random import *

#小型敌机
class SmallEnemy(pygame.sprite.Sprite):
     def __init__(self,bg_size):
          pygame.sprite.Sprite.__init__(self)

          self.image = pygame.image.load("images/enemy1.png").convert_alpha()
          self.destroy_images = []
          self.destroy_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.rect = self.image.get_rect()
          self.width,self.height = bg_size[0],bg_size[1]
          self.speed = 2
          self.active = True
          self.rect.left,self.rect.top = \
                         randint(0, self.width - self.rect.width),\
                         randint(-5 * self.height, 0)
          self.mask = pygame.mask.from_surface(self.image)


     def move(self):
          if self.rect.top < self.height:
               self.rect.top += self.speed
          else:
               self.reset()

     def reset(self):
          self.active = True
          self.rect.left,self.rect.top = \
                         randint(0, self.width - self.rect.width),\
                         randint(-5 * self.height,0)

#中型敌机
class MidEnemy(pygame.sprite.Sprite):
     energy = 8
     
     def __init__(self,bg_size):
          pygame.sprite.Sprite.__init__(self)

          self.image = pygame.image.load("images/enemy2.png").convert_alpha()
          self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()
          self.destroy_images = []
          self.destroy_images.extend([\
            pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
            pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
            pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
            pygame.image.load("images/enemy2_down4.png").convert_alpha() \
            ])
          self.rect = self.image.get_rect()
          self.width,self.height = bg_size[0],bg_size[1]
          self.speed = 1
          self.active = True
          self.rect.left,self.rect.top = \
                         randint(0, self.width - self.rect.width),\
                         randint(-10 * self.height,-self.height)
          self.mask = pygame.mask.from_surface(self.image)
          self.energy = MidEnemy.energy
          self.hit = False


     def move(self):
          if self.rect.top < self.height:
               self.rect.top += self.speed
          else:
               self.reset()

     def reset(self):
          self.active = True
          self.energy = MidEnemy.energy
          self.rect.left,self.rect.top = \
                         randint(0, self.width - self.rect.width),\
                         randint(-10 * self.height,-self.height)

#大型敌机
class BigEnemy(pygame.sprite.Sprite):
     energy = 20
     
     def __init__(self,bg_size):
          pygame.sprite.Sprite.__init__(self)

          self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
          self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
          self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()
          self.destroy_images = []
          self.destroy_images.extend([\
            pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
            pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
            pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
            pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
            pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
            pygame.image.load("images/enemy3_down6.png").convert_alpha() \
            ])
          self.rect = self.image1.get_rect()
          self.width,self.height = bg_size[0],bg_size[1]
          self.speed = 1
          self.active = True
          self.rect.left,self.rect.top = \
                         randint(0, self.width - self.rect.width),\
                         randint(-15 * self.height,-5 * self.height)
          self.mask = pygame.mask.from_surface(self.image1)
          self.energy = BigEnemy.energy
          self.hit = False


     def move(self):
          if self.rect.top < self.height:
               self.rect.top += self.speed
          else:
               self.reset()

     def reset(self):
          self.active = True
          self.energy = BigEnemy.energy
          self.rect.left,self.rect.top = \
                         randint(0, self.width - self.rect.width),\
                         randint(-15 * self.height,-5 * self.height)

3️⃣玩家飞机模块----myplane.py

myplane.py

#myplane.py
import pygame

class MyPlane(pygame.sprite.Sprite):
     def __init__(self,bg_size):
          pygame.sprite.Sprite.__init__(self)

          self.image1 = pygame.image.load("images/me1.png").convert_alpha()
          self.image2 = pygame.image.load("images/me2.png").convert_alpha()
          self.destroy_images = []
          self.destroy_images.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.rect = self.image1.get_rect()
          self.width,self.height = bg_size[0],bg_size[1]
          self.rect.left,self.rect.top = \
                                   (self.width - self.rect.width )// 2,\
                                   self.height - self.rect.height - 60
          self.speed = 10
          self.active = True
          self.invincible = False
          self.mask = pygame.mask.from_surface(self.image1)

     #向上走
     def moveUp(self):
          if self.rect.top > 0:
               self.rect.top -= self.speed
          else:
               self.rect.top = 0
               
     #向下走
     def moveDown(self):
          if self.rect.bottom < self.height - 60:
               self.rect.top += self.speed
          else:
               self.rect.bottom = self.height - 60
               
     #向左走
     def moveLeft(self):
          if self.rect.left > 0:
               self.rect.left -= self.speed
          else:
               self.rect.left = 0

     #向右走
     def moveRight(self):
          if self.rect.right < self.width:
               self.rect.right += self.speed
          else:
               self.rect.right = self.width
               
     # 玩家复活
     def reset(self):
          self.rect.left, self.rect.top = \
                        (self.width - self.rect.width) // 2, \
                        self.height - self.rect.height - 60
          self.active = True
          self.invincible = True 

4️⃣补给包模块----supply.py

supply.py

# supply.py
import pygame
from 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.rect = self.image.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.rect.left, self.rect.bottom = \
                        randint(0, self.width - self.rect.width), -100
        self.speed = 5
        self.active = False
        self.mask = pygame.mask.from_surface(self.image)

    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.active = False

    def reset(self):
        self.active = True
        self.rect.left, self.rect.bottom = \
                        randint(0, self.width - self.rect.width), -100
                 
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.rect = self.image.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.rect.left, self.rect.bottom = \
                        randint(0, self.width - self.rect.width), -100
        self.speed = 5
        self.active = False
        self.mask = pygame.mask.from_surface(self.image)

    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.active = False

    def reset(self):
        self.active = True
        self.rect.left, self.rect.bottom = \
                        randint(0, self.width - self.rect.width), -100                                          
            

5️⃣主模块----main.py

main.py

import pygame
import sys
import traceback
import myplane
import enemy
import bullet
import supply

from pygame.locals import *
from random import *


pygame.init()
pygame.mixer.init()

bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战 -- Monster ZF")


background = pygame.image.load("images/background.png").convert()

BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0,0)
WHITE = (255,255,255)

# 载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.1)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)

def add_small_enemies(group1,group2,num):
    for i in range(num):
        e1 = enemy.SmallEnemy(bg_size)
        group1.add(e1)
        group2.add(e1)

def add_mid_enemies(group1,group2,num):
    for i in range(num):
        e2 = enemy.MidEnemy(bg_size)
        group1.add(e2)
        group2.add(e2)
        
def add_big_enemies(group1,group2,num):
    for i in range(num):
        e3 = enemy.BigEnemy(bg_size)
        group1.add(e3)
        group2.add(e3)
        
def inc_speed(target, inc):
    for each in target:
        each.speed += inc

def main():
    pygame.mixer.music.play(-1)

    #生成玩家飞机
    me = myplane.MyPlane(bg_size)

    #生成所有敌机汇总
    enemies = pygame.sprite.Group()

    #生成小型敌机
    small_enemies = pygame.sprite.Group()
    add_small_enemies(small_enemies,enemies,15)

    #生成中型敌机
    mid_enemies = pygame.sprite.Group()
    add_mid_enemies(mid_enemies,enemies,4)

    #生成大型敌机
    big_enemies = pygame.sprite.Group()
    add_big_enemies(big_enemies,enemies,2)

    #生成普通子弹
    bullet1 = []
    bullet1_index = 0
    BULLET1_NUM = 4
    for i in range(BULLET1_NUM):
        bullet1.append(bullet.Bullet1(me.rect.midtop))

    # 生成超级子弹
    bullet2 = []
    bullet2_index = 0
    BULLET2_NUM = 8
    for i in range(BULLET2_NUM//2):
        bullet2.append(bullet.Bullet2((me.rect.centerx-33, me.rect.centery)))
        bullet2.append(bullet.Bullet2((me.rect.centerx+30, me.rect.centery)))   

    clock = pygame.time.Clock()

    # 中弹图片索引
    e1_destroy_index = 0
    e2_destroy_index = 0
    e3_destroy_index = 0
    me_destroy_index = 0

    # 统计得分
    score = 0

    #字体的定义
    score_font = pygame.font.Font("font/font.ttf",36)

    #标志是否暂停游戏
    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_rect = pause_nor_image.get_rect()
    paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
    paused_image = pause_nor_image

    #默认历史最高分是0
    record_score = 0

    #设置难度级别
    level = 1

    # 全屏炸弹
    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)
    bomb_num = 3

    # 每30秒发放一个补给包
    bullet_supply = supply.Bullet_Supply(bg_size)
    bomb_supply = supply.Bomb_Supply(bg_size)                                
    SUPPLY_TIME = USEREVENT
    pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)

    # 超级子弹定时器
    DOUBLE_BULLET_TIME = USEREVENT + 1

    # 标志是否使用超级子弹
    is_double_bullet = False

    # 解除玩家无敌状态
    INVINCIBLE_TIME = USEREVENT + 2

    # 生命数量
    life_image = pygame.image.load("images/life.png").convert_alpha()
    life_rect = life_image.get_rect()
    life_num = 3

    # 用于阻止重复打开记录文件
    recorded = False

    # 游戏结束画面
    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()
    
    #用于切换图片
    switch_image = True

    #用于延迟
    delay = 100

    
    running = True


    while running:
        for event in pygame.event.get():
            if event.type == QUIT:       
                pygame.quit()
                sys.exit()

            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1 and paused_rect.collidepoint(event.pos):
                    paused = not paused
                    if paused:
                        pygame.time.set_timer(SUPPLY_TIME, 0)
                        pygame.mixer.music.pause()
                        pygame.mixer.pause()
                    else:
                        pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
                        pygame.mixer.music.unpause()
                        pygame.mixer.unpause()

            elif event.type == MOUSEMOTION:
                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 == KEYDOWN:
                if event.key == K_SPACE:
                    if bomb_num:
                        bomb_num -= 1
                        bomb_sound.play()
                        for each in enemies:
                            if each.rect.bottom > 0:
                                each.active = False

            elif event.type == SUPPLY_TIME:
                supply_sound.play()
                if choice([True, False]):
                    bomb_supply.reset()
                else:
                    bullet_supply.reset()
                    
            elif event.type == DOUBLE_BULLET_TIME:
                is_double_bullet = False
                pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)

            elif event.type == INVINCIBLE_TIME:
                me.invincible = False
                pygame.time.set_timer(INVINCIBLE_TIME, 0)   
                        

        # 根据用户的得分增加游戏难度
        if level == 1 and score > 50000:
            level = 2
            upgrade_sound.play()
            # 增加3架小型敌机,2架中型敌机和1架大型敌机
            add_small_enemies(small_enemies,enemies,3)
            add_mid_enemies(mid_enemies, enemies, 2)
            add_big_enemies(big_enemies, enemies, 1)
            # 提升小型敌机的速度
            inc_speed(small_enemies, 1)
        elif level == 2 and score > 300000:
            level = 3
            upgrade_sound.play()
            # 增加5架小型敌机、3架中型敌机和2架大型敌机
            add_small_enemies(small_enemies, enemies, 5)
            add_mid_enemies(mid_enemies, enemies, 3)
            add_big_enemies(big_enemies, enemies, 2)
            # 提升小型敌机的速度
            inc_speed(small_enemies, 1)
            inc_speed(mid_enemies, 1)
        elif level == 3 and score > 600000:
            level = 4
            upgrade_sound.play()
            # 增加5架小型敌机、3架中型敌机和2架大型敌机
            add_small_enemies(small_enemies, enemies, 5)
            add_mid_enemies(mid_enemies, enemies, 3)
            add_big_enemies(big_enemies, enemies, 2)
            # 提升小型敌机的速度
            inc_speed(small_enemies, 1)
            inc_speed(mid_enemies, 1)
        elif level == 4 and score > 1000000:
            level = 5
            upgrade_sound.play()
            # 增加5架小型敌机、3架中型敌机和2架大型敌机
            add_small_enemies(small_enemies, enemies, 5)
            add_mid_enemies(mid_enemies, enemies, 3)
            add_big_enemies(big_enemies, enemies, 2)
            # 提升小型敌机的速度
            inc_speed(small_enemies, 1)
            inc_speed(mid_enemies, 1)

        screen.blit(background,(0,0))
        
                       
        if life_num and not paused:
            #检测用户的键盘操作
            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()

            # 绘制全屏炸弹补给并检测是否获得
            if bomb_supply.active:
                bomb_supply.move()
                screen.blit(bomb_supply.image, bomb_supply.rect)
                if pygame.sprite.collide_mask(bomb_supply, me):
                    get_bomb_sound.play()
                    if bomb_num < 3:
                        bomb_num += 1
                    bomb_supply.active = False

            # 绘制超级子弹补给并检测是否获得
            if bullet_supply.active:
                bullet_supply.move()
                screen.blit(bullet_supply.image, bullet_supply.rect)
                if pygame.sprite.collide_mask(bullet_supply, me):
                    get_bullet_sound.play()
                    is_double_bullet = True
                    pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
                    bullet_supply.active = False
                                   

            # 发射子弹
            if not(delay % 10):
                bullet_sound.play()
                if is_double_bullet:
                    bullets = bullet2
                    bullets[bullet2_index].reset((me.rect.centerx-33, me.rect.centery))
                    bullets[bullet2_index+1].reset((me.rect.centerx+30, me.rect.centery))
                    bullet2_index = (bullet2_index + 2) % BULLET2_NUM
                else:
                    bullets = bullet1
                    bullets[bullet1_index].reset(me.rect.midtop)
                    bullet1_index = (bullet1_index + 1) % BULLET1_NUM
                    

            # 检测子弹是否击中敌机
            for b in bullets:
                if b.active:
                    b.move()
                    screen.blit(b.image,b.rect)
                    enemy_hit = pygame.sprite.spritecollide(b,enemies,False,pygame.sprite.collide_mask)
                    if enemy_hit:
                        b.active = False
                        for e in enemy_hit:
                            if e in mid_enemies or e in big_enemies:
                                e.hit = True
                                e.energy -= 1
                                if e.energy == 0:
                                    e.active = False
                            else:
                                e.active = False
                        
                    
                

            # 绘制大型敌机
            for each in big_enemies:
                if each.active:
                    each.move()
                    if each.hit:
                        #绘制被打到的特效
                        screen.blit(each.image_hit,each.rect)
                        each.hit = False
                    else:          
                        if switch_image:
                            screen.blit(each.image1,each.rect)
                        else:
                            screen.blit(each.image2,each.rect)

                    # 绘制血槽
                    pygame.draw.line(screen, BLACK, \
                                     (each.rect.left, each.rect.top - 5), \
                                     (each.rect.right, each.rect.top - 5), \
                                     2)
                    # 当生命大于20%显示绿色,否则显示红色
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    if energy_remain > 0.2:
                        energy_color = GREEN
                    else:
                        energy_color = RED
                    pygame.draw.line(screen, energy_color, \
                                     (each.rect.left, each.rect.top - 5), \
                                     (each.rect.left + int(each.rect.width * energy_remain), \
                                      each.rect.top - 5), 2)
                        
                    
                    #即将出现在画面中,播放音效
                    if each.rect.bottom == -50:
                        enemy3_fly_sound.play(-1)
                else:
                    # 毁灭
                    if not(delay % 3):
                        if e3_destroy_index == 0:
                            enemy3_down_sound.play()
                        screen.blit(each.destroy_images[e3_destroy_index], each.rect)
                        e3_destroy_index = (e3_destroy_index + 1) % 6
                        if e3_destroy_index == 0:
                            enemy3_fly_sound.stop()
                            score += 10000
                            each.reset()

                    

            # 绘制中型敌机:
            for each in mid_enemies:
                if each.active:
                    each.move()
                    if each.hit:
                        screen.blit(each.image_hit, each.rect)
                        each.hit = False
                    else:   
                        screen.blit(each.image,each.rect)

                     # 绘制血槽
                    pygame.draw.line(screen, BLACK, \
                                     (each.rect.left, each.rect.top - 5), \
                                     (each.rect.right, each.rect.top - 5), \
                                     2)
                    # 当生命大于20%显示绿色,否则显示红色
                    energy_remain = each.energy / enemy.MidEnemy.energy
                    if energy_remain > 0.2:
                        energy_color = GREEN
                    else:
                        energy_color = RED
                    pygame.draw.line(screen, energy_color, \
                                     (each.rect.left, each.rect.top - 5), \
                                     (each.rect.left + int(each.rect.width * energy_remain), \
                                      each.rect.top - 5), 2)
                    
                else:
                    # 毁灭
                    if not(delay % 3):
                        if e2_destroy_index == 0:
                            enemy2_down_sound.play()
                        screen.blit(each.destroy_images[e2_destroy_index], each.rect)
                        
                        e2_destroy_index = (e2_destroy_index + 1) % 4
                        if e2_destroy_index == 0:
                            score += 6000
                            each.reset()

            # 绘制小型敌机:
            for each in small_enemies:
                if each.active:
                    each.move()
                    screen.blit(each.image,each.rect)
                else:
                    # 毁灭
                    if not(delay % 3):
                        if e1_destroy_index == 0:
                            enemy1_down_sound.play()
                        screen.blit(each.destroy_images[e1_destroy_index], each.rect)
                        e1_destroy_index = (e1_destroy_index + 1) % 4
                        if e1_destroy_index == 0:
                            score += 1000
                            each.reset()

            #检测玩家飞机是否被撞
            enemies_down = pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)
            if enemies_down and not me.invincible:
                me.active = False
                for e in enemies_down:
                    e.active = False
            
                                  
            # 绘制玩家飞机
            if me.active:
                if switch_image:
                    screen.blit(me.image1,me.rect)
                else:
                    screen.blit(me.image2, me.rect)
            else:
                # 毁灭
                if not(delay % 3):
                    if me_destroy_index == 0:
                        me_down_sound.play()
                    screen.blit(me.destroy_images[me_destroy_index], me.rect)
                    me_destroy_index = (me_destroy_index + 1) % 4
                    if me_destroy_index == 0:
                        life_num -= 1
                        me.reset()
                        pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)
                        

            # 绘制剩余全屏炸弹数量
            bomb_text = bomb_font.render("× %d" % bomb_num, True, WHITE)
            text_rect = bomb_text.get_rect()
            screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
            screen.blit(bomb_text, (20 + bomb_rect.width, height - 5 - text_rect.height))
            
            # 绘制剩余生命数量
            if life_num:
                for i in range(life_num):
                    screen.blit(life_image, \
                                (width-10-(i+1)*life_rect.width, \

                                 height-10-life_rect.height))

            # 绘制得分
            score_text = score_font.render("Score : %s" % str(score), True, WHITE)
            screen.blit(score_text, (10, 5))
            
                    
        # 绘制游戏结束画面
        elif life_num == 0:
            # 背景音乐停止
            pygame.mixer.music.stop()

            # 停止全部音效
            pygame.mixer.stop()

            # 停止发放补给
            pygame.time.set_timer(SUPPLY_TIME, 0)

            if not recorded:
                recorded = True
                # 读取历史最高得分
                try:
                    with open("record.txt", "r") as f:
                        record_score = int(f.read())
                except FileNotFoundError:
                    with open("record.txt", "w") as f:
                        f.write(str(record_score))


                # 如果玩家得分高于历史最高得分,则存档
                if score > record_score:
                    with open("record.txt", "w") as f:
                        f.write(str(score))

            # 绘制结束画面
            record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
            screen.blit(record_score_text, (50, 50))
            
            gameover_text1 = gameover_font.render("Your Score", True, (255, 255, 255))
            gameover_text1_rect = gameover_text1.get_rect()
            gameover_text1_rect.left, gameover_text1_rect.top = \
                                 (width - gameover_text1_rect.width) // 2, height // 3
            screen.blit(gameover_text1, gameover_text1_rect)
            
            gameover_text2 = gameover_font.render(str(score), True, (255, 255, 255))
            gameover_text2_rect = gameover_text2.get_rect()
            gameover_text2_rect.left, gameover_text2_rect.top = \
                                 (width - gameover_text2_rect.width) // 2, \
                                 gameover_text1_rect.bottom + 10
            screen.blit(gameover_text2, gameover_text2_rect)

            again_rect.left, again_rect.top = \
                             (width - again_rect.width) // 2, \
                             gameover_text2_rect.bottom + 50
            screen.blit(again_image, again_rect)

            gameover_rect.left, gameover_rect.top = \
                                (width - again_rect.width) // 2, \
                                again_rect.bottom + 10
            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函数,重新开始游戏
                    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)
                    
                       
       
        #切换图片
        if not (delay % 5):
            switch_image = not switch_image
            
        delay -= 1
        if not delay:
            delay = 100


        pygame.display.flip()
        clock.tick(60)


if __name__ == "__main__":
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()   

本篇博客到这就完啦,非常感谢您的阅读🙏,如果对您有帮助,可以帮忙点个赞或者来波关注鼓励一下喔😬 ,嘿嘿👀

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!