1、坦克大战游戏8
当前阶段要实现的是为游戏增加声音效果,以提升游戏体验。
1-1、增加audio.py类
添加的游戏音效包括:
- 游戏开始音乐
- 玩家发射炮弹的声音
- 玩家攻击铁墙的声音
- 玩家攻击坦克的声音
- 玩家吃到获取生命的声音
- 玩家吃到宝物的声音
- 玩家吃到暂停的声音
- 玩家被打死的声音
- 敌人被打死的声音
- 玩家移动的声音
- 游戏结束(失败)的声音
import pygame
class Audio:
def __init__(self):
# 玩家发射炮弹的声音
self.bullet = pygame.mixer.Sound('../audio/bullet.mp3')
# 加载音效-玩家攻击石头的声音
self.hit_stone = pygame.mixer.Sound('../audio/hit_stone.mp3')
# 加载音效-玩家攻击坦克的声音
self.hit_tank = pygame.mixer.Sound('../audio/hit_tank.mp3')
# 加载音效-玩家吃到获取生命的声音
self.get_live = pygame.mixer.Sound('../audio/get_live.mp3')
# 加载音效-玩家吃到宝物的声音
self.get_treasure = pygame.mixer.Sound('../audio/get_treasure.mp3')
# 加载音效-玩家吃到暂停的声音
self.get_pause = pygame.mixer.Sound('../audio/get_pause.mp3')
# 玩家被打死的声音
self.player_dead = pygame.mixer.Sound('../audio/player_dead.mp3')
# 敌人被打死的声音
self.enemy_dead = pygame.mixer.Sound('../audio/enemy_dead.mp3')
# 加载音效-玩家移动的声音
self.move = pygame.mixer.Sound('../audio/move.wav')
self.move_play = None
# 加载游戏结束(失败)的声音
self.game_over = pygame.mixer.Sound('../audio/game_over.wav')
def play(self, sound_name):
if sound_name == 'start_game':
# 播放游戏开始音乐
pygame.mixer.music.load('../audio/game_start.wav')
pygame.mixer.music.play(0, start=0, fade_ms=2000)
if sound_name == 'bullet':
self.bullet.play(maxtime=0)
if sound_name == 'hit_stone':
self.hit_stone.play(maxtime=0)
if sound_name == 'hit_tank':
self.hit_tank.play(maxtime=0)
if sound_name == 'get_treasure':
self.get_treasure.play(maxtime=0)
if sound_name == 'get_live':
self.get_live.play(maxtime=0)
if sound_name == 'get_pause':
self.get_pause.play(maxtime=0)
if sound_name == 'player_dead':
self.player_dead.play(maxtime=0)
if sound_name == 'enemy_dead':
self.enemy_dead.play(maxtime=0)
if sound_name == 'move':
if self.move_play is None:
self.move_play = self.move.play(loops=-1)
else:
self.move_play.unpause()
if sound_name == 'game_over':
self.game_over.play(maxtime=0)
def pause(self, sound_name):
if sound_name == 'move' and self.move_play is not None:
self.move_play.pause()
1-2、在level.py中创建游戏音效对象
def __init__(self):
.....
# 初始化音效对象
self.audio = Audio()
.....
1-3、游戏音效调用
- 游戏开始音效,在levle.py类的start_game(self)方法中调用
# 开始游戏
def start_game(self):
.......
self.audio.play('start_game')
- 游戏结束是音效,在levle.py类的game_over(self)方法中调用
# 游戏结束
def game_over(self):
if self.game_status != 'game_over':
self.game_status = 'game_over'
self.start_time = pygame.time.get_ticks()
# 播放游戏结束音效
self.audio.play('game_over')
- 玩家移动音效,玩家发射炮弹音效,在player.py中的input(self)方法中调用
# 键盘事件
def input(self):
......
if self.direct.x != 0 or self.direct.y != 0:
# 播放移动声音
self.level_class.audio.play('move')
else:
# 停止播放移动声音
self.level_class.audio.pause('move')
# 坦克发射炮弹
if (keys[pygame.K_SPACE] and not self.timers['missile'].lock
and self.direct.x == 0 and self.direct.y == 0):
......
# 玩家发射炮弹音效
self.level_class.audio.play('bullet')
- 玩家炮弹打到铁墙和边界的音效,在missile.py类的collsion(self)方法中调用
# 炮弹碰撞
def collision(self):
for obstacle in self.obstacle_group:
if obstacle.hitbox.colliderect(self.hitbox):
# 炮弹打到土墙
if obstacle.obstacle_type == '1':
....
elif obstacle.obstacle_type == '2' or obstacle.obstacle_type == '7':
......
if self.activity.activity_type == 'player':
# 玩家炮弹打到铁墙的效果
self.level_class.audio.play('hit_stone')
elif obstacle.obstacle_type == '6':
......
- 玩家打死敌人,以及打到高等级敌人(血量大于1)的音效,在missile.py类的collision_player_enemy(self)方法中调用
# 玩家击打敌人
def collision_player_enemy(self):
for enemy in self.enemy_group:
enemy_hitbox = enemy.hitbox
if enemy_hitbox.colliderect(self.hitbox):
self.lock = True
if not enemy.status['daddy']:
hp = enemy.status['hp']
hp -= self.attack_harm
if hp < 0:
hp = 0
enemy.status['hp'] = hp
if hp == 0:
....
# 播放敌人被打死的音效
self.level_class.audio.play('enemy_dead')
....
else:
# 播放打击敌人(亮坦克)未死亡时的音效
self.level_class.audio.play('hit_tank')
.....
# 只要玩家发射的炮弹和敌人碰撞了,炮弹要销毁并且释放炮弹锁
self.kill()
self.un_lock()
- 敌人打死玩家音效,在missile.py类的collision_enemy_player(self)方法中调用
# 敌人击打玩家
def collision_enemy_player(self):
for player in self.player_group:
if player.hitbox.colliderect(self.hitbox):
self.lock = True
if not player.status['daddy']:
.....
# 如果玩家的血量等于0,则玩家被消灭
if hp == 0 or level == 'level0':
....
# 播放玩家被打死的音效
self.level_class.audio.play('enemy_dead')
.....
else:
player.update_status(level, direction, False)
.....
- 关于玩家吃到宝物的音效,在treasure.py类的collision_player(self)中调用
# 玩家同宝物碰撞
def collision_player(self):
for player in self.player_group:
# 玩家同宝物碰撞
if player.hitbox.colliderect(self.hitbox):
if self.treasure_type == 'bomb':
# 播放敌人被打死的音效
self.level_class.audio.play('enemy_dead')
.....
elif self.treasure_type == 'gun':
# 播放玩家获取宝贝音效
self.level_class.audio.play('get_treasure')
.....
elif self.treasure_type == 'hat':
# 播放玩家获取宝贝音效
self.level_class.audio.play('get_treasure')
.....
elif self.treasure_type == 'iron':
# 播放玩家获取宝贝音效
self.level_class.audio.play('get_treasure')
.....
elif self.treasure_type == 'life':
# 播放玩家获取宝贝(生命+1)音效
self.level_class.audio.play('get_live')
......
elif self.treasure_type == 'pause':
# 播放玩家获取宝贝音效
self.level_class.audio.play('get_treasure')
.....
elif self.treasure_type == 'star':
# 播放玩家获取宝贝音效
self.level_class.audio.play('get_star')
....
self.kill()
2、运行游戏并查看游戏效果
2-1、打开cmd命令窗口
2-2、进入code目录
2-3、执行命令:python main.py
游戏运行过程会根据玩家及敌人的动作播放对应的游戏音效,当前只是配置了主要的音效,读者可以根据自己的需求增加更多的音效以提高游戏的体验。
点击源码包下载源码

4154

被折叠的 条评论
为什么被折叠?



