写在前面:
坦克大战,这款经典的电子游戏,无疑是许多80后和90后心中不可磨灭的童年记忆。它不仅仅是一款游戏,更是那个时代科技娱乐方式的缩影,见证了电子游戏行业的起步与发展。
在那个电脑和网络尚未完全普及的年代,坦克大战以其简单的规则、丰富的关卡设计和紧张刺激的游戏性,迅速赢得了广大玩家的喜爱。游戏中,玩家需要驾驶坦克在各种地形中穿梭,巧妙地躲避敌人的攻击,同时精准地击毁敌方坦克,以获得胜利。
这款游戏的成功,不仅在于它的娱乐性,更在于它所蕴含的策略性和合作精神。玩家需要不断地思考如何利用地形优势,如何合理分配火力,以及如何在团队中与队友协同作战。这些元素的结合,使得坦克大战成为了一款深受各个年龄段玩家欢迎的游戏。
随着时间的推移,尽管游戏界出现了许多新的游戏类型和平台,但坦克大战依然在许多玩家心中占有一席之地。它不仅代表了那个时代的美好回忆,更是游戏文化发展史上的一个重要里程碑。今天,当我们回顾这款游戏时,不禁感叹于它对后世游戏设计和玩家游戏体验所产生的深远影响。也算是带大家回顾经典,今天就带大家用Python实现一下童年回忆——“坦克大战”。
环境搭建
开发环境和库
- Python 3.8: 作为编程语言的核心。
- Pygame 2.0.1: 用于游戏开发的Python库。
安装步骤:
安装Python 3.8:
访问Python官方网站下载并安装Python 3.8。
安装时确保勾选“Add Python to PATH
”。
安装Pygame 2.0.1:
打开命令行,输入pip install pygame==2.0.1
来安装指定版本的Pygame。
项目实现
游戏开始界面的原理就是在650×650的界面中在指定位置放置图片以及输出文本,然后通过检测鼠标的移动以及按键来检测,如果鼠标移动到说明的矩形框中就在下方放置说明的图片,移开后就用黑色填充矩形进行覆盖。如果鼠标移入开始的矩形框中,并按下左键就返回主函数,调用游戏场景的函数。
前期准备
在前期我们需要将编程需要的素材准备好。这里编程平台使用的是pycharm,使用到的包有pygame、sys、random和自定义的文件,导包如下:
import pygame
from pygame.sprite import Group
from pygame import sprite, display, image
图片素材
开发游戏元素
设计坦克类:坦克类应该包含移动、旋转和射击等基本功能。此外,还需要考虑坦克的耐久度、速度和武器升级等属性。
我方坦克:
import pygame
import bulletClass
tank_T1_0 = r"..\image\tank_T1_0.png"
tank_T1_1 = r"..\image\tank_T1_1.png"
tank_T1_2 = r"..\image\tank_T1_2.png"
tank_T2_0 = r"..\image\tank_T2_0.png"
tank_T2_1 = r"..\image\tank_T2_1.png"
tank_T2_2 = r"..\image\tank_T2_2.png"
class MyTank(pygame.sprite.Sprite):
def __init__(self, playerNumber):
pygame.sprite.Sprite.__init__(self)
# �������
self.life = True
#���ڼ������ ̹�˵������ȼ�
if playerNumber == 1:
self.tank_L0_image = pygame.image.load(tank_T1_0).convert_alpha()
self.tank_L1_image = pygame.image.load(tank_T1_1).convert_alpha()
self.tank_L2_image = pygame.image.load(tank_T1_2).convert_alpha()
if playerNumber == 2:
self.tank_L0_image = pygame.image.load(tank_T2_0).convert_alpha()
self.tank_L1_image = pygame.image.load(tank_T2_1).convert_alpha()
self.tank_L2_image = pygame.image.load(tank_T2_2).convert_alpha()
self.level = 0
# ��ʼ̹��Ϊ0��
self.tank = self.tank_L0_image
# �˶��е�����ͼƬ
self.tank_R0 = self.tank.subsurface((0, 0),(48, 48))
self.tank_R1 = self.tank.subsurface((48, 0),(48, 48))
self.rect = self.tank_R0.get_rect()
if playerNumber == 1:
self.rect.left, self.rect.top = 3 + 24 * 8, 3 + 24 * 24
if playerNumber == 2:
self.rect.left, self.rect.top = 3 + 24 * 16, 3 + 24 * 24
# ̹���ٶ� ̹�˷��� ̹������ �ӵ���ȴ
self.speed = 3
self.dir_x, self.dir_y = 0, -1
self.life = 3
self.bulletNotCooling = True
self.bullet = bulletClass.Bullet()
#self.bullet.rect.left, self.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
def shoot(self):
# �ӵ�
self.bullet.life = True
self.bullet.changeImage(self.dir_x, self.dir_y)
if self.dir_x == 0 and self.dir_y == -1:
self.bullet.rect.left = self.rect.left + 20
self.bullet.rect.bottom = self.rect.top + 1
elif self.dir_x == 0 and self.dir_y == 1:
self.bullet.rect.left = self.rect.left + 20
self.bullet.rect.top = self.rect.bottom - 1
elif self.dir_x == -1 and self.dir_y == 0:
self.bullet.rect.right = self.rect.left - 1
self.bullet.rect.top = self.rect.top + 20
elif self.dir_x == 1 and self.dir_y == 0:
self.bullet.rect.left = self.rect.right + 1
self.bullet.rect.top = self.rect.top + 20
if self.level == 1:
self.bullet.speed = 16
self.bullet.strong = False
if self.level == 2:
self.bullet.speed = 16
self.bullet.strong = True
if self.level == 3:
self.bullet.speed = 48
self.bullet.strong = True
def levelUp(self):
if self.level < 2:
self.level += 1
if self.level == 0:
self.tank = self.tank_L0_image
if self.level == 1:
self.tank = self.tank_L1_image
if self.level == 2:
self.tank = self.tank_L2_image
if self.level == 3:
self.tank = self.tank_L2_image
def levelDown(self):
if self.level > 0:
self.level -= 1
if self.level == 0:
self.tank = self.tank_L0_image
self.bullet.speed = 6
self.bullet.strong = False
if self.level == 1:
self.tank = self.tank_L1_image
if self.level == 2:
self.tank = self.tank_L2_image
# ����True ����������ײ
def moveUp(self, tankGroup, brickGroup, ironGroup):
self.rect = self.rect.move(self.speed * 0, self.speed * -1)
self.tank_R0 = self.tank.subsurface((0, 0),(48, 48))
self.tank_R1 = self.tank.subsurface((48, 0),(48, 48))
self.dir_x, self.dir_y = 0, -1
if self.rect.top < 3:
self.rect = self.rect.move(self.speed * 0, self.speed * 1)
return True
if pygame.sprite.spritecollide(self, brickGroup, False, None) \
or pygame.sprite.spritecollide(self, ironGroup, False, None):
self.rect = self.rect.move(self.speed * 0, self.speed * 1)
return True
if pygame.sprite.spritecollide(self, tankGroup, False, None):
self.rect = self.rect.move(self.speed * 0, self.speed * 1)
return True
return False
def moveDown(self, tankGroup, brickGroup, ironGroup):
self.rect = self.rect.move(self.speed * 0, self.speed * 1)
self.tank_R0 = self.tank.subsurface((0, 48),(48, 48))
self.tank_R1 = self.tank.subsurface((48, 48),(48, 48))
self.dir_x, self.dir_y = 0, 1
if self.rect.bottom > 630 - 3:
self.rect = self.rect.move(self.speed * 0, self.speed * -1)
return True
if pygame.sprite.spritecollide(self, brickGroup, False, None) \
or pygame.sprite.spritecollide(self, ironGroup, False, None):
self.rect = self.rect.move(self.speed * 0, self.speed * -1)
return True
if pygame.sprite.spritecollide(self, tankGroup, False, None):
self.rect = self.rect.move(self.speed * 0, self.speed * -1)
return True
return False
def moveLeft(self, tankGroup, brickGroup, ironGroup):
self.rect = self.rect.move(self.speed * -1, self.speed * 0)
self.tank_R0 = self.tank.subsurface((0, 96),(48, 48))
self.tank_R1 = self.tank.subsurface((48, 96),(48, 48))
self.dir_x, self.dir_y = -1, 0
if self.rect.left < 3:
self.rect = self.rect.move(self.speed * 1, self.speed * 0)
return True
if pygame.sprite.spritecollide(self, brickGroup, False, None) \
or pygame.sprite.spritecollide(self, ironGroup, False, None):
self.rect = self.rect.move(self.speed * 1, self.speed * 0)
return True
if pygame.sprite.spritecollide(self, tankGroup, False, None):
self.rect = self.rect.move(self.speed * 1, self.speed * 0)
return True
return False
def moveRight(self, tankGroup, brickGroup, ironGroup):
self.rect = self.rect.move(self.speed * 1, self.speed * 0)
self.tank_R0 = self.tank.subsurface((0, 144),(48, 48))
self.tank_R1 = self.tank.subsurface((48, 144),(48, 48))
self.dir_x, self.dir_y = 1, 0
if self.rect.right > 630 - 3:
self.rect = self.rect.move(self.speed * -1, self.speed * 0)
return True
if pygame.sprite.spritecollide(self, brickGroup, False, None) \
or pygame.sprite.spritecollide(self, ironGroup, False, None):
self.rect = self.rect.move(self.speed * -1, self.speed * 0)
return True
if pygame.sprite.spritecollide(self, tankGroup, False, None):
self.rect = self.rect.move(self.speed * -1, self.speed * 0)
return True
return False
实现子弹类:子弹类需要处理子弹的生成、移动轨迹和碰撞检测。子弹可以有多种类型,例如穿甲弹或爆炸弹,每种类型都有不同的伤害和效果。
"""
功能:实现爆炸效果
"""
import pygame
from pygame import image
# from litao.GameRun import MainGame
class Boom:
def __init__(self, tank):
self.rect = tank.rect
self.step = 0
self.images = [
image.load('./image/bomb_0.png'),
image.load('./image/bomb_1.png'),
image.load('./image/bomb_2.png'),
image.load('./image/bomb_3.png')
]
self.image = self.images[self.step]
self.live = True
# 展示爆炸效果
def displayBoom(self, screen):
if self.step < len(self.images):
screen.blit(self.image, self.rect)
self.image = self.images[self.step]
self.step += 1
else:
self.live = False
self.step = 0
设计敌方AI:敌方AI应该具有一定的智能,能够根据玩家的位置进行追击或规避。可以通过状态机或行为树来实现复杂的AI行为。
创建障碍物:障碍物不仅可以阻挡子弹和坦克的移动,还可以作为地图上的战略要点。障碍物的设计应该与游戏地图的风格和主题保持一致。
wall.py
import pygame
brickImage = r"..\image\brick.png"
ironImage = r"..\image\iron.png"
class Brick(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(brickImage)
self.rect = self.image.get_rect()
class Iron(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(ironImage)
self.rect = self.image.get_rect()
class Map():
def __init__(self):
self.brickGroup = pygame.sprite.Group()
self.ironGroup = pygame.sprite.Group()
# 数字代表地图中的位置
# 画砖块
X1379 = [2, 3, 6, 7, 18, 19, 22, 23]
Y1379 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23]
X28 = [10, 11, 14, 15]
Y28 = [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20]
X46 = [4, 5, 6, 7, 18, 19, 20, 21]
Y46 = [13, 14]
X5 = [12, 13]
Y5 = [16, 17]
X0Y0 = [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]
for x in X1379:
for y in Y1379:
self.brick = Brick()
self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
self.brickGroup.add(self.brick)
for x in X28:
for y in Y28:
self.brick = Brick()
self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
self.brickGroup.add(self.brick)
for x in X46:
for y in Y46:
self.brick = Brick()
self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
self.brickGroup.add(self.brick)
for x in X5:
for y in Y5:
self.brick = Brick()
self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
self.brickGroup.add(self.brick)
for x, y in X0Y0:
self.brick = Brick()
self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
self.brickGroup.add(self.brick)
# 画石头
for x, y in [(0,14),(1,14),(12,6),(13,6),(12,7),(13,7),(24,14),(25,14)]:
self.iron = Iron()
self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24
self.ironGroup.add(self.iron)
音效与图形
添加音效:音效可以增强游戏的沉浸感。为射击、爆炸、坦克移动等关键事件添加音效,使游戏更加生动有趣。
"""
功能:播放音乐
"""
import pygame
from pygame import mixer
# 我方坦克出生
class Music:
def __init__(self, fileName):
self.fileName = fileName
# 先初始化混合器
mixer.init()
mixer.music.load(self.fileName)
# 开始播放音乐
def play(self):
mixer.music.play()
设计图形资源:图形资源是游戏的视觉表现。设计高质量的坦克、子弹和障碍物图像,以及动画效果,可以使游戏更加吸引人。
主函数(游戏逻辑)
实现游戏界面
设计菜单界面:菜单界面应该直观易用,提供游戏开始、设置、排行榜和退出等功能。良好的用户界面设计可以提升玩家的游戏体验。
游戏得分显示:得分板应该实时更新玩家的得分和生命值。此外,还可以显示当前关卡、剩余时间和特殊成就等信息。
游戏结束逻辑:当玩家的基地被摧毁或所有敌方坦克被消灭时,游戏结束。根据玩家的表现,可以提供重新开始或进入下一关卡的选项。
# -*- coding: utf-8 -*-
import pygame
import sys
import traceback
import wall
import myTank
import enemyTank
import food
def main():
pygame.init()
pygame.mixer.init()
resolution = 630, 630
screen = pygame.display.set_mode(resolution)
pygame.display.set_caption("Tank War ")
# 加载图片,音乐,音效.
background_image = pygame.image.load(r"..\image\background.png")
home_image = pygame.image.load(r"..\image\home.png")
home_destroyed_image = pygame.image.load(r"..\image\home_destroyed.png")
bang_sound = pygame.mixer.Sound(r"..\music\bang.wav")
bang_sound.set_volume(1)
fire_sound = pygame.mixer.Sound(r"..\music\Gunfire.wav")
start_sound = pygame.mixer.Sound(r"..\music\start.wav")
start_sound.play()
# 定义精灵组:坦克,我方坦克,敌方坦克,敌方子弹
allTankGroup = pygame.sprite.Group()
mytankGroup = pygame.sprite.Group()
allEnemyGroup = pygame.sprite.Group()
redEnemyGroup = pygame.sprite.Group()
greenEnemyGroup = pygame.sprite.Group()
otherEnemyGroup = pygame.sprite.Group()
enemyBulletGroup = pygame.sprite.Group()
# 创建地图
bgMap = wall.Map()
# 创建食物/道具 但不显示
prop = food.Food()
# 创建我方坦克
myTank_T1 = myTank.MyTank(1)
allTankGroup.add(myTank_T1)
mytankGroup.add(myTank_T1)
myTank_T2 = myTank.MyTank(2)
allTankGroup.add(myTank_T2)
mytankGroup.add(myTank_T2)
# 创建敌方 坦克
for i in range(1, 4):
enemy = enemyTank.EnemyTank(i)
allTankGroup.add(enemy)
allEnemyGroup.add(enemy)
if enemy.isred == True:
redEnemyGroup.add(enemy)
continue
if enemy.kind == 3:
greenEnemyGroup.add(enemy)
continue
otherEnemyGroup.add(enemy)
# 敌军坦克出现动画
appearance_image = pygame.image.load(r"..\image\appear.png").convert_alpha()
appearance = []
appearance.append(appearance_image.subsurface(( 0, 0), (48, 48)))
appearance.append(appearance_image.subsurface((48, 0), (48, 48)))
appearance.append(appearance_image.subsurface((96, 0), (48, 48)))
# 自定义事件
# 创建敌方坦克延迟200
DELAYEVENT = pygame.constants.USEREVENT
pygame.time.set_timer(DELAYEVENT, 200)
# 创建 敌方 子弹延迟1000
ENEMYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 1
pygame.time.set_timer(ENEMYBULLETNOTCOOLINGEVENT, 1000)
# 创建 我方 子弹延迟200
MYBULLETNOTCOOLINGEVENT = pygame.constants.USEREVENT + 2
pygame.time.set_timer(MYBULLETNOTCOOLINGEVENT, 200)
# 敌方坦克 静止8000
NOTMOVEEVENT = pygame.constants.USEREVENT + 3
pygame.time.set_timer(NOTMOVEEVENT, 8000)
delay = 100
moving = 0
movdir = 0
moving2 = 0
movdir2 = 0
enemyNumber = 3
enemyCouldMove = True
switch_R1_R2_image = True
homeSurvive = True
running_T1 = True
running_T2 = True
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 我方子弹冷却事件
if event.type == MYBULLETNOTCOOLINGEVENT:
myTank_T1.bulletNotCooling = True
# 敌方子弹冷却事件
if event.type == ENEMYBULLETNOTCOOLINGEVENT:
for each in allEnemyGroup:
each.bulletNotCooling = True
# 敌方坦克静止事件
if event.type == NOTMOVEEVENT:
enemyCouldMove = True
# 创建敌方坦克延迟
if event.type == DELAYEVENT:
if enemyNumber < 4:
enemy = enemyTank.EnemyTank()
if pygame.sprite.spritecollide(enemy, allTankGroup, False, None):
break
allEnemyGroup.add(enemy)
allTankGroup.add(enemy)
enemyNumber += 1
if enemy.isred == True:
redEnemyGroup.add(enemy)
elif enemy.kind == 3:
greenEnemyGroup.add(enemy)
else:
otherEnemyGroup.add(enemy)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c and pygame.KMOD_CTRL:
pygame.quit()
sys.exit()
if event.key == pygame.K_e:
myTank_T1.levelUp()
if event.key == pygame.K_q:
myTank_T1.levelDown()
if event.key == pygame.K_3:
myTank_T1.levelUp()
myTank_T1.levelUp()
myTank_T1.level = 3
if event.key == pygame.K_2:
if myTank_T1.speed == 3:
myTank_T1.speed = 24
else:
myTank_T1.speed = 3
if event.key == pygame.K_1:
for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
bgMap.brick = wall.Brick()
bgMap.brick.rect.left, bgMap.brick.rect.top = 3 + x * 24, 3 + y * 24
bgMap.brickGroup.add(bgMap.brick)
if event.key == pygame.K_4:
for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
bgMap.iron = wall.Iron()
bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
bgMap.ironGroup.add(bgMap.iron)
# 检查用户的键盘操作
key_pressed = pygame.key.get_pressed()
# 玩家一的移动操作
if moving:
moving -= 1
if movdir == 0:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if movdir == 1:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if movdir == 2:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if movdir == 3:
allTankGroup.remove(myTank_T1)
if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving += 1
allTankGroup.add(myTank_T1)
running_T1 = True
if not moving:
if key_pressed[pygame.K_w]:
moving = 7
movdir = 0
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
elif key_pressed[pygame.K_s]:
moving = 7
movdir = 1
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
elif key_pressed[pygame.K_a]:
moving = 7
movdir = 2
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
elif key_pressed[pygame.K_d]:
moving = 7
movdir = 3
running_T1 = True
allTankGroup.remove(myTank_T1)
if myTank_T1.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup):
moving = 0
allTankGroup.add(myTank_T1)
if key_pressed[pygame.K_j]:
if not myTank_T1.bullet.life and myTank_T1.bulletNotCooling:
fire_sound.play()
myTank_T1.shoot()
myTank_T1.bulletNotCooling = False
# 玩家二的移动操作
if moving2:
moving2 -= 1
if movdir2 == 0:
allTankGroup.remove(myTank_T2)
myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if movdir2 == 1:
allTankGroup.remove(myTank_T2)
myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if movdir2 == 2:
allTankGroup.remove(myTank_T2)
myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if movdir2 == 3:
allTankGroup.remove(myTank_T2)
myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
running_T2 = True
if not moving2:
if key_pressed[pygame.K_UP]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveUp(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 0
running_T2 = True
elif key_pressed[pygame.K_DOWN]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveDown(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 1
running_T2 = True
elif key_pressed[pygame.K_LEFT]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveLeft(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 2
running_T2 = True
elif key_pressed[pygame.K_RIGHT]:
allTankGroup.remove(myTank_T2)
myTank_T2.moveRight(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(myTank_T2)
moving2 = 7
movdir2 = 3
running_T2 = True
if key_pressed[pygame.K_KP0]:
if not myTank_T2.bullet.life:
# fire_sound.play()
myTank_T2.shoot()
# 画背景
screen.blit(background_image, (0, 0))
# 画砖块
for each in bgMap.brickGroup:
screen.blit(each.image, each.rect)
# 花石头
for each in bgMap.ironGroup:
screen.blit(each.image, each.rect)
# 画home
if homeSurvive:
screen.blit(home_image, (3 + 12 * 24, 3 + 24 * 24))
else:
screen.blit(home_destroyed_image, (3 + 12 * 24, 3 + 24 * 24))
# 画我方坦克1
if not (delay % 5):
switch_R1_R2_image = not switch_R1_R2_image
if switch_R1_R2_image and running_T1:
screen.blit(myTank_T1.tank_R0, (myTank_T1.rect.left, myTank_T1.rect.top))
running_T1 = False
else:
screen.blit(myTank_T1.tank_R1, (myTank_T1.rect.left, myTank_T1.rect.top))
# 画我方坦克2
if switch_R1_R2_image and running_T2:
screen.blit(myTank_T2.tank_R0, (myTank_T2.rect.left, myTank_T2.rect.top))
running_T2 = False
else:
screen.blit(myTank_T2.tank_R1, (myTank_T2.rect.left, myTank_T2.rect.top))
# 画敌方坦克
for each in allEnemyGroup:
# 判断5毛钱特效是否播放
if each.flash:
# 判断画左动作还是右动作
if switch_R1_R2_image:
screen.blit(each.tank_R0, (each.rect.left, each.rect.top))
if enemyCouldMove:
allTankGroup.remove(each)
each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(each)
else:
screen.blit(each.tank_R1, (each.rect.left, each.rect.top))
if enemyCouldMove:
allTankGroup.remove(each)
each.move(allTankGroup, bgMap.brickGroup, bgMap.ironGroup)
allTankGroup.add(each)
else:
# 播放5毛钱特效
if each.times > 0:
each.times -= 1
if each.times <= 10:
screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 20:
screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 30:
screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
elif each.times <= 40:
screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 50:
screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 60:
screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
elif each.times <= 70:
screen.blit(appearance[2], (3 + each.x * 12 * 24, 3))
elif each.times <= 80:
screen.blit(appearance[1], (3 + each.x * 12 * 24, 3))
elif each.times <= 90:
screen.blit(appearance[0], (3 + each.x * 12 * 24, 3))
if each.times == 0:
each.flash = True
# 绘制我方子弹1
if myTank_T1.bullet.life:
myTank_T1.bullet.move()
screen.blit(myTank_T1.bullet.bullet, myTank_T1.bullet.rect)
# 子弹 碰撞 子弹
for each in enemyBulletGroup:
if each.life:
if pygame.sprite.collide_rect(myTank_T1.bullet, each):
myTank_T1.bullet.life = False
each.life = False
pygame.sprite.spritecollide(myTank_T1.bullet, enemyBulletGroup, True, None)
# 子弹 碰撞 敌方坦克
if pygame.sprite.spritecollide(myTank_T1.bullet, redEnemyGroup, True, None):
prop.change()
bang_sound.play()
enemyNumber -= 1
myTank_T1.bullet.life = False
elif pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, False, None):
for each in greenEnemyGroup:
if pygame.sprite.collide_rect(myTank_T1.bullet, each):
if each.life == 1:
pygame.sprite.spritecollide(myTank_T1.bullet,greenEnemyGroup, True, None)
bang_sound.play()
enemyNumber -= 1
elif each.life == 2:
each.life -= 1
each.tank = each.enemy_3_0
elif each.life == 3:
each.life -= 1
each.tank = each.enemy_3_2
myTank_T1.bullet.life = False
elif pygame.sprite.spritecollide(myTank_T1.bullet, otherEnemyGroup, True, None):
bang_sound.play()
enemyNumber -= 1
myTank_T1.bullet.life = False
#if pygame.sprite.spritecollide(myTank_T1.bullet, allEnemyGroup, True, None):
# bang_sound.play()
# enemyNumber -= 1
# myTank_T1.bullet.life = False
# 子弹 碰撞 brickGroup
if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.brickGroup, True, None):
myTank_T1.bullet.life = False
myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 子弹 碰撞 brickGroup
if myTank_T1.bullet.strong:
if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, True, None):
myTank_T1.bullet.life = False
myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
else:
if pygame.sprite.spritecollide(myTank_T1.bullet, bgMap.ironGroup, False, None):
myTank_T1.bullet.life = False
myTank_T1.bullet.rect.left, myTank_T1.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 绘制我方子弹2
if myTank_T2.bullet.life:
myTank_T2.bullet.move()
screen.blit(myTank_T2.bullet.bullet, myTank_T2.bullet.rect)
# 子弹 碰撞 敌方坦克
if pygame.sprite.spritecollide(myTank_T2.bullet, allEnemyGroup, True, None):
bang_sound.play()
enemyNumber -= 1
myTank_T2.bullet.life = False
# 子弹 碰撞 brickGroup
if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.brickGroup, True, None):
myTank_T2.bullet.life = False
myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 子弹 碰撞 brickGroup
if myTank_T2.bullet.strong:
if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, True, None):
myTank_T2.bullet.life = False
myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
else:
if pygame.sprite.spritecollide(myTank_T2.bullet, bgMap.ironGroup, False, None):
myTank_T2.bullet.life = False
myTank_T2.bullet.rect.left, myTank_T2.bullet.rect.right = 3 + 12 * 24, 3 + 24 * 24
# 绘制敌人子弹
for each in allEnemyGroup:
# 如果子弹没有生命,则赋予子弹生命
if not each.bullet.life and each.bulletNotCooling and enemyCouldMove:
enemyBulletGroup.remove(each.bullet)
each.shoot()
enemyBulletGroup.add(each.bullet)
each.bulletNotCooling = False
# 如果5毛钱特效播放完毕 并且 子弹存活 则绘制敌方子弹
if each.flash:
if each.bullet.life:
# 如果敌人可以移动
if enemyCouldMove:
each.bullet.move()
screen.blit(each.bullet.bullet, each.bullet.rect)
# 子弹 碰撞 我方坦克
if pygame.sprite.collide_rect(each.bullet, myTank_T1):
bang_sound.play()
myTank_T1.rect.left, myTank_T1.rect.top = 3 + 8 * 24, 3 + 24 * 24
each.bullet.life = False
moving = 0 # 重置移动控制参数
for i in range(myTank_T1.level+1):
myTank_T1.levelDown()
if pygame.sprite.collide_rect(each.bullet, myTank_T2):
bang_sound.play()
myTank_T2.rect.left, myTank_T2.rect.top = 3 + 16 * 24, 3 + 24 * 24
each.bullet.life = False
# 子弹 碰撞 brickGroup
if pygame.sprite.spritecollide(each.bullet, bgMap.brickGroup, True, None):
each.bullet.life = False
# 子弹 碰撞 ironGroup
if each.bullet.strong:
if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, True, None):
each.bullet.life = False
else:
if pygame.sprite.spritecollide(each.bullet, bgMap.ironGroup, False, None):
each.bullet.life = False
# 最后画食物/道具
if prop.life:
screen.blit(prop.image, prop.rect)
# 我方坦克碰撞 食物/道具
if pygame.sprite.collide_rect(myTank_T1, prop):
if prop.kind == 1: # 敌人全毁
for each in allEnemyGroup:
if pygame.sprite.spritecollide(each, allEnemyGroup, True, None):
bang_sound.play()
enemyNumber -= 1
prop.life = False
if prop.kind == 2: # 敌人静止
enemyCouldMove = False
prop.life = False
if prop.kind == 3: # 子弹增强
myTank_T1.bullet.strong = True
prop.life = False
if prop.kind == 4: # 家得到保护
for x, y in [(11,23),(12,23),(13,23),(14,23),(11,24),(14,24),(11,25),(14,25)]:
bgMap.iron = wall.Iron()
bgMap.iron.rect.left, bgMap.iron.rect.top = 3 + x * 24, 3 + y * 24
bgMap.ironGroup.add(bgMap.iron)
prop.life = False
if prop.kind == 5: # 坦克无敌
prop.life = False
pass
if prop.kind == 6: # 坦克升级
myTank_T1.levelUp()
prop.life = False
if prop.kind == 7: # 坦克生命+1
myTank_T1.life += 1
prop.life = False
# 延迟
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()
写在最后:
本期github优秀小游戏项目分享到这里就结束啦。感谢大家的耐心阅读,如果你对这些项目感兴趣,或者从中获得了灵感和乐趣,不妨通过点赞、关注和收藏来支持我们。你的每一个互动都是对我们最大的鼓励!
同时,如果你对我们的专栏感兴趣,不要忘记订阅哦!我会定期更新,带来更多有趣、实用的开源项目分享。无论是游戏开发新手还是资深玩家,相信都能在这里找到值得一探的宝藏。
期待着与你一起探索游戏世界的无限可能,让我们下期再见!
开源地址:https://github.com/wangxingyao/TankWar/tree/master