飞机大战课程设计报告

飞机大战游戏的设计与开发

摘要:在未来的世界里,地球已经不能使人类居住下去,于是人们开始向月球移民,在移民过程中,发现了比我们快一步的生物居住在那,双方都行驶战斗机开始了一场激烈的战斗,多次战斗无果,势均力敌......协商后我们派出3台己方战斗机与敌机开战,这次的游戏不光光是个人荣辱的游戏,更是我们占领月球的一次关键决定,请用最珍贵的三条己方飞机寿命攻击敌机,获得相应的分数,时间越久难度越大,游戏中也会有空投的补给包,控制好自己手中的飞机,与身边的伙伴来进行分数的比拼吧!

关键词:Python;Pygame;飞机大战游戏

需求分析

通过对武汉软件工程学院计算机学院的日常休闲娱乐生活进行调研、分析了解到同学们对得分比拼类游戏非常感兴趣,结合未来世界人类居住的星球不在只是地球,可能有月球,甚至更多。我们便结合了大众需求,研发了这款名叫“飞机大战”的游戏,游戏画风清新可爱,易懂易上手,得分情况清晰可见,更容易使玩家在游戏中体验感足,具有较强的新鲜感。

1.1飞机大战游戏的功能分析

飞机大战游戏的主要功能是:

玩家进入游戏,即可体验就开始进行游戏

玩家可通过↑↓←→键来操控己方飞机移动,灵敏的躲过敌方的攻击,并利用自动发出的子弹攻击敌方。

游戏左上角有实时的得分情况,摧毁敌机越多,得分越高。

右上角有一个暂停按键,方便随时暂停,得分依然还在。

右下角有三条生命,触碰到敌机,便会丢失一条生命,全部丢失之后,游戏结束,需重新的挑战游戏。

1.2飞机大战游戏的功能图

根据以上需求分析,飞机大战游戏功能如图1-1所示。

图1-1

概要设计

2.1游戏主窗口设计

本游戏为宽400,长700的页面,游戏最上方有游戏名称“飞机大战”显示,右上角会有一个X键用来关闭游戏,也有暂停键,可随时暂停游戏,游戏背景选用清新风的月球图案。

详细功能设计及代码实现

3.1开发环境安装

本系统是以Windows系统为操作平台,用Python编程语言来实现本系统所需功能,本机配置如下:

处理器:Intel(R) Core(TM) i5-11300H

主频:3.11 GHz

内存:16.0 GB

硬盘:512G

编程语言:Python

编程环境:Window10系统

编程软件:Pycharm 2021.3.2

3.2准备基本工作

#会调用的函数

import pygame

import sys

import traceback

from pygame.locals import *

import myplane

import enemy

import bullet

import supply

import random

3.3 页面设计

pygame.init()

bg_size = width, height = 400, 700

#设置页面的为宽400,长700的大小

screen = pygame.display.set_mode(bg_size)

#调用窗口界面

pygame.display.set_caption("飞机大战")

#设置游戏最上方名称“飞机大战”的显示

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

#游戏背景调用的图片

BLACK = (0, 0, 0)

GREEN = (0, 255, 0)

RED = (255, 0, 0)

WHITE = (255, 255, 255)

#设置血条的颜色

3.4音乐设计

#调用音乐文件

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.2)

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 main():

global each

#声明一个全局变量:each

pygame.mixer.music.play(-1)

#播放音乐,-1代表无线循环

clock = pygame.time.Clock()

#设置时钟变量,控制游戏执行频率

3.5 飞机设计

#有三种小中大类型的敌机

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):

e1 = enemy.MidEnemy(bg_size)

group1.add(e1)

group2.add(e1)

def add_big_enemies(group1, group2, num):

for i in range(num):

e1 = enemy.BigEnemy(bg_size)

group1.add(e1)

group2.add(e1)

# 绘制大型敌机,利用全局变量each

for each in big_enemies:

if each.active:

each.move()

if each.hit:

screen.blit(each.image_hit, each.rect)

#即获取矩形的方法就是函数get_rect()

each.hit = False

else:

if switch_image:

screen.blit(each.image1, each.rect)

else:

screen.blit(each.image2, each.rect)

if each.rect.bottom == -50:

enemy3_fly_sound.play(-1)

# 绘制血槽

pygame.draw.line(screen, BLACK,

(each.rect.left, each.rect.top - 5),

(each.rect.right, each.rect.top - 5),

2)

energy_remain = each.energy / enemy.BigEnemy.energy

if energy_remain > 0.2:

energy_color = GREEN

else:

energy_color = RED

#生命大于20%显示绿色,否则就显示红色

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),2)

else:

if not (delay % 3):

#当到了第3秒的时候

if e3_destroy_index == 0:

enemy3_down_sound.play()

screen.blit(each.destroy_images[e3_destroy_index], each.rect)

# destroy 只是终止主循环并删除所有小部件

e3_destroy_index = (e3_destroy_index + 1) % 6

if e3_destroy_index == 0:

me_down_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.image1, each.rect)

# 绘制血槽

pygame.draw.line(screen, BLACK,

(each.rect.left, each.rect.top - 5),

(each.rect.right, each.rect.top - 5),2)

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 + 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 += 5000

each.reset()

# 绘制小型敌机

for each in small_enemies:

if each.active:

each.move()

screen.blit(each.image1, 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()

#改变飞机坐标

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()

#改变飞机的坐标

#创建速度方法

def inc_speed(target, inc):

for each in target:

each.speed += inc

me = myplane.MyPlane(bg_size)

#生成己方飞机

enemies = pygame.sprite.Group()

#讲敌机放进enemies的精灵组里面

# 生成敌方小型飞机

small_enemies = pygame.sprite.Group()

add_small_enemies(small_enemies, enemies, 15)

#添加小型飞机数量15

mid_enemies = pygame.sprite.Group()

add_mid_enemies(mid_enemies, enemies, 5)

#添加中型的飞机数量为5

big_enemies = pygame.sprite.Group()

add_big_enemies(big_enemies, enemies, 1)

#添加大型飞机数量为1

# 检测我方飞机是否被撞

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:

me_down_sound.play()

if not (delay % 3):

screen.blit(each.destroy_images[me_destroy_index], each.rect)

me_destroy_index = (me_destroy_index + 1) % 4

# 剩余生命数量

if me_destroy_index == 0:

life_num -= 1

me.reset()

pygame.time.set_timer(INVINCEBLE_TIME, 3 * 1000)

#第一个参数传入这个自定义的事件,设定30秒,单位是1000毫秒

3.6子弹设计

# 中弹图片索引

e1_destroy_index = 0

e2_destroy_index = 0

e3_destroy_index = 0

me_destroy_index = 0

bullets = []

#创建子弹bullets列表存放子弹

# 如何生成普通子弹

bullet1 = []

#创建己方发射普通子弹列表

bullet1_index = 0

BULLET1_NUM = 4

#己方子弹的数量

for i in range(BULLET1_NUM):

#把发射的子弹加进bullet1列表

bullet1.append(bullet.Bullet1(me.rect.midtop))

#子弹发射的位置在己方飞机正上方

# 生成超级子弹

bullet2 = []

#建立一个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)))

#子弹发射的位置右

# 标记是否使用超级子弹

is_double_bullet = False

level = 1

if life_num and not paused:

# 绘制全屏炸弹补给并检测是否获得

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 not (delay % 10):

#延迟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 + 33, 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

3.7补给包设计

# 全屏炸弹(补给包

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)

#三十秒触发这个事件

# 绘制全屏炸弹补给并检测是否获得

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)

#到18秒时触发事件

bullet_supply.active = False

# 有三次全屏消除敌机的爆炸机会

#绘制剩余炸弹数量

bomb_text = bomb_font.render("x %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(str("Score: %s" % score), True, WHITE)

screen.blit(score_text, (10, 5))

elif life_num == 0:

#当爆炸包值为0时,结束混音器声音

pygame.mixer.music.stop()

pygame.mixer.stop()

# 停止发放补给

pygame.time.set_timer(SUPPLY_TIME, 0)

if not recorded:

recorded = True

3.8得分设计

running = True

#控制循环

switch_image = True

#控制切换图片

delay = 100

#控制延迟

score = 0

#自己定义的初始分0分

score_font = pygame.font.Font("font/font.ttf", 36)

#显示字体,font:指定字体,字体大小36

# 会根据用户的得分增加难度

if level == 1 and score > 50000:

#当用户的得分达到50000分时

level = 2

#会再升级到等级2

upgrade_sound.play()

add_small_enemies(small_enemies, enemies, 3)

#会出现3个小型敌机

add_mid_enemies(mid_enemies, enemies, 2)

#会出现2个中性敌机

add_big_enemies(big_enemies, enemies, 1)

#会出现1个大型敌机

inc_speed(small_enemies, 1)

#提升小型低级的速度

elif level == 2 and score > 300000:

level = 3

#用户得分到了300000时会升级到等级3,会出现5个小型敌机,3个中型敌机,2个大型敌机

upgrade_sound.play()

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 > 500000:

level = 4

upgrade_sound.play()

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()

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))

#重新调用背景

3.9其他设计

# 生命数量

life_image = pygame.image.load("images/life.png").convert_alpha()

life_rect = life_image.get_rect()

#获取图片属性

life_num = 3

# 游戏结束画面

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()

DOUBLE_BULLET_TIME = USEREVENT + 1

#第二个事件

# 解除我方飞机无敌状态

INVINCEBLE_TIME = USEREVENT + 2#第三个事件

# 阻止重复读取成绩记录文件

recorded = False

# 标志是否暂停游戏

paused = False

#设置初始值false,默认不暂停

paused_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 = paused_nor_image.get_rect()

#获取图片属性

paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10

#图片位置

paused_image = paused_nor_image

#切换图片

while running:

#死循环

for event in pygame.event.get():

#获取事件

if event.type == QUIT:

#如果按下×键,就关闭游戏退出

pygame.quit()

#退出

sys.exit()

#关闭游戏

elif event.type == MOUSEBUTTONDOWN:

#获取鼠标事件

ifevent.button==1andpaused_rect.collidepoint(event.pos):

#判断鼠标是否在图片范围内按下

paused = not paused

#如果按下了把flase变成true

if paused:

#如果暂停

pygame.time.set_timer(SUPPLY_TIME, 0)

#停止事件

pygame.mixer.music.pause()

#停止音乐

pygame.mixer.pause()

#停止特效

paused_image = resume_pressed_image

#显示空白屏幕

else:

pygame.time.set_timer(SUPPLY_TIME,30*1000)

#30秒执行补给包事件

pygame.mixer.music.unpause()

#播放音乐

pygame.mixer.unpause()

#播放特效

paused_image = pause_pressed_image

#显示原来游戏界面

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 = paused_nor_image

elif event.type == KEYDOWN:

#获取键盘事件

if event.key == K_SPACE:

#如果按下空格

if bomb_num:

#全屏炸弹

bomb_num -= 1

#炸弹数量-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 random.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 == INVINCEBLE_TIME:

me.invincible = False

pygame.time.set_timer(INVINCEBLE_TIME, 0)

# 读取历史最高分

with open("record/record.txt", "r") as f:

record_score = int(f.read())

if score > record_score:

with open("record/record.txt", "w") as f:

f.write(str(score))

# 绘制结束画面

record_score_text = score_font.render("Best: %d" % record_score, True, WHITE)

screen.blit(record_score_text, (50, 50))

gameover_text1 = gameover_font.render("Your Score: ", True, WHITE)

gameover_text1_rect = gameover_text1.get_rect()

gameover_text1_rect.left, gameover_text1_rect.top = \

(width - gameover_text1_rect.width) // 2, height // 2

screen.blit(gameover_text1, gameover_text1_rect)

gameover_text2 = gameover_font.render(str(score), True, WHITE)

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()

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()

参考文献

Python快速编程入门第2 版编著:黑马程序员人民邮电出版社

鱼C-小甲鱼 一位来自bilibili的up主 《零基础入门学习Python》中的飞机大战教学视频

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘟嘟彬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值