pygame之坦克大战(三)
程序员文章站
2024-02-20 10:41:46
...
我方坦克类(MyTank)
# -*- coding: utf-8 -*-
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"
# 继承Sprite基类
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()
# 初始坦克为0级
self.level = 0
self.tank = self.tank_L0_image
# 运动中的两种图片,表现为坦克履带的滚动
# 使用subsurface获取图像子表面
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 # 一共有3次生命
self.bulletNotCooling = True # 子弹是否在冷却中
self.bullet = bulletClass.Bullet()
# 射击
def shoot(self):
# 子弹
self.bullet.life = True
# 传入子弹初始朝向
self.bullet.changeImage(self.dir_x, self.dir_y)
# 绘制向上子弹,子弹左边界等于坦克图片左边界+20,
# 下边界等于坦克上边界+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 == 1:
self.tank = self.tank_L1_image
if self.level == 2:
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.bullet.speed = 16
self.tank = self.tank_L1_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
上一篇: linuxshell脚本编程
推荐阅读
-
pygame之坦克大战(三)
-
cocos2d-x游戏开发系列教程-坦克大战游戏之坦克的显示
-
cocos2d x游戏开发系列教程 坦克大战游戏之坦克和地图碰撞的检测上
-
cocos2d x游戏开发系列教程 坦克大战游戏之虚拟手柄控制坦克移动
-
python之pygame模块实现飞机大战完整代码
-
用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
-
python使用pygame模块实现坦克大战游戏
-
用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
-
python使用pygame模块实现坦克大战游戏
-
python+pygame实现坦克大战小游戏的示例代码(可以自定义子弹速度)