diff --git a/image/TankWar.png b/image/TankWar.png new file mode 100644 index 0000000..3829e58 Binary files /dev/null and b/image/TankWar.png differ diff --git a/image/appear.png b/image/appear.png new file mode 100644 index 0000000..9bf662b Binary files /dev/null and b/image/appear.png differ diff --git a/image/background.png b/image/background.png new file mode 100644 index 0000000..de250c2 Binary files /dev/null and b/image/background.png differ diff --git a/image/boom_dynamic.png b/image/boom_dynamic.png new file mode 100644 index 0000000..d7be90d Binary files /dev/null and b/image/boom_dynamic.png differ diff --git a/image/boom_static.png b/image/boom_static.png new file mode 100644 index 0000000..c3a4c82 Binary files /dev/null and b/image/boom_static.png differ diff --git a/image/brick.png b/image/brick.png new file mode 100644 index 0000000..f3a2b82 Binary files /dev/null and b/image/brick.png differ diff --git a/image/bullet_down.png b/image/bullet_down.png new file mode 100644 index 0000000..b0146fa Binary files /dev/null and b/image/bullet_down.png differ diff --git a/image/bullet_left.png b/image/bullet_left.png new file mode 100644 index 0000000..ae34409 Binary files /dev/null and b/image/bullet_left.png differ diff --git a/image/bullet_right.png b/image/bullet_right.png new file mode 100644 index 0000000..7dbaa39 Binary files /dev/null and b/image/bullet_right.png differ diff --git a/image/bullet_up.png b/image/bullet_up.png new file mode 100644 index 0000000..acc5e41 Binary files /dev/null and b/image/bullet_up.png differ diff --git a/image/enemy_1_0.png b/image/enemy_1_0.png new file mode 100644 index 0000000..eb55ca3 Binary files /dev/null and b/image/enemy_1_0.png differ diff --git a/image/enemy_1_1.png b/image/enemy_1_1.png new file mode 100644 index 0000000..99f238d Binary files /dev/null and b/image/enemy_1_1.png differ diff --git a/image/enemy_1_2.png b/image/enemy_1_2.png new file mode 100644 index 0000000..6700c2c Binary files /dev/null and b/image/enemy_1_2.png differ diff --git a/image/enemy_1_3.png b/image/enemy_1_3.png new file mode 100644 index 0000000..8bdfa76 Binary files /dev/null and b/image/enemy_1_3.png differ diff --git a/image/enemy_2_0.png b/image/enemy_2_0.png new file mode 100644 index 0000000..27a0c47 Binary files /dev/null and b/image/enemy_2_0.png differ diff --git a/image/enemy_2_1.png b/image/enemy_2_1.png new file mode 100644 index 0000000..93d42b2 Binary files /dev/null and b/image/enemy_2_1.png differ diff --git a/image/enemy_2_2.png b/image/enemy_2_2.png new file mode 100644 index 0000000..4277b89 Binary files /dev/null and b/image/enemy_2_2.png differ diff --git a/image/enemy_2_3.png b/image/enemy_2_3.png new file mode 100644 index 0000000..fb33e6b Binary files /dev/null and b/image/enemy_2_3.png differ diff --git a/image/enemy_3_0.png b/image/enemy_3_0.png new file mode 100644 index 0000000..0e9416f Binary files /dev/null and b/image/enemy_3_0.png differ diff --git a/image/enemy_3_1.png b/image/enemy_3_1.png new file mode 100644 index 0000000..c4eaffc Binary files /dev/null and b/image/enemy_3_1.png differ diff --git a/image/enemy_3_2.png b/image/enemy_3_2.png new file mode 100644 index 0000000..0dde02f Binary files /dev/null and b/image/enemy_3_2.png differ diff --git a/image/enemy_3_3.png b/image/enemy_3_3.png new file mode 100644 index 0000000..1d6dd0e Binary files /dev/null and b/image/enemy_3_3.png differ diff --git a/image/enemy_4_0.png b/image/enemy_4_0.png new file mode 100644 index 0000000..f549767 Binary files /dev/null and b/image/enemy_4_0.png differ diff --git a/image/enemy_4_1.png b/image/enemy_4_1.png new file mode 100644 index 0000000..88dea16 Binary files /dev/null and b/image/enemy_4_1.png differ diff --git a/image/enemy_4_2.png b/image/enemy_4_2.png new file mode 100644 index 0000000..1141c37 Binary files /dev/null and b/image/enemy_4_2.png differ diff --git a/image/enemy_4_3.png b/image/enemy_4_3.png new file mode 100644 index 0000000..0fc5dbb Binary files /dev/null and b/image/enemy_4_3.png differ diff --git a/image/food_boom.png b/image/food_boom.png new file mode 100644 index 0000000..8c6c601 Binary files /dev/null and b/image/food_boom.png differ diff --git a/image/food_clock.png b/image/food_clock.png new file mode 100644 index 0000000..13c84db Binary files /dev/null and b/image/food_clock.png differ diff --git a/image/food_gun.png b/image/food_gun.png new file mode 100644 index 0000000..b2fbf14 Binary files /dev/null and b/image/food_gun.png differ diff --git a/image/food_iron.png b/image/food_iron.png new file mode 100644 index 0000000..ae7a0b8 Binary files /dev/null and b/image/food_iron.png differ diff --git a/image/food_protect.png b/image/food_protect.png new file mode 100644 index 0000000..e026096 Binary files /dev/null and b/image/food_protect.png differ diff --git a/image/food_star.png b/image/food_star.png new file mode 100644 index 0000000..ae927a6 Binary files /dev/null and b/image/food_star.png differ diff --git a/image/food_tank.png b/image/food_tank.png new file mode 100644 index 0000000..ab7172c Binary files /dev/null and b/image/food_tank.png differ diff --git a/image/gameover.png b/image/gameover.png new file mode 100644 index 0000000..ae4148b Binary files /dev/null and b/image/gameover.png differ diff --git a/image/home.png b/image/home.png new file mode 100644 index 0000000..f61346a Binary files /dev/null and b/image/home.png differ diff --git a/image/home1.png b/image/home1.png new file mode 100644 index 0000000..008528a Binary files /dev/null and b/image/home1.png differ diff --git a/image/home_destroyed.png b/image/home_destroyed.png new file mode 100644 index 0000000..95e8d73 Binary files /dev/null and b/image/home_destroyed.png differ diff --git a/image/ice.png b/image/ice.png new file mode 100644 index 0000000..89e6917 Binary files /dev/null and b/image/ice.png differ diff --git a/image/iron.png b/image/iron.png new file mode 100644 index 0000000..fc57588 Binary files /dev/null and b/image/iron.png differ diff --git a/image/logo.png b/image/logo.png new file mode 100644 index 0000000..7d0091c Binary files /dev/null and b/image/logo.png differ diff --git a/image/mask.png b/image/mask.png new file mode 100644 index 0000000..e13c818 Binary files /dev/null and b/image/mask.png differ diff --git a/image/protect.png b/image/protect.png new file mode 100644 index 0000000..81ad51e Binary files /dev/null and b/image/protect.png differ diff --git a/image/river1.png b/image/river1.png new file mode 100644 index 0000000..ed71af6 Binary files /dev/null and b/image/river1.png differ diff --git a/image/river2.png b/image/river2.png new file mode 100644 index 0000000..0a92a4e Binary files /dev/null and b/image/river2.png differ diff --git a/image/tank_T1_0.png b/image/tank_T1_0.png new file mode 100644 index 0000000..0dda565 Binary files /dev/null and b/image/tank_T1_0.png differ diff --git a/image/tank_T1_1.png b/image/tank_T1_1.png new file mode 100644 index 0000000..e1c3628 Binary files /dev/null and b/image/tank_T1_1.png differ diff --git a/image/tank_T1_2.png b/image/tank_T1_2.png new file mode 100644 index 0000000..070db20 Binary files /dev/null and b/image/tank_T1_2.png differ diff --git a/image/tank_T2_0.png b/image/tank_T2_0.png new file mode 100644 index 0000000..7a68721 Binary files /dev/null and b/image/tank_T2_0.png differ diff --git a/image/tank_T2_1.png b/image/tank_T2_1.png new file mode 100644 index 0000000..7346bd8 Binary files /dev/null and b/image/tank_T2_1.png differ diff --git a/image/tank_T2_2.png b/image/tank_T2_2.png new file mode 100644 index 0000000..0b37d82 Binary files /dev/null and b/image/tank_T2_2.png differ diff --git a/image/tip.png b/image/tip.png new file mode 100644 index 0000000..ca81fdc Binary files /dev/null and b/image/tip.png differ diff --git a/image/tree.png b/image/tree.png new file mode 100644 index 0000000..2069205 Binary files /dev/null and b/image/tree.png differ diff --git a/music/Gunfire.wav b/music/Gunfire.wav new file mode 100644 index 0000000..322b4c0 Binary files /dev/null and b/music/Gunfire.wav differ diff --git a/music/add.wav b/music/add.wav new file mode 100644 index 0000000..afc98ff Binary files /dev/null and b/music/add.wav differ diff --git a/music/bang.wav b/music/bang.wav new file mode 100644 index 0000000..ae1458d Binary files /dev/null and b/music/bang.wav differ diff --git a/music/blast.wav b/music/blast.wav new file mode 100644 index 0000000..09e7db7 Binary files /dev/null and b/music/blast.wav differ diff --git a/music/fire.wav b/music/fire.wav new file mode 100644 index 0000000..1c66edf Binary files /dev/null and b/music/fire.wav differ diff --git a/music/hit.wav b/music/hit.wav new file mode 100644 index 0000000..d124b6b Binary files /dev/null and b/music/hit.wav differ diff --git a/music/start.wav b/music/start.wav new file mode 100644 index 0000000..ba70772 Binary files /dev/null and b/music/start.wav differ diff --git a/src/__pycache__/bullet.cpython-34.pyc b/src/__pycache__/bullet.cpython-34.pyc new file mode 100644 index 0000000..149a951 Binary files /dev/null and b/src/__pycache__/bullet.cpython-34.pyc differ diff --git a/src/__pycache__/bulletClass.cpython-34.pyc b/src/__pycache__/bulletClass.cpython-34.pyc new file mode 100644 index 0000000..e556805 Binary files /dev/null and b/src/__pycache__/bulletClass.cpython-34.pyc differ diff --git a/src/__pycache__/enemyTank.cpython-34.pyc b/src/__pycache__/enemyTank.cpython-34.pyc new file mode 100644 index 0000000..5c76e5a Binary files /dev/null and b/src/__pycache__/enemyTank.cpython-34.pyc differ diff --git a/src/__pycache__/food.cpython-34.pyc b/src/__pycache__/food.cpython-34.pyc new file mode 100644 index 0000000..471f1f2 Binary files /dev/null and b/src/__pycache__/food.cpython-34.pyc differ diff --git a/src/__pycache__/myTank.cpython-34.pyc b/src/__pycache__/myTank.cpython-34.pyc new file mode 100644 index 0000000..2883d18 Binary files /dev/null and b/src/__pycache__/myTank.cpython-34.pyc differ diff --git a/src/__pycache__/wall.cpython-34.pyc b/src/__pycache__/wall.cpython-34.pyc new file mode 100644 index 0000000..07a904e Binary files /dev/null and b/src/__pycache__/wall.cpython-34.pyc differ diff --git a/src/bulletClass.py b/src/bulletClass.py new file mode 100644 index 0000000..8a9ab5b --- /dev/null +++ b/src/bulletClass.py @@ -0,0 +1,67 @@ +import pygame + + +class Bullet(pygame.sprite.Sprite): + def __init__(self): + pygame.sprite.Sprite.__init__(self) + + self.bullet_up = pygame.image.load(r"..\image\bullet_up.png") + self.bullet_down = pygame.image.load(r"..\image\bullet_down.png") + self.bullet_left = pygame.image.load(r"..\image\bullet_left.png") + self.bullet_right = pygame.image.load(r"..\image\bullet_right.png") + + # 子弹方向 速度 生命 碎石 + self.dir_x, self.dir_y = 0, 0 + self.speed = 6 + self.life = False + self.strong = False + + self.bullet = self.bullet_up + self.rect = self.bullet.get_rect() + self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 + + def changeImage(self, dir_x, dir_y): + self.dir_x, self.dir_y = dir_x, dir_y + if self.dir_x == 0 and self.dir_y == -1: + self.bullet = self.bullet_up + elif self.dir_x == 0 and self.dir_y == 1: + self.bullet = self.bullet_down + elif self.dir_x == -1 and self.dir_y == 0: + self.bullet = self.bullet_left + elif self.dir_x == 1 and self.dir_y == 0: + self.bullet = self.bullet_right + + + + def move(self): + self.rect = self.rect.move(self.speed * self.dir_x, + self.speed * self.dir_y) + + # 碰撞地图边缘 + if self.rect.top < 3: + self.life = False + # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 + if self.rect.bottom > 630 - 3: + self.life = False + # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 + if self.rect.left < 3: + self.life = False + # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 + if self.rect.right > 630 - 3: + self.life = False + # self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24 + + # 碰撞 brickGroup + #if pygame.sprite.spritecollide(self, brickGroup, True, None): + # self.life = False + # moving = 0 + # 碰撞 ironGroup + #if self.strong: + # if pygame.sprite.spritecollide(self, ironGroup, True, None): + # self.life = False + #else: + # if pygame.sprite.spritecollide(self, ironGroup, False, None): + # self.life = False + # moving = 0 + #return moving + \ No newline at end of file diff --git a/src/enemyTank.py b/src/enemyTank.py new file mode 100644 index 0000000..76b937a --- /dev/null +++ b/src/enemyTank.py @@ -0,0 +1,125 @@ +import pygame +import random +import bulletClass + + + +class EnemyTank(pygame.sprite.Sprite): + def __init__(self, x = None, kind = None, isred = None): + pygame.sprite.Sprite.__init__(self) + + # 坦克出现前动画是否播放 + self.flash = False + self.times = 90 + + # 参数:坦克种类 + self.kind = kind + if not kind: + self.kind = random.choice([1, 2, 3, 4]) + + # 选择敌军坦克种类 + if self.kind == 1: + self.enemy_x_0 = pygame.image.load(r"..\image\enemy_1_0.png").convert_alpha() + self.enemy_x_3 = pygame.image.load(r"..\image\enemy_1_3.png").convert_alpha() + if self.kind == 2: + self.enemy_x_0 = pygame.image.load(r"..\image\enemy_2_0.png").convert_alpha() + self.enemy_x_3 = pygame.image.load(r"..\image\enemy_2_3.png").convert_alpha() + if self.kind == 3: + self.enemy_x_0 = pygame.image.load(r"..\image\enemy_3_1.png").convert_alpha() + self.enemy_x_3 = pygame.image.load(r"..\image\enemy_3_0.png").convert_alpha() + if self.kind == 4: + self.enemy_x_0 = pygame.image.load(r"..\image\enemy_4_0.png").convert_alpha() + self.enemy_x_3 = pygame.image.load(r"..\image\enemy_4_3.png").convert_alpha() + self.enemy_3_0 = pygame.image.load(r"..\image\enemy_3_0.png").convert_alpha() + self.enemy_3_2 = pygame.image.load(r"..\image\enemy_3_2.png").convert_alpha() + + + # 参数:是否携带食物 + self.isred = isred + if not None: + self.isred = random.choice((True, False, False, False, False)) + if self.isred: + self.tank = self.enemy_x_3 + else: + self.tank = self.enemy_x_0 + # 参数:坦克位置 + self.x = x + if not self.x: + self.x = random.choice([1, 2, 3]) + self.x -= 1 + + # 运动中的两种图片 + self.tank_R0 = self.tank.subsurface(( 0, 48), (48, 48)) + self.tank_R1 = self.tank.subsurface((48, 48), (48, 48)) + self.rect = self.tank_R0.get_rect() + self.rect.left, self.rect.top = 3 + self.x * 12 * 24, 3 + 0 * 24 + + # 坦克速度 方向 生命 子弹生命 子弹延迟 + self.speed = 1 + self.dir_x, self.dir_y = 0, 1 + self.life = 1 + self.bulletNotCooling = True + self.bullet = bulletClass.Bullet() + # 是否撞墙,撞墙则改变方向 + self.dirChange = False + + # 每种坦克不同的属性 + if self.kind == 2: + self.speed = 3 + if self.kind == 3: + self.life = 3 + + 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 + + def move(self, tankGroup, brickGroup, ironGroup): + self.rect = self.rect.move(self.speed * self.dir_x, self.speed * self.dir_y) + + if self.dir_x == 0 and self.dir_y == -1: + self.tank_R0 = self.tank.subsurface(( 0, 0),(48, 48)) + self.tank_R1 = self.tank.subsurface((48, 0),(48, 48)) + elif self.dir_x == 0 and self.dir_y == 1: + self.tank_R0 = self.tank.subsurface(( 0, 48),(48, 48)) + self.tank_R1 = self.tank.subsurface((48, 48),(48, 48)) + elif self.dir_x == -1 and self.dir_y == 0: + self.tank_R0 = self.tank.subsurface(( 0, 96),(48, 48)) + self.tank_R1 = self.tank.subsurface((48, 96),(48, 48)) + elif self.dir_x == 1 and self.dir_y == 0: + self.tank_R0 = self.tank.subsurface(( 0, 144),(48, 48)) + self.tank_R1 = self.tank.subsurface((48, 144),(48, 48)) + + + # 碰撞地图边缘 + if self.rect.top < 3: + self.rect = self.rect.move(self.speed * 0, self.speed * 1) + self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) + elif self.rect.bottom > 630 - 3: + self.rect = self.rect.move(self.speed * 0, self.speed * -1) + self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) + elif self.rect.left < 3: + self.rect = self.rect.move(self.speed * 1, self.speed * 0) + self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) + elif self.rect.right > 630 - 3: + self.rect = self.rect.move(self.speed * -1, self.speed * 0) + self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) + # 碰撞墙体 和坦克 + if pygame.sprite.spritecollide(self, brickGroup, False, None) \ + or pygame.sprite.spritecollide(self, ironGroup, False, None) \ + or pygame.sprite.spritecollide(self, tankGroup, False, None): + self.rect = self.rect.move(self.speed * -self.dir_x, self.speed * -self.dir_y) + self.dir_x, self.dir_y = random.choice(([0,1],[0,-1],[1,0],[-1,0])) diff --git a/src/food.py b/src/food.py new file mode 100644 index 0000000..08ccee1 --- /dev/null +++ b/src/food.py @@ -0,0 +1,55 @@ +import pygame +import random + +class Food(pygame.sprite.Sprite): + def __init__(self): + + self.food_boom = pygame.image.load(r"..\image\food_boom.png").convert_alpha() + self.food_clock = pygame.image.load(r"..\image\food_clock.png").convert_alpha() + self.food_gun = pygame.image.load(r"..\image\food_gun.png").convert_alpha() + self.food_iron = pygame.image.load(r"..\image\food_iron.png").convert_alpha() + self.food_protect = pygame.image.load(r"..\image\food_protect.png").convert_alpha() + self.food_star = pygame.image.load(r"..\image\food_star.png").convert_alpha() + self.food_tank = pygame.image.load(r"..\image\food_tank.png").convert_alpha() + self.kind = random.choice([1, 2, 3, 4, 5, 6, 7]) + if self.kind == 1: + self.image = self.food_boom + elif self.kind == 2: + self.image = self.food_clock + elif self.kind == 3: + self.image = self.food_gun + elif self.kind == 4: + self.image = self.food_iron + elif self.kind == 5: + self.image = self.food_protect + elif self.kind == 6: + self.image = self.food_star + elif self.kind == 7: + self.image = self.food_tank + + self.rect = self.image.get_rect() + self.rect.left = self.rect.top = random.randint(100, 500) + + self.life = False + + def change(self): + self.kind = random.choice([1, 2, 3, 4, 5, 6, 7]) + if self.kind == 1: + self.image = self.food_boom + elif self.kind == 2: + self.image = self.food_clock + elif self.kind == 3: + self.image = self.food_gun + elif self.kind == 4: + self.image = self.food_iron + elif self.kind == 5: + self.image = self.food_protect + elif self.kind == 6: + self.image = self.food_star + elif self.kind == 7: + self.image = self.food_tank + + self.rect.left = self.rect.top = random.randint(100, 500) + self.life = True + + \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..e6c6ada --- /dev/null +++ b/src/main.py @@ -0,0 +1,533 @@ +# -*- 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) + # 鐢籬ome + 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() \ No newline at end of file diff --git a/src/myTank.py b/src/myTank.py new file mode 100644 index 0000000..bc3f425 --- /dev/null +++ b/src/myTank.py @@ -0,0 +1,170 @@ +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 \ No newline at end of file diff --git a/src/wall.py b/src/wall.py new file mode 100644 index 0000000..dcc3642 --- /dev/null +++ b/src/wall.py @@ -0,0 +1,67 @@ +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() + + # 鏁板瓧浠h〃鍦板浘涓殑浣嶇疆 + # 鐢荤爾鍧 + 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) + + \ No newline at end of file