!!!末尾附完整代码仓库!!!
由于找不到很多坦克图片,部分是自个ps画的…(略丑见谅)
敌方蓝色坦克3血,粉色2血,绿色1血,会根据坦克血量而变换颜色
我方俩坦克均2血,在坦克机体显示血条
1、实现双人模式。玩家有血条显示。
2、有三款不同颜色血量的敌机,随机时间随机地点出现在地图内。
for i in range(Settings.ENEMY_COUNT):
direction = random.randint(0, 3)
enemy = Enemy(Settings.ENEMY_IMAGES[direction], screen, out_time=random.randint(0, 15), hp=1)
enemy.direction = direction
enemies.add(enemy)
for i in range(Settings.ENEMY_COUNT2):
direction = random.randint(0, 3)
enemy = Enemy(Settings.ENEMY_IMAGES2[direction], screen, out_time=random.randint(10, 25), hp=2)
enemy.direction = direction
enemies.add(enemy)
for i in range(Settings.ENEMY_COUNT3):
direction = random.randint(0, 3)
enemy = Enemy(Settings.ENEMY_IMAGES3[direction], screen, out_time=random.randint(15, 25), hp=3)
enemy.direction = direction
enemies.add(enemy)
3、摧毁敌机有几率掉落加速道具,
摧毁墙体有几率掉落回血道具。
#碰撞道具
for prop in props1:
if pygame.sprite.collide_rect(player, prop):
prop.kill()
player.speed += 1
player.temp = player.speed
# print("i am temp"+ str(player.temp))
if pygame.sprite.collide_rect(player2, prop):
prop.kill()
player2.speed += 1
player2.temp = player2.speed
# print("i am temp" + str(player2.temp))
for prop in props2:
if pygame.sprite.collide_rect(player, prop):
prop.kill()
player.hp = 2
if pygame.sprite.collide_rect(player2, prop):
prop.kill()
player2.hp = 2
4、玩家经过河流发生减速
# 我方坦克撞墙
if pygame.sprite.collide_rect(player, wall):
# 不可穿越墙
if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.home_WALL:
player.is_hit_wall = True
# 移出墙内
player.move_out_wall(wall)
elif wall.type == Settings.WATER_WALL:
player.is_hit_wall = False
t0 = clock.get_time()
player.water(t0)
if player.rect.right > wall.rect.left and player.rect.bottom > wall.rect.top and\
player.rect.left > wall.rect.right and player.rect.top > wall.rect.bottom:
player.water(2000)
def water(self, t):
self.water_time = t
if self.water_time/1000 < 1:
self.speed = 1
else:
self.speed = self.temp
码云开源仓库:https://gitee.com/a502314638/pygame-tank-battle
谢谢点赞🌹