坦克大战

‘’’
v.1.16
子弹打墙更换图片
‘’’
import pygame, sys, time, random

SCREEN_WIDTH = 800
SCREEN_HIGHT = 600
COLOR_BLACK = pygame.color.Color(123, 123, 123)
ENEMY_TANKS_COUNT = 10

class BaseTank:
def init(self, x, y):
# 方向
self.direction = ‘U’
# 图片集
self.image_dic = {
‘U’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p1tankU.gif’),
‘D’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p1tankD.gif’),
‘L’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p1tankL.gif’),
‘R’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p1tankR.gif’)
}
# 当前图片
self.image = self.image_dic[self.direction]
# 移动速度
self.speed = 10
# 是否死亡
self.live = True
# 坦克区域
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# 记录当前坐标
self.oldx = 0
self.oldy = 0
def move(self):
self.oldx = self.rect.x
self.oldy = self.rect.y
if self.direction == ‘U’:
if self.rect.y > 0:
self.rect.y -= self.speed
elif self.direction == ‘D’:
if self.rect.y < SCREEN_HIGHT - self.rect.height:
self.rect.y += self.speed
elif self.direction == ‘R’:
if self.rect.x < SCREEN_WIDTH - self.rect.height:
self.rect.x += self.speed
elif self.direction == ‘L’:
if self.rect.x > 0:
self.rect.x -= self.speed

def fire(self):
    return Bullet(self)
def stay(self):
    self.rect.x = self.oldx
    self.rect.y = self.oldy
def hit_wall(self):
    for wall in MainGame.wall_list:
        if pygame.sprite.collide_rect(self,wall):
            self.stay()

# 当前坦克加载到窗口中
def display_tank(self):
    # 动态根据方向加载图片
    self.image = self.image_dic[self.direction]
    # 加载到窗口中
    MainGame.window.blit(self.image, self.rect)

class MyTank(BaseTank):
def init(self, x, y):
super(MyTank, self).init(x, y)

def move(self):
    pressed_list = pygame.key.get_pressed()
    if pressed_list[pygame.K_UP]:
        self.direction = 'U'
        super().move()
    elif pressed_list[pygame.K_DOWN]:
        self.direction = 'D'
        super().move()
    elif pressed_list[pygame.K_LEFT]:
        self.direction = 'L'
        super().move()
    elif pressed_list[pygame.K_RIGHT]:
        self.direction = 'R'
        super().move()

class EnemyTank(BaseTank):
def init(self, x, y):
super(EnemyTank, self).init(x, y)
self.image_dic = {
‘U’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p2tankU.gif’),
‘D’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p2tankD.gif’),
‘L’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p2tankL.gif’),
‘R’: pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\p2tankR.gif’)
}
# 随机方向
self.direction = self.random_direction()
self.image = self.image_dic[self.direction]
self.speed = random.randint(1,2)
self.step = random.randint(50,70)
# 生成随机方向
def random_direction(self):
l = [‘U’, ‘D’, ‘L’, ‘R’]
return random.choice(l)
# 随机移动
def move(self):
if self.step > 0:
super(EnemyTank, self).move()
self.step -= 1

    else:
        self.step = random.randint(50,70)
        self.direction = self.random_direction()
# def fire(self):
#     pass

class Bullet:
def init(self,tank):
self.image = pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\enemymissile.gif’)
# 固定用法,获取位置
self.rect = self.image.get_rect()
self.direction = tank.direction
if self.direction == ‘U’:
self.rect.x = tank.rect.x + tank.rect.width/2 - self.rect.width/2
self.rect.y = tank.rect.y - self.rect.height
elif self.direction == ‘D’:
self.rect.x = tank.rect.x + tank.rect.width/2 - self.rect.width/2
self.rect.y = tank.rect.y + tank.rect.height
elif self.direction == ‘L’:
self.rect.x = tank.rect.x - self.rect.width
self.rect.y = tank.rect.y + tank.rect.width/2 - self.rect.width/2
elif self.direction == ‘R’:
self.rect.x = tank.rect.x + tank.rect.height
self.rect.y = tank.rect.y + tank.rect.width/2 - self.rect.height/2
self.live = True
self.speed = 15
def move(self):
if self.direction == ‘U’:
if self.rect.y > 0:
self.rect.y -= self.speed
else:
self.live = False
elif self.direction == ‘D’:
if self.rect.y < SCREEN_HIGHT - self.rect.height:
self.rect.y += self.speed
else:
self.live = False
elif self.direction == ‘L’:
if self.rect.x > 0:
self.rect.x -= self.speed
else:
self.live = False
elif self.direction == ‘R’:
if self.rect.x < SCREEN_WIDTH - self.rect.width:
self.rect.x += self.speed
else:
self.live = False
def hit_enemy_tank(self):
for etank in MainGame.enemy_tank_list:
if pygame.sprite.collide_rect(self,etank):
self.live = False
etank.live = False
bomb = Bomb(etank)
MainGame.bomb_list.append(bomb)
# 新增子弹撞墙方法
def hit_wall(self):
for wall in MainGame.wall_list:
if pygame.sprite.collide_rect(self,wall):
wall.hp -= 1
wall.count += 1
self.live = False
if wall.hp <= 0:
wall.live = False
def display_bullet(self):
MainGame.window.blit(self.image,self.rect)
class Bomb:
def init(self,enemy):
self.rect = enemy.rect
self.images = [
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast0.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast1.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast2.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast3.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast4.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast5.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast6.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\blast7.gif’)
]
self.count = 0
self.live = True
self.image = self.images[self.count]
def display_bomb(self):
if self.count < len(self.images):
MainGame.window.blit(self.image,self.rect)
self.image = self.images[self.count]
self.count += 1
else:
self.live = False
self.count = 0
class Music:
def init(self,music):
pygame.mixer.init()
pygame.mixer.music.load(music)
def play_music(self):
pygame.mixer.music.play(2)
class Wall:
def init(self,x,y):
self.images = [
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\walls.gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\walls(2).gif’),
pygame.image.load(r’F:\老师发的文件\系统班1\tank_img\walls(3).gif’),
]
self.count = 0
self.image = self.images[self.count]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.live = True
self.hp = 3
def display_wall(self):
# 需要更改图片在展示时定义需展示的图片
self.image = self.images[self.count]
MainGame.window.blit(self.image,self.rect)
class MainGame:
# 游戏窗口
window = None
# 我方坦克,唯一
p1 = None
# 敌方坦克存储类表
enemy_tank_list = []
# 存储我方子弹的列表
tank_bullet_list = []
# 存储敌方子弹
enemy_bullet_list = []
# 储存爆炸效果
bomb_list = []
# 储存墙壁
wall_list = []
# 创建游戏窗口
def creat_window(self):
pygame.display.init()
# 创建窗口
MainGame.window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HIGHT))
# 初始化我方坦克
def creat_my_tank(self):
MainGame.p1 = MyTank(400, 500)
audio = Music(r’F:\老师发的文件\系统班1\tank_img\Bob Seger - Old Time Rock & Roll.mp3’)
audio.play_music()
def load_my_tank(self):
MainGame.p1.display_tank()
MainGame.p1.move()
MainGame.p1.hit_wall()
# 初始化敌方坦克
def creat_enemy_tank(self):
for i in range(ENEMY_TANKS_COUNT):
etank = EnemyTank(random.randint(1,10)70,100)
MainGame.enemy_tank_list.append(etank)
# 加载敌方坦克
def load_enemy_tank(self):
for p2 in MainGame.enemy_tank_list:
if p2.live:
p2.display_tank()
p2.move()
p2.hit_wall()
# 调用发射子弹
if p2.step == 1:
b = p2.fire()
MainGame.enemy_bullet_list.append(b)
else:
MainGame.enemy_tank_list.remove(p2)
# 创建墙壁
def creat_wall(self):
for i in range(15):
wall = Wall(i
60,300)
MainGame.wall_list.append(wall)
# 加载墙壁
def load_wall(self):
for wall in MainGame.wall_list:
if wall.live:
wall.display_wall()
else:
MainGame.wall_list.remove(wall)

# 加载我方坦克发射子弹
def load_bullet_my(self):
    for b in MainGame.tank_bullet_list:
        if b.live:
            b.display_bullet()
            b.move()
            b.hit_enemy_tank()
            # 新增我方坦克子弹撞墙方法
            b.hit_wall()
        else:
            MainGame.tank_bullet_list.remove(b)

# 加载敌方坦克发射子弹
def load_bullet_enemy(self):
    for b in MainGame.enemy_bullet_list:
        if b.live:
            b.display_bullet()
            b.move()
            # 新增敌方坦克子弹撞墙方法
            b.hit_wall()
            # b.hit_enemy_tank()
        else:
            MainGame.enemy_bullet_list.remove(b)
# 加载爆炸效果
def load_bomb(self):
    for a in MainGame.bomb_list:
        if a.live:
            a.display_bomb()
        else:
            MainGame.bomb_list.remove(a)
# 处理按键事件
def deal_events(self):
    event_list = pygame.event.get()
    for e in event_list:
        if e.type == pygame.QUIT:
            # 结束程序
            self.game_over()
        elif e.type == pygame.KEYDOWN:
            print('按下按键')
            print(e.key, e.unicode)
            if e.key == pygame.K_SPACE:
                # 调用我方坦克发射方法
                b = MainGame.p1.fire()
                # 装到列表中
                MainGame.tank_bullet_list.append(b)
                print(f'当前子弹个数为{len(MainGame.tank_bullet_list)}')

        elif e.type == pygame.MOUSEBUTTONDOWN:
            print('按下鼠标')
def start_game(self):
    # 调用创建窗口的方法
    self.creat_window()
    # 创建我方坦克
    self.creat_my_tank()
    # 创建敌方坦克
    self.creat_enemy_tank()
    # 创建墙壁
    self.creat_wall()

    while True:
        # 修改背景色
        MainGame.window.fill(COLOR_BLACK)

        # 调用事件处理方法
        self.deal_events()
        # 调用加载我方坦克方法
        self.load_my_tank()
        # 调用敌方坦克
        self.load_enemy_tank()
        # 加载我方坦克发射子弹
        self.load_bullet_my()
        # 加载敌方坦克发射子弹
        self.load_bullet_enemy()
        # 加载爆炸效果
        self.load_bomb()
        # 加载墙壁
        self.load_wall()
        # 窗口持续刷新
        pygame.display.update()
        time.sleep(0.02)
def game_over(self):
    print('游戏结束')
    sys.exit()

game = MainGame()
game.start_game()

你可能感兴趣的:(坦克大战)