导语
今天是回忆童年的一天,文章游戏代码的话小编写了蛮多还,等啥时候有时间了我整理下笔记归类放着!!
《坦克大战》当年算是红遍大江南北的热门游戏。
别看这游戏画面简陋,它可是集合了所有经典游戏的成功要素——
上手容易、升级强化、关卡丰富、可以双打,甚至还有良心到爆的编辑模式。
虽然是个低K游戏,但在当时的国人看来,这游戏买得非常值!
今天小编带大家写一款经典版的坦克大战游戏项目,期待叭~
正文
**(1)准备的图片素材很多分类处理:**以下是部分截图。
**(2)不同的音乐背景:**当熟悉的背景音乐响起,扑面而来的是整个童年啊!
(3)游戏规则:
游戏有单人和双人两种模式,己方大本营被破或者己方坦克被歼灭则游戏失败,成功通过所有关卡则游戏胜利。另外,玩家可以通过射击特定的坦克使地图上随机出现一个道具,若己方坦克捡到该道具,则触发一个事件,例如坦克能力的增强。
玩家操作方式如下:
玩家一:
wsad键:上下左右;
空格键:射击。
玩家二:
↑↓←→键:上下左右;
小键盘0键:射击。
# 玩家一, WSAD移动, 空格键射击
if tank_player1.num_lifes >= 0:
if key_pressed[pygame.K_w]:
player_tanks_group.remove(tank_player1)
tank_player1.move('up', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player1)
elif key_pressed[pygame.K_s]:
player_tanks_group.remove(tank_player1)
tank_player1.move('down', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player1)
elif key_pressed[pygame.K_a]:
player_tanks_group.remove(tank_player1)
tank_player1.move('left', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player1)
elif key_pressed[pygame.K_d]:
player_tanks_group.remove(tank_player1)
tank_player1.move('right', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player1)
elif key_pressed[pygame.K_SPACE]:
bullet = tank_player1.shoot()
if bullet:
self.sounds['fire'].play() if tank_player1.tanklevel < 2 else self.sounds['Gunfire'].play()
player_bullets_group.add(bullet)
# 玩家二, ↑↓←→移动, 小键盘0键射击
if self.is_dual_mode and (tank_player2.num_lifes >= 0):
if key_pressed[pygame.K_UP]:
player_tanks_group.remove(tank_player2)
tank_player2.move('up', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player2)
elif key_pressed[pygame.K_DOWN]:
player_tanks_group.remove(tank_player2)
tank_player2.move('down', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player2)
elif key_pressed[pygame.K_LEFT]:
player_tanks_group.remove(tank_player2)
tank_player2.move('left', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player2)
elif key_pressed[pygame.K_RIGHT]:
player_tanks_group.remove(tank_player2)
tank_player2.move('right', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
player_tanks_group.add(tank_player2)
elif key_pressed[pygame.K_KP0]:
bullet = tank_player2.shoot()
if bullet:
player_bullets_group.add(bullet)
self.sounds['fire'].play() if tank_player2.tanklevel < 2 else self.sounds['Gunfire'].play()
复制代码
加载游戏素材中:
附:
# 加载游戏素材
sounds = {}
for key, value in cfg.AUDIO_PATHS.items():
sounds[key] = pygame.mixer.Sound(value)
sounds[key].set_volume(1)
复制代码
(4)在坦克移动中、打到对方等肯定有碰撞:
# 碰撞检测
# --子弹和砖墙
pygame.sprite.groupcollide(player_bullets_group, self.scene_elems.get('brick_group'), True, True)
pygame.sprite.groupcollide(enemy_bullets_group, self.scene_elems.get('brick_group'), True, True)
# --子弹和铁墙
for bullet in player_bullets_group:
if pygame.sprite.spritecollide(bullet, self.scene_elems.get('iron_group'), bullet.is_stronger, None):
player_bullets_group.remove(bullet)
pygame.sprite.groupcollide(enemy_bullets_group, self.scene_elems.get('iron_group'), True, False)
# --子弹撞子弹
pygame.sprite.groupcollide(player_bullets_group, enemy_bullets_group, True, True)
# --我方子弹撞敌方坦克
for tank in enemy_tanks_group:
if pygame.sprite.spritecollide(tank, player_bullets_group, True, None):
if tank.food:
foods_group.add(tank.food)
tank.food = None
if tank.decreaseTankLevel():
self.sounds['bang'].play()
self.total_enemy_num -= 1
# --敌方子弹撞我方坦克
for tank in player_tanks_group:
if pygame.sprite.spritecollide(tank, enemy_bullets_group, True, None):
if tank.is_protected:
self.sounds['blast'].play()
else:
if tank.decreaseTankLevel():
self.sounds['bang'].play()
if tank.num_lifes < 0:
player_tanks_group.remove(tank)
# --我方子弹撞我方大本营
if pygame.sprite.spritecollide(home, player_bullets_group, True, None):
is_win = False
is_running = False
home.setDead()
# --敌方子弹撞我方大本营
if pygame.sprite.spritecollide(home, enemy_bullets_group, True, None):
is_win = False
is_running = False
home.setDead()
复制代码
(5)坦克游戏中会随机生成食物:
# --坦克吃到食物
for player_tank in player_tanks_group:
for food in foods_group:
if pygame.sprite.collide_rect(player_tank, food):
if food.name == 'boom':
self.sounds['add'].play()
for _ in enemy_tanks_group:
self.sounds['bang'].play()
self.total_enemy_num -= len(enemy_tanks_group)
enemy_tanks_group = pygame.sprite.Group()
elif food.name == 'clock':
self.sounds['add'].play()
for enemy_tank in enemy_tanks_group:
enemy_tank.setStill()
elif food.name == 'gun':
self.sounds['add'].play()
player_tank.improveTankLevel()
elif food.name == 'iron':
self.sounds['add'].play()
self.__pretectHome()
elif food.name == 'protect':
self.sounds['add'].play()
player_tank.setProtected()
elif food.name == 'star':
self.sounds['add'].play()
player_tank.improveTankLevel()
player_tank.improveTankLevel()
elif food.name == 'tank':
self.sounds['add'].play()
player_tank.addLife()
foods_group.remove(food)
复制代码