hello!各位彦祖们!我们又见面了!!

今天兄弟我给大家带来了一款 Python 制作的经典游戏-全民飞机大战(狂暴版)

本项目案例涉及到的技术:

  1. Python 语法基础
  2. Python 面向对象
  3. Pygame 游戏引擎

是一个非常适合小白来加强以及巩固学习,提升编程思想,以及逻辑思维的项目案例!

我们来看看酷炫的界面项目效果(额......原谅我冲不起会员,无法转成高清的动图):

来一个高清的静图吧:

Python飞机大战源码是一个典型的基于Python及其pygame库实现的射击类游戏项目。下面将从项目概述、主要组成部分、核心功能实现以及开发流程等几个方面进行详细介绍。

一、项目概述

飞机大战游戏是一款经典的射击类游戏,玩家需要驾驶飞机在空中与敌人进行战斗,躲避敌人的攻击,并发射子弹消灭敌人。游戏背景通常设定在蓝天白云之间,通过键盘控制飞机的移动和射击,增加游戏的互动性和挑战性。

二、主要组成部分

  1. 游戏界面:包括背景、飞机、敌机、子弹等元素。背景采用蓝天白云的图片,飞机和敌机使用图像资源绘制在屏幕上,子弹则使用简单的矩形或图像表示。
  2. 游戏角色
    • 玩家飞机:玩家控制的飞机,可以移动和发射子弹。
    • 敌机:从屏幕上方不断飞来,玩家需要射击它们以获得分数。
    • 子弹:玩家飞机发射的子弹,用于击中敌机。
  3. 游戏逻辑:包括飞机的移动控制、子弹的发射和移动、敌机的生成和移动、碰撞检测等。

三、核心功能实现

  1. 初始化游戏
    • 使用pygame库创建游戏窗口,并设置窗口的大小和标题。
    • 加载游戏所需的图像资源,如背景图、飞机图、敌机图和子弹图等。
    • 初始化游戏变量和常量,如屏幕宽度、高度、飞机速度、敌机速度等。
  2. 绘制游戏元素
    • 在游戏窗口上绘制背景、飞机、敌机和子弹等元素。
    • 根据需要更新这些元素的位置和状态。
  3. 控制飞机移动
    • 使用pygame库中的键盘事件检测函数来检测玩家按下的键盘按键。
    • 根据按键更新飞机的位置,实现飞机的上下左右移动。
  4. 发射子弹
    • 当玩家按下空格键时,在飞机位置发射子弹。
    • 子弹以一定速度向上移动,直到离开屏幕或击中敌机。
  5. 生成敌机
    • 在游戏过程中,不断生成新的敌机从屏幕上方飞入。
    • 敌机的飞行速度和生成频率可以根据游戏难度进行调整。
  6. 碰撞检测
    • 检测子弹与敌机之间的碰撞。
    • 当子弹击中敌机时,敌机消失并增加玩家分数。
    • 检测敌机与玩家飞机之间的碰撞,如果发生碰撞则游戏结束。
  7. 游戏循环
    • 通过游戏主循环不断处理事件、绘制游戏元素、更新游戏状态等。
    • 在每次循环结束时更新屏幕显示。

四、开发流程

  1. 环境搭建:安装Python解释器和pygame库。
  2. 设计游戏界面:确定游戏背景、角色和元素的设计。
  3. 编写代码
    • 创建游戏窗口并设置窗口属性。
    • 加载游戏资源并初始化游戏变量。
    • 编写绘制游戏元素的代码。
    • 实现飞机移动、子弹发射和敌机生成的逻辑。
    • 编写碰撞检测和游戏结束的逻辑。
  4. 测试与优化
    • 对游戏进行测试,检查是否存在bug或性能问题。
    • 根据测试结果进行优化和改进,提高游戏的稳定性和流畅度。
  5. 发布与分享:将游戏打包并发布到相关平台,与玩家分享。

五、注意事项

  • 在开发过程中,需要注意代码的清晰性和可读性,以便后续维护和扩展。
  • 使用的图像和音效资源需要确保合法合规,避免侵犯版权。
  • 游戏难度和游戏体验可以通过不断迭代和优化来提升。

是不是很 nice,实际上这个游戏并不难,你只需要先这样,再这样,然后再那样就实现了!!

说点人话:先看看主程序 main1.py,主要是一些设置颜色,以及加载图片的代码

import pygame
from game_music import *
from main2 import *

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

bg_size = width, height = 512, 700
screen = pygame.display.set_mode(bg_size)

BLUCK = (0, 0, 0)

# 白色

bg = (255, 255, 255)

# 红色
RED = (255, 0, 0)

# 橙色
ORANGE = (255, 165, 0)

GREENS = (152, 102, 0)
# 绿色
GREEN = (0, 255, 0)


# 爆炸样式
blast_images = []
blast_images.extend([
    pygame.image.load(r'image\aircraft_image\baozai_1.png').convert_alpha(), \
    pygame.image.load(r'image\aircraft_image\baozai_2.png').convert_alpha(), \
    pygame.image.load(r'image\aircraft_image\baozai_3.png').convert_alpha(), \
    ])
blast_image = []
blast_image.extend([
    pygame.image.load(r'image\aircraft_image\baozai_2.png').convert_alpha(), \
    pygame.image.load(r'image\aircraft_image\baozai_3.png').convert_alpha(), \
    ])

# 我方飞机尾部喷气
plane_images = []
plane_images.extend([
    pygame.image.load(r'image\aircraft_image\myplane_2.png').convert_alpha(), \
    pygame.image.load(r'image\aircraft_image\myplane_2.png').convert_alpha(), \
 \
    pygame.image.load(r'image\aircraft_image\myplane_2.png').convert_alpha(), \
    pygame.image.load(r'image\aircraft_image\myplane_2.png').convert_alpha(), \
 \
    pygame.image.load(r'image\aircraft_image\myplane_2.png').convert_alpha(), \
    ])



if __name__ == "__main__":
    main()

在上面的代码中调用了 main() 函数,这个 main 函数的代码如下:

import pygame
import sys
import traceback
import myplane
import bullet
import enemy
import supply
from pygame.locals import *
from random import *
import background
import prop
import time
from game_music import *
from main1 import *

def bollet_lvs(bollet, enemies, my_frection, enemy_index, damage):
    for b in bollet:
        if b.action:
            b.move()
            screen.blit(b.image, b.rect)

            mybullet_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
            if mybullet_hit:
                bullet_sound.play()
                my_frection += 10

                screen.blit(blast_image[enemy_index], (b.rect.left - 20, b.rect.top))
                enemy_index = (enemy_index + 1) % 2
                if enemy_index == 0:

                    b.action = False
                    for each in mybullet_hit:
                        each.hit = True
                        each.energy -= damage
                        if each.energy <= 0:
                            each.action = False


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


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


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


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


def add_mid_enemies_n1(group1, group2, num):
    for i in range(num):
        e2 = enemy.MidEnemy_n1(bg_size)
        group1.add(e2)
        group2.add(e2)


def add_mid_enemies_n2(group1, group2, num):
    for i in range(num):
        e2 = enemy.MidEnemy_n2(bg_size)
        group1.add(e2)
        group2.add(e2)


def add_mid_enemies_n3(group1, group2, num):
    for i in range(num):
        e2 = enemy.MidEnemy_n3(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 blit(target, ground, position, number):
    x = position[0]
    y = position[1]
    temp = pygame.Surface((ground.get_width(), ground.get_height())).convert()
    temp.blit(target, (-x, -y))
    temp.blit(ground, (0, 0))
    temp.set_alpha(number)
    target.blit(temp, position)


def main():
    # 当前子弹等级
    bollet_lv = 50

    # 背景音乐参数
    music_choice = True
    music_num = 0
    music_nums = 1

    # 绘制全屏炸弹
    zd_num = 15
    # 读取了炸弹的图片
    zd_lcons = pygame.image.load(r'image\aircraft_image\zd_lcon.png')
    zd_lcon = pygame.image.load(r'image\aircraft_image\zd_lcon1.png')
    zd_lcon_1 = pygame.image.load(r'image\aircraft_image\zd_lcon2.png')

    zd_nums = pygame.font.Font('font/Marker Felt.ttf', 18)

    # 编辑血量HP
    HP = pygame.image.load(r'image\aircraft_image\HP.png')
    HP_num = pygame.font.Font('font/Interstate Mono - Blk.ttf', 10)

    # 创建背景图片对象
    # 循环刷新  把前面遗留的图片翻盖
    position = 0, height
    buss = []
    ball = background.Back(position, bg_size)
    ground = background.Ground(bg_size)
    buss.append(ball)
    # 我方吃了多少个星星
    myplane_prop = 0

    # 创建我方飞机对象
    me = myplane.Myplane(bg_size)

    enemies = pygame.sprite.Group()

    # 统计我方得分与行驶距离
    my_frection = 0
    my_Frection = pygame.font.Font('font/Legothick.ttf', 35)
    my_distance = 0
    my_dist = 1
    my_Distance = pygame.font.Font('font/Interstate Mono - Blk.ttf', 20)

    # 生成小型敌机
    small_enemies_n1 = pygame.sprite.Group()
    add_small_enemies_n1(small_enemies_n1, enemies, 5)

    small_enemies_n2 = pygame.sprite.Group()
    add_small_enemies_n2(small_enemies_n2, enemies, 5)

    small_enemies_n3 = pygame.sprite.Group()
    add_small_enemies_n3(small_enemies_n3, enemies, 5)
    small_enemies_n4 = pygame.sprite.Group()
    add_small_enemies_n4(small_enemies_n4, enemies, 5)

    # 生成敌方子弹
    bullets = []

    # 生成我方0级蓝色子弹
    mybullet_0 = []

    mybullet_index = 0
    mybullet_num = 20
    mybullet_nums = mybullet_num
    for i in range(mybullet_num):
        mybullet_0.append(bullet.Mybullet((me.rect.midtop)))

    # 生成我方红色级子弹
    mybullet_1 = []
    mybullet_index1 = 0

    for i in range(mybullet_nums):
        mybullet_1.append(bullet.Mybullet1((me.rect.midtop[0] - 10, me.rect.midtop[1])))
        mybullet_1.append(bullet.Mybullet1((me.rect.midtop[0] + 10, me.rect.midtop[1])))
    # 生成我方红色级子弹子弹
    mybullet_2 = []
    mybullet_index2 = 0

    for i in range(mybullet_nums):
        mybullet_2.append(bullet.Mybullet1((me.rect.midtop[0] - 40, me.rect.midtop[1] + 10)))
        mybullet_2.append(bullet.Mybullet1((me.rect.midtop[0] + 40, me.rect.midtop[1] + 10)))
    # 生成我方红色子弹子弹
    mybullet_3 = []
    mybullet_index3 = 0

    for i in range(mybullet_nums):
        mybullet_3.append(bullet.Mybullet1((me.rect.midtop[0] - 20, me.rect.midtop[1] + 10)))
        mybullet_3.append(bullet.Mybullet1((me.rect.midtop[0] + 20, me.rect.midtop[1] + 10)))
    # 生成我方蓝色子弹
    mybullet_01 = []

    mybullet_index01 = 0
    for i in range(mybullet_num):
        mybullet_01.append(bullet.Mybullet((me.rect.midtop[0] - 10, me.rect.midtop[1])))
        mybullet_01.append(bullet.Mybullet((me.rect.midtop[0] + 10, me.rect.midtop[1])))
    # 生成我方蓝色子弹

    mybullet_02 = []

    mybullet_index02 = 0
    for i in range(mybullet_num):
        mybullet_02.append(bullet.Mybullet((me.rect.midtop[0] - 10, me.rect.midtop[1])))
        mybullet_02.append(bullet.Mybullet((me.rect.midtop[0] + 10, me.rect.midtop[1])))

    # 生成我方紫色色子弹子弹
    mybullet_4 = []
    mybullet_index4 = 0

    for i in range(mybullet_num):
        mybullet_4.append(bullet.Mybullet2((me.rect.midtop)))

    # 生成中型敌机
    mid_enemies_n1 = pygame.sprite.Group()
    add_mid_enemies_n1(mid_enemies_n1, enemies, 1)

    mid_enemies_n2 = pygame.sprite.Group()
    add_mid_enemies_n2(mid_enemies_n2, enemies, 1)

    mid_enemies_n3 = pygame.sprite.Group()
    add_mid_enemies_n3(mid_enemies_n3, enemies, 1)

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

    # 中弹图片索引
    me_index = 0
    small_index_1 = 0
    small_index_2 = 0
    small_index_3 = 0
    small_index_4 = 0
    mid_index_1 = 0
    mid_index_2 = 0
    mid_index_3 = 0
    big_index = 0

    # 用于切换我方飞机螺旋桨图片
    switch_image = True

    # 生成飞机喷气索引
    me_indexs = 0

    # 敌方中弹图片索引
    enemy_index = 0

    # 道具的集合
    prop_xx = []
    prop_bollet_s = []
    xx_num = 0

    # 统计星星图标与数字
    prop_num = pygame.font.Font('font/Interstate Mono - Blk.ttf', 25)

    prop_lcon = pygame.image.load(r'image\aircraft_image\prop_lcon.png')

    clock = pygame.time.Clock()

    running = True

    delay = 100

    while running:
        # 背景音乐音效
        if music_choice and music_nums:
            music[music_num].play()
            music_nums = 0
        if not pygame.mixer.get_busy():
            music_nums = 1

            music_choice = True
            music_num = (music_num + 1) % 3
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            # 按下空格释放全屏炸弹
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    zd_num -= 1
                    if zd_num > 0:
                        for each in enemies:
                            if each.rect.bottom > 0:
                                each.action = False

        # 绘制背景
        for each in buss:
            each.move()
            if each.rect.top > height:
                buss.remove(each)
                each.action = False

            if my_dist < 10:
                screen.blit(each.image, each.rect)
            if 10 < my_dist < 20:
                screen.blit(each.images[0], each.rect)
            if 20 < my_dist < 30:
                screen.blit(each.images[1], each.rect)
            if 30 < my_dist < 40:
                screen.blit(each.images[2], each.rect)
            if 40 < my_dist < 50:
                screen.blit(each.images[3], each.rect)
            if 50 < my_dist < 60:
                screen.blit(each.images[4], each.rect)
            if 160 < my_dist < 70:
                screen.blit(each.images[5], each.rect)

        if each.action:
            position = each.rect.left, each.rect.top
            speed = [0, 1]
            ball = background.Back(position, bg_size)
            buss.append(ball)
        # 云朵的生成
        blit(screen, ground.image_s, ground.image_rect, 170)
        ground.move()

        # 检测用户的键盘操作,Key_pressed是一个序列包含了键盘上所有布尔类型的值
        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()

        # 绘制大型敌机
        for each in big_enemies:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left, each.rect.top - 5), \
                                     (each.rect.left + each.rect.width, \
                                      each.rect.top - 5), \
                                     5)
                    # 目前血量
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left, each.rect.top - 5), \
                                     (each.rect.left + each.rect.width * energy_remain, \
                                      each.rect.top - 5), \
                                     5)





            else:
                my_frection += 1000
                if not (delay % 2):

                    screen.blit(blast_images[big_index], each.rect)
                    screen.blit(blast_images[big_index], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[big_index], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[big_index], (each.rect.left + 30, each.rect.bottom - 100))

                    big_index = (big_index + 1) % 3
                    if big_index == 0:
                        my_frection += 1000

                        xx_num = randint(3, 7)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        # 绘制中型敌机
        for each in mid_enemies_n1:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom + 5), \
                                     (each.rect.left + each.rect.width - 70, \
                                      each.rect.bottom + 5), \
                                     3)
                    # 目前血量
                    energy_remain = each.energy / enemy.MidEnemy_n3.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom + 5), \
                                     (each.rect.left + 45 + (each.rect.width - 140) * energy_remain, \
                                      each.rect.bottom + 5), \
                                     3)
            else:

                if not (delay % 2):

                    screen.blit(blast_images[mid_index_1], each.rect)
                    screen.blit(blast_images[mid_index_1], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[mid_index_1], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[mid_index_1], (each.rect.left + 30, each.rect.bottom - 100))

                    mid_index_1 = (mid_index_1 + 1) % 3
                    if mid_index_1 == 0:
                        my_frection += 500

                        xx_num = randint(2, 5)
                        bollet_num = choice([True, False])
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)
                        if bollet_num:
                            prop_bollet = prop.Prop_bollet(each.rect.midtop)
                            prop_bollet_s.append(prop_bollet)

                        each.reset()

        for each in mid_enemies_n2:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom + 5), \
                                     (each.rect.left + each.rect.width - 50, \
                                      each.rect.bottom + 5), \
                                     3)
                    # 目前血量
                    energy_remain = each.energy / enemy.MidEnemy_n3.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom + 5), \
                                     (each.rect.left + 45 + (each.rect.width - 100) * energy_remain, \
                                      each.rect.bottom + 5), \
                                     3)


            else:
                if not (delay % 6):

                    screen.blit(blast_images[mid_index_2], each.rect)
                    screen.blit(blast_images[mid_index_2], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[mid_index_2], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[mid_index_2], (each.rect.left + 30, each.rect.bottom - 100))

                    mid_index_2 = (mid_index_2 + 1) % 3
                    if mid_index_2 == 0:
                        my_frection += 500

                        xx_num = randint(2, 5)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        for each in mid_enemies_n3:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                screen.blit(each.images, (each.rect.left - 44, each.rect[1] + 6))
                screen.blit(each.images, (each.rect.right - 86, each.rect[1] + 6))

                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom + 5), \
                                     (each.rect.left + each.rect.width - 45, \
                                      each.rect.bottom + 5), \
                                     3)
                    # 目前血量
                    energy_remain = each.energy / enemy.MidEnemy_n3.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom + 5), \
                                     (each.rect.left + 45 + (each.rect.width - 90) * energy_remain, \
                                      each.rect.bottom + 5), \
                                     3)
                if each.rect.bottom == 0 or each.rect.bottom == 1:
                    mid_enemies_n3_sound.play()










            else:
                if not (delay % 2):

                    screen.blit(blast_images[mid_index_3], each.rect)
                    screen.blit(blast_images[mid_index_3], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[mid_index_3], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[mid_index_3], (each.rect.left + 30, each.rect.bottom - 100))

                    mid_index_3 = (mid_index_3 + 1) % 3
                    if mid_index_3 == 0:
                        my_frection += 500
                        xx_num = randint(2, 5)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        # 绘制小型敌机
        for each in small_enemies_n1:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width - 50, \
                                      each.rect.bottom), \
                                     2)
                    # 目前血量
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width * energy_remain + 50, \
                                      each.rect.bottom), \
                                     2)
                if each.bullet:
                    # 小型敌机形成子弹
                    small_bullet = bullet.Small_n1s(each.rect.midbottom, bg_size)
                    bullets.append(small_bullet)




            else:
                # 毁灭

                if not (delay % 2):

                    screen.blit(blast_images[small_index_1], each.rect)
                    screen.blit(blast_images[small_index_1], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_1], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_1], (each.rect.left + 30, each.rect.bottom - 100))

                    small_index_1 = (small_index_1 + 1) % 3

                    if small_index_1 == 0:
                        my_frection += 150

                        xx_num = randint(0, 3)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        # 小型敌机发射子弹
        for b in bullets:
            if b.action:
                b.move()

                screen.blit(b.image, b.rect)
                # 检测我方飞机是否中弹
                enemy_hit = pygame.sprite.spritecollide(me, bullets, False, pygame.sprite.collide_mask)
                if enemy_hit:
                    b.action = False
                    if me.output > 0:
                        me.output -= 10

                    else:
                        me.action = False

        for each in small_enemies_n2:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width - 50, \
                                      each.rect.bottom), \
                                     2)
                    # 目前血量
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width * energy_remain + 50, \
                                      each.rect.bottom), \
                                     2)
            else:
                # 毁灭

                if not (delay % 3):

                    screen.blit(blast_images[small_index_2], each.rect)
                    screen.blit(blast_images[small_index_2], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_2], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_2], (each.rect.left + 30, each.rect.bottom - 100))

                    small_index_2 = (small_index_2 + 1) % 3

                    if small_index_2 == 0:
                        my_frection += 150

                        xx_num = randint(0, 3)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        for each in small_enemies_n3:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width - 50, \
                                      each.rect.bottom), \
                                     2)
                    # 目前血量
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width * energy_remain + 50, \
                                      each.rect.bottom), \
                                     2)
            else:
                # 毁灭
                if not (delay % 4):

                    screen.blit(blast_images[small_index_3], each.rect)
                    screen.blit(blast_images[small_index_3], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_3], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_3], (each.rect.left + 30, each.rect.bottom - 100))

                    small_index_3 = (small_index_3 + 1) % 3
                    if small_index_3 == 0:
                        my_frection += 150

                        xx_num = randint(0, 3)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        for each in small_enemies_n4:
            if each.action:
                each.move()
                screen.blit(each.image, each.rect)
                if each.hit:
                    # 绘制显示血槽
                    pygame.draw.line(screen, RED, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width - 50, \
                                      each.rect.bottom), \
                                     2)
                    # 目前血量
                    energy_remain = each.energy / enemy.BigEnemy.energy
                    energy_color = GREEN
                    pygame.draw.line(screen, energy_color, (each.rect.left + 45, each.rect.bottom), \
                                     (each.rect.left + each.rect.width * energy_remain + 50, \
                                      each.rect.bottom), \
                                     2)
            else:
                # 毁灭
                if not (delay % 5):

                    screen.blit(blast_images[small_index_4], each.rect)
                    screen.blit(blast_images[small_index_4], (each.rect.left, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_4], (each.rect.left + 30, each.rect.bottom - 45))
                    screen.blit(blast_images[small_index_4], (each.rect.left + 30, each.rect.bottom - 100))

                    small_index_4 = (small_index_4 + 1) % 3
                    if small_index_4 == 0:
                        my_frection += 150

                        xx_num = randint(0, 3)
                        for i in range(xx_num):
                            prop_xxs = prop.Prop_xx(each.rect.midtop)
                            each.rect.left -= 30

                            prop_xx.append(prop_xxs)

                        each.reset()

        # 检测我方飞机是否被撞
        enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
        if enemies_down:
            me.action = False
            for e in enemies_down:
                e.action = False
                e.hit = True

        # 生成我方子弹

        if not (delay % 6):
            if bollet_lv < 4:

                # 0级子弹
                mybullet_0[mybullet_index].reset((me.rect.midtop))
                mybullet_index = (mybullet_index + 1) % mybullet_num
                # 1级子弹
                if bollet_lv >= 1:
                    mybullet_1[mybullet_index1].reset((me.rect.midtop[0] - 20, me.rect.midtop[1]))

                    mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] + 20, me.rect.midtop[1]))
                    mybullet_index1 = (mybullet_index1 + 2) % mybullet_num

                # 2级级子弹
                if bollet_lv >= 2:
                    mybullet_2[mybullet_index2].reset((me.rect.midtop[0] - 40, me.rect.midtop[1] + 15))
                    mybullet_2[mybullet_index2 + 1].reset((me.rect.midtop[0] + 40, me.rect.midtop[1] + 15))
                    mybullet_index2 = (mybullet_index2 + 2) % mybullet_num
                # 3级级子弹
                if bollet_lv >= 3:
                    mybullet_3[mybullet_index3].reset((me.rect.midtop[0] - 30, me.rect.midtop[1] + 10))
                    mybullet_3[mybullet_index3 + 1].reset((me.rect.midtop[0] + 30, me.rect.midtop[1] + 10))
                    mybullet_index3 = (mybullet_index3 + 2) % mybullet_num
            if bollet_lv >= 4 and bollet_lv < 8:
                # 4级子弹
                if bollet_lv >= 4:
                    mybullet_0[mybullet_index].reset((me.rect.midtop[0] - 10, me.rect.midtop[1]))
                    mybullet_0[mybullet_index + 1].reset((me.rect.midtop[0] + 10, me.rect.midtop[1]))

                    mybullet_index = (mybullet_index + 2) % mybullet_num

                # 5级子弹
                if bollet_lv >= 5:
                    mybullet_1[mybullet_index1].reset((me.rect.midtop[0] - 25, me.rect.midtop[1]))

                    mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] + 25, me.rect.midtop[1]))
                    mybullet_index1 = (mybullet_index1 + 2) % mybullet_num
                # 6级子弹
                if bollet_lv >= 6:
                    mybullet_2[mybullet_index2].reset((me.rect.midtop[0] - 40, me.rect.midtop[1] + 10))

                    mybullet_2[mybullet_index2 + 1].reset((me.rect.midtop[0] + 40, me.rect.midtop[1] + 10))
                    mybullet_index2 = (mybullet_index2 + 2) % mybullet_num
                # 7级子弹
                if bollet_lv >= 7:
                    mybullet_3[mybullet_index3].reset((me.rect.midtop[0] - 33, me.rect.midtop[1] + 5))

                    mybullet_3[mybullet_index3 + 1].reset((me.rect.midtop[0] + 33, me.rect.midtop[1] + 5))
                    mybullet_index3 = (mybullet_index3 + 2) % mybullet_num
            if bollet_lv >= 8 and bollet_lv <= 10:
                # 8级子弹
                if bollet_lv >= 8:
                    mybullet_0[mybullet_index].reset((me.rect.midtop))
                    mybullet_index = (mybullet_index + 1) % mybullet_num
                    mybullet_02[mybullet_index02 + 1].reset((me.rect.midtop[0] - 30, me.rect.midtop[1] + 10))
                    mybullet_02[mybullet_index02 + 2].reset((me.rect.midtop[0] + 30, me.rect.midtop[1] + 10))
                    mybullet_index02 = (mybullet_index02 + 3) % mybullet_num

                # 9级子弹
                if bollet_lv >= 9:
                    mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] - 50, me.rect.midtop[1] + 10))
                    mybullet_1[mybullet_index1 + 2].reset((me.rect.midtop[0] + 50, me.rect.midtop[1] + 10))

                    mybullet_index1 = (mybullet_index1 + 2) % mybullet_nums
                # 10级子弹
                if bollet_lv >= 10:
                    mybullet_2[mybullet_index2 + 0].reset((me.rect.midtop[0] - 15, me.rect.midtop[1] + 10))
                    mybullet_2[mybullet_index2 + 1].reset((me.rect.midtop[0] + 15, me.rect.midtop[1] + 10))

                    mybullet_index2 = (mybullet_index2 + 2) % mybullet_nums
            if bollet_lv >= 11 and bollet_lv < 14:
                if bollet_lv == 11:
                    mybullet_02[mybullet_index02 + 0].reset((me.rect.midtop[0] - 15, me.rect.midtop[1]))
                    mybullet_02[mybullet_index02 + 1].reset((me.rect.midtop[0] + 15, me.rect.midtop[1]))
                    mybullet_index02 = (mybullet_index02 + 2) % mybullet_num

                    mybullet_01[mybullet_index01 + 0].reset((me.rect.midtop[0] - 43, me.rect.midtop[1] + 20))
                    mybullet_01[mybullet_index01 + 1].reset((me.rect.midtop[0] + 45, me.rect.midtop[1] + 20))
                    mybullet_index01 = (mybullet_index01 + 2) % mybullet_num

                if bollet_lv == 12:
                    mybullet_01[mybullet_index01 + 0].reset((me.rect.midtop[0] - 10, me.rect.midtop[1] + 20))
                    mybullet_01[mybullet_index01 + 1].reset((me.rect.midtop[0] + 10, me.rect.midtop[1] + 20))
                    mybullet_index01 = (mybullet_index01 + 2) % mybullet_num

                    mybullet_02[mybullet_index02 + 0].reset((me.rect.midtop[0] - 30, me.rect.midtop[1]))
                    mybullet_02[mybullet_index02 + 1].reset((me.rect.midtop[0] + 30, me.rect.midtop[1]))
                    mybullet_index02 = (mybullet_index02 + 2) % mybullet_num
                    mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] - 43, me.rect.midtop[1] + 20))
                    mybullet_1[mybullet_index1 + 2].reset((me.rect.midtop[0] + 45, me.rect.midtop[1] + 20))
                    mybullet_index1 = (mybullet_index1 + 2) % mybullet_nums

                if bollet_lv == 13:
                    mybullet_0[mybullet_index + 0].reset((me.rect.midtop[0] - 25, me.rect.midtop[1]))
                    mybullet_0[mybullet_index + 1].reset((me.rect.midtop[0] + 25, me.rect.midtop[1]))
                    mybullet_0[mybullet_index + 2].reset((me.rect.midtop[0] - 40, me.rect.midtop[1]))
                    mybullet_0[mybullet_index + 3].reset((me.rect.midtop[0] + 40, me.rect.midtop[1]))
                    mybullet_index = (mybullet_index + 4) % mybullet_num

                    mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] - 55, me.rect.midtop[1] + 10))
                    mybullet_1[mybullet_index1 + 2].reset((me.rect.midtop[0] + 55, me.rect.midtop[1] + 10))
                    mybullet_index1 = (mybullet_index1 + 2) % mybullet_nums

                    mybullet_2[mybullet_index2 + 1].reset((me.rect.midtop[0] - 10, me.rect.midtop[1] + 10))
                    mybullet_2[mybullet_index2 + 2].reset((me.rect.midtop[0] + 10, me.rect.midtop[1] + 10))
                    mybullet_index2 = (mybullet_index2 + 2) % mybullet_nums
            if bollet_lv >= 14 and bollet_lv < 19:
                # 14级子弹
                mybullet_4[mybullet_index4].reset((me.rect.midtop))
                mybullet_index4 = (mybullet_index4 + 1) % mybullet_num
                if 14 <= bollet_lv <= 15 or bollet_lv >= 17:
                    mybullet_01[mybullet_index01 + 0].reset((me.rect.midtop[0] - 25, me.rect.midtop[1] + 20))
                    mybullet_01[mybullet_index01 + 1].reset((me.rect.midtop[0] + 25, me.rect.midtop[1] + 20))
                    mybullet_index01 = (mybullet_index01 + 2) % mybullet_num
                # 15级子弹
                if 15 <= bollet_lv <= 16 or bollet_lv == 18:
                    if bollet_lv == 15:
                        mybullet_1[mybullet_index1 + 0].reset((me.rect.midtop[0] - 43, me.rect.midtop[1] + 30))
                        mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] + 45, me.rect.midtop[1] + 30))

                        mybullet_index1 = (mybullet_index1 + 2) % mybullet_nums
                    else:
                        mybullet_1[mybullet_index1 + 0].reset((me.rect.midtop[0] - 55, me.rect.midtop[1] + 30))
                        mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] + 55, me.rect.midtop[1] + 30))

                        mybullet_index1 = (mybullet_index1 + 2) % mybullet_nums

                # 16级子弹
                if bollet_lv >= 16 and bollet_lv <= 18:
                    mybullet_02[mybullet_index02 + 0].reset((me.rect.midtop[0] - 43, me.rect.midtop[1] + 20))
                    mybullet_02[mybullet_index02 + 1].reset((me.rect.midtop[0] + 45, me.rect.midtop[1] + 20))
                    mybullet_index02 = (mybullet_index02 + 2) % mybullet_nums
                if bollet_lv >= 16 and bollet_lv < 17:
                    mybullet_3[mybullet_index3 + 0].reset((me.rect.midtop[0] - 25, me.rect.midtop[1] + 30))
                    mybullet_3[mybullet_index3 + 1].reset((me.rect.midtop[0] + 27, me.rect.midtop[1] + 30))

                    mybullet_index3 = (mybullet_index3 + 2) % mybullet_nums
            if bollet_lv >= 19:

                mybullet_4[mybullet_index4].reset((me.rect.midtop[0] - 15, me.rect.midtop[1]))
                mybullet_4[mybullet_index4 + 1].reset((me.rect.midtop[0] + 15, me.rect.midtop[1]))
                mybullet_index4 = (mybullet_index4 + 2) % mybullet_num
                mybullet_01[mybullet_index01 + 0].reset((me.rect.midtop[0] - 40, me.rect.midtop[1] + 40))
                mybullet_01[mybullet_index01 + 1].reset((me.rect.midtop[0] + 40, me.rect.midtop[1] + 40))
                mybullet_index01 = (mybullet_index01 + 2) % mybullet_nums
                if bollet_lv >= 20:
                    mybullet_1[mybullet_index1].reset((me.rect.midtop[0] - 55, me.rect.midtop[1] + 30))
                    mybullet_1[mybullet_index1 + 1].reset((me.rect.midtop[0] + 55, me.rect.midtop[1] + 30))

                    mybullet_index1 = (mybullet_index1 + 2) % mybullet_nums

        if bollet_lv < 4:
            # 发射0级子弹
            bollet_lvs(mybullet_0, enemies, my_frection, enemy_index, 2)

            # 发射1级子弹
            if bollet_lv >= 1:
                bollet_lvs(mybullet_1, enemies, my_frection, enemy_index, 1)

            # 发射2级子弹
            if bollet_lv >= 2:
                bollet_lvs(mybullet_2, enemies, my_frection, enemy_index, 1)
            # 发射3级子弹
            if bollet_lv >= 3:
                bollet_lvs(mybullet_3, enemies, my_frection, enemy_index, 1)

        elif bollet_lv >= 4 and bollet_lv < 8:
            # 发射3级子弹
            bollet_lvs(mybullet_0, enemies, my_frection, enemy_index, 5)
            if bollet_lv >= 5:
                bollet_lvs(mybullet_1, enemies, my_frection, enemy_index, 1)
            if bollet_lv >= 6:
                bollet_lvs(mybullet_2, enemies, my_frection, enemy_index, 1)
            if bollet_lv >= 7:
                bollet_lvs(mybullet_3, enemies, my_frection, enemy_index, 1)
        elif bollet_lv >= 8 and bollet_lv < 11:
            bollet_lvs(mybullet_0, enemies, my_frection, enemy_index, 6)
            bollet_lvs(mybullet_02, enemies, my_frection, enemy_index, 1)

            if bollet_lv >= 9:
                bollet_lvs(mybullet_1, enemies, my_frection, enemy_index, 1)

            if bollet_lv >= 10:
                bollet_lvs(mybullet_2, enemies, my_frection, enemy_index, 1)
        elif bollet_lv >= 11:
            if bollet_lv == 11:
                bollet_lvs(mybullet_01, enemies, my_frection, enemy_index, 6)

                bollet_lvs(mybullet_02, enemies, my_frection, enemy_index, 6)

            if bollet_lv == 12:
                bollet_lvs(mybullet_01, enemies, my_frection, enemy_index, 6)
                bollet_lvs(mybullet_02, enemies, my_frection, enemy_index, 6)

                bollet_lvs(mybullet_1, enemies, my_frection, enemy_index, 2)
            if bollet_lv >= 13:
                bollet_lvs(mybullet_0, enemies, my_frection, enemy_index, 6)
                bollet_lvs(mybullet_1, enemies, my_frection, enemy_index, 2)
                bollet_lvs(mybullet_2, enemies, my_frection, enemy_index, 2)
        if bollet_lv >= 14 and bollet_lv < 19:

            bollet_lvs(mybullet_4, enemies, my_frection, enemy_index, 14)
            if bollet_lv >= 14:
                bollet_lvs(mybullet_01, enemies, my_frection, enemy_index, 8)

            if bollet_lv >= 15:
                bollet_lvs(mybullet_2, enemies, my_frection, enemy_index, 2)
            if bollet_lv >= 16:
                bollet_lvs(mybullet_3, enemies, my_frection, enemy_index, 2)
                bollet_lvs(mybullet_02, enemies, my_frection, enemy_index, 8)

        if bollet_lv >= 19:
            bollet_lvs(mybullet_4, enemies, my_frection, enemy_index, 14)
            bollet_lvs(mybullet_01, enemies, my_frection, enemy_index, 8)
            if bollet_lv >= 20:
                bollet_lvs(mybullet_2, enemies, my_frection, enemy_index, 4)

        delay -= 1
        if not delay:
            delay = 100
        # 绘制我方飞机
        if me.output > 0 and me.action:

            if bollet_lv < 5:
                screen.blit(me.image, me.rect)
                screen.blit(plane_images[me_indexs], (me.rect.left - 5, me.rect.bottom - 20))

            elif 5 < bollet_lv <= 10:
                screen.blit(me.image1, me.rect)
                screen.blit(plane_images[me_indexs], (me.rect.left - 5, me.rect.bottom - 20))

            else:

                screen.blit(me.image2, me.rect)
                screen.blit(plane_images[me_indexs], (me.rect.left - 5, me.rect.bottom + 10))

            me_indexs = (me_indexs + 1) % 5
            # 绘制血条
            pygame.draw.rect(screen, GREENS, (24, 10, 100, 12), 1)
            pygame.draw.line(screen, BLUCK, (25, 15), (122, 15), 10)

            output_remain = me.output / myplane.Myplane.output
            output_color = RED
            pygame.draw.line(screen, output_color, (25, 15), (25 + 97 * output_remain, 15), 10)

            HP_NUM = HP_num.render(str(me.output), True, bg)
            screen.blit(HP, (5, 5))
            screen.blit(HP_NUM, (60, 11))

        else:
            # 毁灭
            if not (delay % 6):
                screen.blit(blast_images[me_index], me.rect)
                screen.blit(blast_images[me_index], (me.rect.left, me.rect.bottom - 45))
                screen.blit(blast_images[me_index], (me.rect.left + 30, me.rect.bottom - 45))
                screen.blit(blast_images[me_index], (me.rect.left + 30, me.rect.bottom - 100))
                me_index = (me_index + 1) % 3
            if me_index == 0:
                me.reset()
        # 星星的移动与是否与我方敌机碰撞
        # 星星图标
        Prop_num = prop_num.render(str(myplane_prop), True, bg)
        Prop_num_rect = Prop_num.get_rect()
        Prop_num_rect.right, Prop_num_rect.top = width - 10, 60

        screen.blit(Prop_num, Prop_num_rect)
        screen.blit(prop_lcon, (Prop_num_rect.left - 40, 50))

        for each in prop_xx:

            each.move()
            xx_hit = pygame.sprite.collide_mask(each, me)

            if each.action:
                screen.blit(each.xx_image[each.num], each.rect)

                if not (delay % 5):
                    each.num = (each.num + 1) % 8
                if each.num == 0:
                    screen.blit(each.image, each.rect)

            if xx_hit and each.action:
                prop_sound.play()
                each.action = False
                myplane_prop += 1
        # 子弹道具
        for each in prop_bollet_s:
            each.move()
            bollet1_hit = pygame.sprite.collide_mask(each, me)
            if each.action:
                screen.blit(each.image, each.rect)
                if bollet1_hit:
                    prop_sound.play()
                    each.action = False
                    bollet_lv += 1
                    print(bollet_lv)

        # 绘制分数与行驶距离:
        my_frections = my_Frection.render(str(my_frection), True, ORANGE)
        my_frections_rect = my_frections.get_rect()
        my_frections_rect.right, my_frections_rect.top = width - 40, 10
        screen.blit(my_frections, my_frections_rect)

        if my_distance < 1000:
            my_distance += 1
            my_distances = my_Distance.render(str(my_distance) + '\n' + 'm', True, ORANGE)
        else:
            my_dist += 0.01
            my_distances = my_Distance.render(str('%.2f' % my_dist) + '\n' + 'Km', True, ORANGE)

        my_distances_rect = my_distances.get_rect()
        my_distances_rect.right, my_distances_rect.top = width - 20, 100
        screen.blit(my_distances, my_distances_rect)

        # 全屏炸弹的绘制
        screen.blit(zd_lcons, (5, 400))
        Zd_nums = zd_nums.render('x' + str(zd_num), True, bg)

        if zd_num > 0:
            screen.blit(zd_lcon, (15, 414))

            screen.blit(Zd_nums, (49, 423))

        # 切换图片
        if not (delay % 5):
            switch_image = not switch_image

        my_frection += 1

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

源代码获取方式:

https://download.csdn.net/download/yueyehuguang/89774634?spm=1001.2014.3001.5503

没有积分的同学可以:关注我,然后私信我免费获取、

代码较多,难以理解?这里推荐一个视频教程可以看看:

【墨轩】Python游戏从0基础到实战【课设】

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐