python童年小游戏坦克大战

项目实现功能:

我方坦克的移动,发射子弹;敌方坦克的AL移动,发射子弹;背景音乐,击中,碰撞动画效果及音效;地图的巧妙完美布局;

游戏玩法:

控制我方坦克保护老总免受敌方坦克攻击。

代码量:

纯代码共计400行左右。

项目主要涉及知识:

python基础,面向对象思想,pygame模块。

项目结构:

 

下面粘贴部分主要代码可供同学参考:

tank_war.py

import pygame
from sprites import *


class TankWar:

def __init__(self):
self.screen = pygame.display.set_mode(Settings.SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.game_still = True
self.hero = None
self.enemies = None
self.enemy_bullets = None
self.walls = None

@staticmethod
def __init_game():
"""
初始化游戏的一些设置
:return:
"""
pygame.init() # 初始化pygame模块
pygame.display.set_caption(Settings.GAME_NAME) # 设置窗口标题
pygame.mixer.init() # 初始化音频模块

def __create_sprite(self):
self.hero = Hero(Settings.HERO_IMAGE_NAME, self.screen)
self.enemies = pygame.sprite.Group()
self.enemy_bullets = pygame.sprite.Group()
self.walls = pygame.sprite.Group()
for i in range(Settings.ENEMY_COUNT):
direction = random.randint(0, 3)
enemy = Enemy(Settings.ENEMY_IMAGES[direction], self.screen)
enemy.direction = direction
self.enemies.add(enemy)
self.__draw_map()

def __draw_map(self):
"""
绘制地图
:return:
"""
for y in range(len(Settings.MAP_ONE)):
for x in range(len(Settings.MAP_ONE[y])):
if Settings.MAP_ONE[y][x] == 0:
continue
wall = Wall(Settings.WALLS[Settings.MAP_ONE[y][x]], self.screen)
wall.rect.x = x*Settings.BOX_SIZE
wall.rect.y = y*Settings.BOX_SIZE
if Settings.MAP_ONE[y][x] == Settings.RED_WALL:
wall.type = Settings.RED_WALL
elif Settings.MAP_ONE[y][x] == Settings.IRON_WALL:
wall.type = Settings.IRON_WALL
elif Settings.MAP_ONE[y][x] == Settings.WEED_WALL:
wall.type = Settings.WEED_WALL
elif Settings.MAP_ONE[y][x] == Settings.BOSS_WALL:
wall.type = Settings.BOSS_WALL
wall.life = 1
self.walls.add(wall)

def __check_keydown(self, event):
"""检查按下按钮的事件"""
if event.key == pygame.K_LEFT:
# 按下左键
self.hero.direction = Settings.LEFT
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_RIGHT:
# 按下右键
self.hero.direction = Settings.RIGHT
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_UP:
# 按下上键
self.hero.direction = Settings.UP
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_DOWN:
# 按下下键
self.hero.direction = Settings.DOWN
self.hero.is_moving = True
self.hero.is_hit_wall = False
elif event.key == pygame.K_SPACE:
# 坦克发子弹
self.hero.shot()

def __check_keyup(self, event):
"""检查松开按钮的事件"""
if event.key == pygame.K_LEFT:
# 松开左键
self.hero.direction = Settings.LEFT
self.hero.is_moving = False
elif event.key == pygame.K_RIGHT:
# 松开右键
self.hero.direction = Settings.RIGHT
self.hero.is_moving = False
elif event.key == pygame.K_UP:
# 松开上键
self.hero.direction = Settings.UP
self.hero.is_moving = False
elif event.key == pygame.K_DOWN:
# 松开下键
self.hero.direction = Settings.DOWN
self.hero.is_moving = False

def __event_handler(self):
for event in pygame.event.get():
# 判断是否是退出游戏
if event.type == pygame.QUIT:
TankWar.__game_over()
elif event.type == pygame.KEYDOWN:
TankWar.__check_keydown(self, event)
elif event.type == pygame.KEYUP:
TankWar.__check_keyup(self, event)

def __check_collide(self):
# 保证坦克不移出屏幕
self.hero.hit_wall()
for enemy in self.enemies:
enemy.hit_wall_turn()

# 子弹击中墙
for wall in self.walls:
# 我方英雄子弹击中墙
for bullet in self.hero.bullets:
if pygame.sprite.collide_rect(wall, bullet):
if wall.type == Settings.RED_WALL:
wall.kill()
bullet.kill()
elif wall.type == Settings.BOSS_WALL:
self.game_still = False
elif wall.type == Settings.IRON_WALL:
bullet.kill()
# 敌方英雄子弹击中墙
for enemy in self.enemies:
for bullet in enemy.bullets:
if pygame.sprite.collide_rect(wall, bullet):
if wall.type == Settings.RED_WALL:
wall.kill()
bullet.kill()
elif wall.type == Settings.BOSS_WALL:
self.game_still = False
elif wall.type == Settings.IRON_WALL:
bullet.kill()

# 我方坦克撞墙
if pygame.sprite.collide_rect(self.hero, wall):
# 不可穿越墙
if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.BOSS_WALL:
self.hero.is_hit_wall = True
# 移出墙内
self.hero.move_out_wall(wall)

# 敌方坦克撞墙
for enemy in self.enemies:
if pygame.sprite.collide_rect(wall, enemy):
if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.BOSS_WALL:
enemy.move_out_wall(wall)
enemy.random_turn()

# 子弹击中、敌方坦克碰撞、敌我坦克碰撞
pygame.sprite.groupcollide(self.hero.bullets, self.enemies, True, True)
# 敌方子弹击中我方
for enemy in self.enemies:
for bullet in enemy.bullets:
if pygame.sprite.collide_rect(bullet, self.hero):
bullet.kill()
self.hero.kill()

def __update_sprites(self):
if self.hero.is_moving:
self.hero.update()
self.walls.update()
self.hero.bullets.update()
self.enemies.update()
for enemy in self.enemies:
enemy.bullets.update()
enemy.bullets.draw(self.screen)
self.enemies.draw(self.screen)
self.hero.bullets.draw(self.screen)
self.screen.blit(self.hero.image, self.hero.rect)
self.walls.draw(self.screen)

def run_game(self):
self.__init_game()
self.__create_sprite()
while True and self.hero.is_alive and self.game_still:
self.screen.fill(Settings.SCREEN_COLOR)
# 1、设置刷新帧率
self.clock.tick(Settings.FPS)
# 2、事件监听
self.__event_handler()
# 3、碰撞监测
self.__check_collide()
# 4、更新/绘制精灵/经理组
self.__update_sprites()
# 5、更新显示
pygame.display.update()
self.__game_over()

@staticmethod
def __game_over():
pygame.quit()
exit()

部分项目运行截图:

 

ps:项目写的简单,同学们可以在此功能上自行修改,添加元素(如累计击中敌方得分,随机药物,者子弹或者护盾补给等等 

猜你喜欢

转载自blog.csdn.net/Abtxr/article/details/131393183