1 Star 0 Fork 0

晗先生/TankWar

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
myTank.py 7.96 KB
一键复制 编辑 原始数据 按行查看 历史
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)
# TODO 这里存在两个一样的属性
self.life = True
self.live = 3
if playerNumber == 1: # 玩家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: # 玩家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()
# 根据当前的级别 level 来选择当前使用的坦克图像
self.level = 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.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.life = True
# 调用子弹对象的 changeImage() 方法来根据坦克的移动方向改变子弹的图像
self.bullet.changeImage(self.dir_x, self.dir_y)
"""
根据坦克的移动方向设置子弹的初始位置
例如:dir_x 为0且 dir_y 为-1,说明坦克向上移动,子弹位于坦克的正上方
"""
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
"""
坦克向上移动,并处理于墙壁、铁块和其它坦克的碰撞检测
"""
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lover_programmer/tank-war.git
[email protected]:lover_programmer/tank-war.git
lover_programmer
tank-war
TankWar
master

搜索帮助