Python“飞机大战”上下左右移动空格发射子弹

您所在的位置:网站首页 飞机撞飞机游戏 Python“飞机大战”上下左右移动空格发射子弹

Python“飞机大战”上下左右移动空格发射子弹

2023-12-23 09:29| 来源: 网络整理| 查看: 265

下载点此去

最后面有运行视频 一、项目背景 作为一名学习计算机的学生,在以往,我认为学习计算机要么就是无所不能的黑客,要么就是能制作出各种软件程序的大神。我选择pygame板块,制作一款能随意更改游戏内容,游戏难易,能自我主宰的打飞机小游戏。

二、需求分析 1、场景设计、背景音乐、音效、窗口显示、相关数据内容的呈现。 2、玩家移动、子弹发射的按键操作,多个敌人的自由移动、控制出界。 3、子弹与敌人的射中检测,敌人与玩家的碰撞检测,敌人位置重置。 4、分数的显示,游戏结束提示,最终分数呈现,结束状态。 5、在进行创作之前我们需要在脑海中进行先行创作,进行游戏框架的构建,确保思路有条不紊,并了解一些相关知识和代码的应用。

三、系统总体设计

在这里插入图片描述

四、系统详细设计 1、显示窗口 (1)、导入相应的库,并进行数据初始化。 (需要提前安装好相应插件和有关制作素材) (2)、导入背景音乐,并进行音量调节,因为背景音乐从始至终都有,所以我们需要对它进行一个循环设定,相关代码如下: 在这里插入图片描述

(3)、导入背景图片、并获取它的大小,以此来创建我们的游戏窗口(图片尺寸可在图片中查看),然后再窗口上加一个窗口名,即我们的游戏名称。相关代码如下: 在这里插入图片描述

(4)、游戏主循环(没有主循环,窗口会卡死,无法显示),此循环为死循环,以便我们能关闭游戏窗口,背景也要放在主循环中。 、 在这里插入图片描述

(5)、界面更新

在这里插入图片描述

2、玩家设定 (1)、导入玩家图片素材(这里注意,玩家素材的导入应在背景图片后,以免盖住背景。)玩家的初位置应当设置在最下面的中间部位,这里需要根据窗口大小进行计算,注意定义变量。 pygame.sprite.Sprite.init(self) self.image = [] # 用来存储玩家对象精灵图片

(2)玩家设定(定义变量) 设置玩家相关参数 player_rect = [] player_rect.append(pygame.Rect(0, 99, 102, 126)) # 玩家精灵图片区域 player_rect.append(pygame.Rect(165, 360, 102, 126)) player_rect.append(pygame.Rect(165, 234, 102, 126)) # 玩家爆炸精灵图片区域 player_rect.append(pygame.Rect(330, 624, 102, 126)) player_rect.append(pygame.Rect(330, 498, 102, 126)) player_rect.append(pygame.Rect(432, 624, 102, 126)) player_pos = [200, 600] player = Player(plane_img, player_rect, player_pos) 3、设置键盘事件 设置键盘上下左右进行控制飞机移动,并设置移动速度

if key_pressed[K_d] or key_pressed[K_RIGHT]: player.moveRight()

如上:更改K_后的RIGAHT为LEFT、UP、DOWN设置其他三个方向的移动,注意速度的设定。 添加敌人 1生成敌机 if enemy_frequency % 50 == 0: enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) enemies1.add(enemy1) enemy_frequency += 1 if enemy_frequency >= 100: enemy_frequency = 0 2移动敌机,若超出窗口范围则删除 for enemy in enemies1: enemy.move()

4、添加子弹和炸弹

1 我先建立子弹的机制

控制发射子弹频率,并发射子弹

if not player.is_hit: if shoot_frequency % 15 == 0: bullet_sound.play() player.shoot(bullet_img) shoot_frequency += 1 if shoot_frequency >= 15: shoot_frequency = 0 2接下来是建立炸弹 子弹和炸弹本身都是一样的,都是对周围造成伤害,大小不一样罢了(但是这个地方没有方法体现)。那么炸弹跟子弹应该一样,只是炸弹由玩家通过空格放置。那么解决方式有个简单的方式,加一个炸弹类,把速度调为0。接下来,把频率放子弹的机制在炸弹中取消,然后加一个控制到末端的键盘控制,就可以完成任务

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

5、击中判断

(1)监听键盘事件 key_pressed = pygame.key.get_pressed()

若玩家被击中,则无效

if not player.is_hit: if key_pressed[K_w] or key_pressed[K_UP]: player.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: player.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: player.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: player.moveRight()

(2)、移动子弹,若超出窗口范围则删除 for bullet in player.bullets: bullet.move() if bullet.rect.bottom < 0: player.bullets.remove(bullet) (3) def reset(self): self.x = random.randint(225,700) self.y = random.randint(100,150) (4)击中音效:添加击中音效 (5)bullet_sound.play()

6、分数显示 添加分数:击中的时候,分数增加,所以分数在击中后增加。 显示分数:因为分数一直都显示,所以主循环中必须设置分数的大小和字体,(注意:分数是击中后才显示的)。 )# 绘制得分 score_font = pygame.font.Font(None, 36) score_text = score_font.render(str(score), True, (128, 128, 128)) text_rect = score_text.get_rect() text_rect.topleft = [10, 10] screen.blit(score_text, text_rect)

7、游戏结束 当玩家被击中时设定为游戏结束,所以要测算玩家与敌人之间的距离

判断玩家是否被击中

if pygame.sprite.collide_circle(enemy, player): enemies_down.add(enemy) enemies1.remove(enemy) player.is_hit = True game_over_sound.play() break if enemy.rect.top > SCREEN_HEIGHT: enemies1.remove(enemy)

出现结束提示:当游戏被判定为结束时,屏幕上出现一个结束 font = pygame.font.Font(None, 48) text = font.render('Score: '+ str(score), True, (255, 0, 0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.centery = screen.get_rect().centery + 24 screen.blit(game_over, (0, 0)) screen.blit(text, text_rect)

8、代码如下: import pygame from sys import exit from pygame.locals import * #from gameRole import * import random

#定义类 SCREEN_WIDTH = 480 SCREEN_HEIGHT = 800

TYPE_SMALL = 1 TYPE_MIDDLE = 2 TYPE_BIG = 3

子弹类

class Bullet(pygame.sprite.Sprite): def init(self, bullet_img, init_pos): pygame.sprite.Sprite.init(self) self.image = bullet_img self.rect = self.image.get_rect() self.rect.midbottom = init_pos self.speed = 10

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

class Player(pygame.sprite.Sprite): def init(self, plane_img, player_rect, init_pos): pygame.sprite.Sprite.init(self) self.image = [] # 用来存储玩家对象精灵图片的列表 for i in range(len(player_rect)): self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha()) self.rect = player_rect[0] # 初始化图片所在的矩形 self.rect.topleft = init_pos # 初始化矩形的左上角坐标 self.speed = 8 # 初始化玩家速度,这里是一个确定的值 self.bullets = pygame.sprite.Group() # 玩家飞机所发射的子弹的集合 self.img_index = 0 # 玩家精灵图片索引 self.is_hit = False # 玩家是否被击中

def shoot(self, bullet_img): bullet = Bullet(bullet_img, self.rect.midtop) self.bullets.add(bullet) def moveUp(self): if self.rect.top = SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height else: self.rect.top += self.speed def moveLeft(self): if self.rect.left = SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left += self.speed 敌人类

class Enemy(pygame.sprite.Sprite): def init(self, enemy_img, enemy_down_imgs, init_pos): pygame.sprite.Sprite.init(self) self.image = enemy_img self.rect = self.image.get_rect() self.rect.topleft = init_pos self.down_imgs = enemy_down_imgs self.speed = 2 self.down_index = 0

def move(self): self.rect.top += self.speed 初始化游戏

pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption(‘飞机大战’)

载入游戏音乐

bullet_sound = pygame.mixer.Sound(‘resources/sound/bullet.wav’) enemy1_down_sound = pygame.mixer.Sound(‘resources/sound/enemy1_down.wav’) game_over_sound = pygame.mixer.Sound(‘resources/sound/game_over.wav’) bullet_sound.set_volume(0.3) enemy1_down_sound.set_volume(0.3) game_over_sound.set_volume(0.3) pygame.mixer.music.load(‘resources/sound/game_music.wav’) pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(0.25)

载入背景图

background = pygame.image.load(‘resources/image/background.png’).convert() game_over = pygame.image.load(‘resources/image/gameover.png’)

filename = ‘resources/image/shoot.png’ plane_img = pygame.image.load(filename)

设置玩家相关参数

player_rect = [] player_rect.append(pygame.Rect(0, 99, 102, 126)) # 玩家精灵图片区域 player_rect.append(pygame.Rect(165, 360, 102, 126)) player_rect.append(pygame.Rect(165, 234, 102, 126)) # 玩家爆炸精灵图片区域 player_rect.append(pygame.Rect(330, 624, 102, 126)) player_rect.append(pygame.Rect(330, 498, 102, 126)) player_rect.append(pygame.Rect(432, 624, 102, 126)) player_pos = [200, 600] player = Player(plane_img, player_rect, player_pos)

定义子弹对象使用的surface相关参数

bullet_rect = pygame.Rect(1004, 987, 9, 21) bullet_img = plane_img.subsurface(bullet_rect)

定义敌机对象使用的surface相关参数

enemy1_rect = pygame.Rect(534, 612, 57, 43) enemy1_img = plane_img.subsurface(enemy1_rect) enemy1_down_imgs = [] enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 347, 57, 43))) enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873, 697, 57, 43))) enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267, 296, 57, 43))) enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930, 697, 57, 43)))

enemies1 = pygame.sprite.Group()

存储被击毁的飞机,用来渲染击毁精灵动画

enemies_down = pygame.sprite.Group()

shoot_frequency = 0 enemy_frequency = 0

player_down_index = 16

score = 0

clock = pygame.time.Clock()

running = True

while running: # 控制游戏最大帧率为60 clock.tick(60)

# 控制发射子弹频率,并发射子弹 if not player.is_hit: if shoot_frequency % 15 == 0: bullet_sound.play() player.shoot(bullet_img) shoot_frequency += 1 if shoot_frequency >= 15: shoot_frequency = 0 # 生成敌机 if enemy_frequency % 50 == 0: enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) enemies1.add(enemy1) enemy_frequency += 1 if enemy_frequency >= 100: enemy_frequency = 0 # 移动子弹,若超出窗口范围则删除 for bullet in player.bullets: bullet.move() if bullet.rect.bottom < 0: player.bullets.remove(bullet) # 移动敌机,若超出窗口范围则删除 for enemy in enemies1: enemy.move() # 判断玩家是否被击中 if pygame.sprite.collide_circle(enemy, player): enemies_down.add(enemy) enemies1.remove(enemy) player.is_hit = True game_over_sound.play() break if enemy.rect.top > SCREEN_HEIGHT: enemies1.remove(enemy) # 将被击中的敌机对象添加到击毁敌机Group中,用来渲染击毁动画 enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1) for enemy_down in enemies1_down: enemies_down.add(enemy_down) # 绘制背景 screen.fill(0) screen.blit(background, (0, 0)) # 绘制玩家飞机 if not player.is_hit: screen.blit(player.image[player.img_index], player.rect) # 更换图片索引使飞机有动画效果 player.img_index = shoot_frequency // 8 else: player.img_index = player_down_index // 8 screen.blit(player.image[player.img_index], player.rect) player_down_index += 1 if player_down_index > 47: running = False # 绘制击毁动画 for enemy_down in enemies_down: if enemy_down.down_index == 0: enemy1_down_sound.play() if enemy_down.down_index > 7: enemies_down.remove(enemy_down) score += 1000 continue screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect) enemy_down.down_index += 1 # 绘制子弹和敌机 player.bullets.draw(screen) enemies1.draw(screen) # 绘制得分 score_font = pygame.font.Font(None, 36) score_text = score_font.render(str(score), True, (128, 128, 128)) text_rect = score_text.get_rect() text_rect.topleft = [10, 10] screen.blit(score_text, text_rect) # 更新屏幕 pygame.display.update()

9、运行视频:

题目: Python“飞机大战”

五、课程总结 不知不觉课程已经接近尾声,说实话,自己没有学到太多东西,并不是自己天赋问题,也不是老师的教学有问题,相反,老师的很棒,只是自己过于慵懒。但有一天沉静下来仔细去钻研考究,才发现Python除了烦人的语法,改不完的B ug,不会做的练习题还有成功编译出属于自己的程序的那份喜悦,希望自己能更熟练地去掌握运用它。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3