Skip to content

Commit

Permalink
小游戏坦克大战
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingyao committed Oct 30, 2016
0 parents commit d80823c
Show file tree
Hide file tree
Showing 71 changed files with 1,017 additions and 0 deletions.
Binary file added image/TankWar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/appear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/boom_dynamic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/boom_static.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/brick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/bullet_down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/bullet_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/bullet_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/bullet_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_1_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_1_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_1_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_1_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_2_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_2_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_2_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_2_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_3_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_3_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_3_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_3_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_4_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_4_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_4_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/enemy_4_3.png
Binary file added image/food_boom.png
Binary file added image/food_clock.png
Binary file added image/food_gun.png
Binary file added image/food_iron.png
Binary file added image/food_protect.png
Binary file added image/food_star.png
Binary file added image/food_tank.png
Binary file added image/gameover.png
Binary file added image/home.png
Binary file added image/home1.png
Binary file added image/home_destroyed.png
Binary file added image/ice.png
Binary file added image/iron.png
Binary file added image/logo.png
Binary file added image/mask.png
Binary file added image/protect.png
Binary file added image/river1.png
Binary file added image/river2.png
Binary file added image/tank_T1_0.png
Binary file added image/tank_T1_1.png
Binary file added image/tank_T1_2.png
Binary file added image/tank_T2_0.png
Binary file added image/tank_T2_1.png
Binary file added image/tank_T2_2.png
Binary file added image/tip.png
Binary file added image/tree.png
Binary file added music/Gunfire.wav
Binary file not shown.
Binary file added music/add.wav
Binary file not shown.
Binary file added music/bang.wav
Binary file not shown.
Binary file added music/blast.wav
Binary file not shown.
Binary file added music/fire.wav
Binary file not shown.
Binary file added music/hit.wav
Binary file not shown.
Binary file added music/start.wav
Binary file not shown.
Binary file added src/__pycache__/bullet.cpython-34.pyc
Binary file not shown.
Binary file added src/__pycache__/bulletClass.cpython-34.pyc
Binary file not shown.
Binary file added src/__pycache__/enemyTank.cpython-34.pyc
Binary file not shown.
Binary file added src/__pycache__/food.cpython-34.pyc
Binary file not shown.
Binary file added src/__pycache__/myTank.cpython-34.pyc
Binary file not shown.
Binary file added src/__pycache__/wall.cpython-34.pyc
Binary file not shown.
67 changes: 67 additions & 0 deletions src/bulletClass.py
Original file line number Diff line number Diff line change
@@ -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

125 changes: 125 additions & 0 deletions src/enemyTank.py
Original file line number Diff line number Diff line change
@@ -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]))
55 changes: 55 additions & 0 deletions src/food.py
Original file line number Diff line number Diff line change
@@ -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


Loading

0 comments on commit d80823c

Please sign in to comment.