实现的功能有:
1.难度变更,根据玩家击杀的敌方坦克数量和获得的分数,增加场上的敌方坦克数量
2.血条显示,玩家坦克每击杀一个敌方坦克获得一个生命标记,没受到一次攻击掉落一个生命标记,初始标记20个
3.敌方坦克的随机生成,敌方坦克3种类型,一种血厚防高移速慢黄色坦克,一种血防平衡移速适中白色坦克,一种血薄防低移速快原谅坦克,随机生成不同类型
4.空格键发射子弹,方向键控制方向,未设置连发,按一次空格射击一次
5.碰撞判定,圆的相交判定
6.允许穿模,即坦克相撞不产生伤害,不然难度太高
# tank.py
class Tank:
def __init__(self, life, speed_x, speed_y, screen):
self.life = life
self.speed_x = speed_x
self.speed_y = speed_y
self.screen = screen
def show_tank(self, image, x, y):
self.screen.blit(image, (x, y))
def hit(self):
pass
def move(self):
pass
# mytank.py
import tank
import pygame
import bullet
class MyTank(tank.Tank):
hero_up = './mytank/1.gif' # 我方坦克各个状态
hero_down = './mytank/3.gif'
hero_left = './mytank/4.gif'
hero_right = './mytank/2.gif'
bullets = [] # 类字段-我方子弹列表
def __init__(self, screen, pos_x=0, pos_y=0): # 初始化
super().__init__(1, 0, 0, screen)
self.pos_x = pos_x
self.pos_y = pos_y
def move(self, move_dic): # 移动方法,速度改变,返回移动方向
if move_dic == 'left':
surface = pygame.image.load(self.hero_left)
self.speed_x = -5
self.speed_y = 0
elif move_dic == 'right':
surface = pygame.image.load(self.hero_right)
self.speed_x = 5
self.speed_y = 0
elif move_dic == 'up':
surface = pygame.image.load(self.hero_up)
self.speed_y = -5
self.speed_x = 0
elif move_dic == 'down':
surface = pygame.image.load(self.hero_down)
self.speed_y = 5
self.speed_x = 0
return surface, move_dic
def stop(self, stop_dic): # 停止方法,接收移动结束的方向,速度归0,返回停止方向
if stop_dic == 'left':
surface = pygame.image.load(self.hero_left)
self.speed_x = 0
self.speed_y = 0
elif stop_dic == 'right':
surface = pygame.image.load(self.hero_right)
self.speed_x = 0
self.speed_y = 0
elif stop_dic == 'up':
surface = pygame.image.load(self.hero_up)
self.speed_y = 0
self.speed_x = 0
elif stop_dic == 'down':
surface = pygame.image.load(self.hero_down)
self.speed_y = 0
self.speed_x = 0
return surface, stop_dic
def change_position(self): # 坐标更改
self.pos_x += self.speed_x
self.pos_y += self.speed_y
def hit(self, dic): # 子弹射击,各个方向
global bullet1
tank_w, tank_h = pygame.image.load(self.hero_up).get_size()
bullet_w, bullet_h = bullet.Bullet.surface.get_size()
if dic == 'left':
bullet1 = bullet.Bullet(self.pos_x - bullet_w, self.pos_y + (tank_h - bullet_h) / 2)
bullet1.move(dic)
elif dic == 'right':
bullet1 = bullet.Bullet(self.pos_x + tank_w, self.pos_y + (tank_h - bullet_h) / 2)
bullet1.move(dic)
elif dic == 'up':
bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y - bullet_h)
bullet1.move(dic)
elif dic == 'down':
bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y + tank_h)
bullet1.move(dic)
self.bullets.append(bullet1)
def bullet_change(self):
for b in self.bullets:
b.pos_x += b.speed_x
b.pos_y += b.speed_y
def bullet_show(self):
for b in self.bullets:
self.screen.blit(bullet.Bullet.surface, (b.pos_x, b.pos_y))
def bullet_dead(self):
for b in self.bullets[:]:
if b.pos_x <= 0 or b.pos_x >= 60*20:
self.bullets.remove(b)
if b.pos_y <= 0 or b.pos_y >= 60*14:
self.bullets.remove(b)
# enemy.py
import pygame
import tank
from random import randint
import bullet
class Enemy(tank.Tank):
enemy_bullets = [] # 类字段-敌方子弹列表
def __init__(self, surface, speed_x=0, speed_y=0, screen=0, pos_x=0, pos_y=0): # 初始化敌方坦克
self.speed_x = speed_x
self.speed_y = speed_y
self.screen = screen
self.pos_x = pos_x
self.pos_y = pos_y
self.type_e = randint(1, 3) # 随机类型
self.life = 8 - self.type_e * 2 # 类型对应血量
self.surface = surface
def move(self): # 移动方法,根据类型决定敌方坦克模样和移动速度
r_dic = randint(-50, 50)
if r_dic == 1:
surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/u.gif')
self.speed_y = self.type_e * (-1)
self.speed_x = 0
self.hit(r_dic)
self.surface = surface
elif r_dic == 2:
surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/d.gif')
self.speed_y = self.type_e * 1
self.speed_x = 0
self.hit(r_dic)
self.surface = surface
elif r_dic == 3:
surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/l.gif')
self.speed_x = self.type_e * (-1)
self.speed_y = 0
self.hit(r_dic)
self.surface = surface
elif r_dic == 4:
surface = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/r.gif')
self.speed_x = self.type_e * 1
self.speed_y = 0
self.hit(r_dic)
self.surface = surface
def hit(self, r_dic): # 敌方子弹射击方法
tank_w, tank_h = pygame.image.load('./enemy/enemy'+str(self.type_e)+'/u.gif').get_size()
bullet_w, bullet_h = bullet.Bullet.surface.get_size()
if r_dic == 3:
bullet1 = bullet.Bullet(self.pos_x - bullet_w, self.pos_y + (tank_h - bullet_h) / 2)
bullet1.move('left')
self.enemy_bullets.append(bullet1)
elif r_dic == 4:
bullet1 = bullet.Bullet(self.pos_x + tank_w, self.pos_y + (tank_h - bullet_h) / 2)
bullet1.move('right')
self.enemy_bullets.append(bullet1)
elif r_dic == 1:
bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y - bullet_h)
bullet1.move('up')
self.enemy_bullets.append(bullet1)
elif r_dic == 2:
bullet1 = bullet.Bullet(self.pos_x + (tank_w - bullet_w) / 2, self.pos_y + tank_h)
bullet1.move('down')
self.enemy_bullets.append(bullet1)
def bullet_change(self):
for b in self.enemy_bullets:
b.pos_x += b.speed_x
b.pos_y += b.speed_y
def bullet_show(self):
for b in self.enemy_bullets:
self.screen.blit(bullet.Bullet.surface, (b.pos_x, b.pos_y))
def bullet_dead(self):
for b in self.enemy_bullets:
if b.pos_x <= 0 or b.pos_x >= 60*20:
self.enemy_bullets.remove(b)
if b.pos_y <= 0 or b.pos_y >= 60*14:
self.enemy_bullets.remove(b)
def change_position(self):
self.pos_x += self.speed_x
self.pos_y += self.speed_y
# bullet.py
import pygame
class Bullet:
surface = pygame.image.load('./bullet/10.gif')
speed_x = 0
speed_y = 0
def __init__(self, pos_x, pos_y):
self.pos_x = pos_x
self.pos_y = pos_y
def move(self, move_dic): # 子弹的速度
if move_dic == 'left':
self.speed_x = -8
elif move_dic == 'right':
self.speed_x = 8
elif move_dic == 'up':
self.speed_y = -8
elif move_dic == 'down':
self.speed_y = 8
# main.py
import pygame
import mytank
from enemy import Enemy
from random import randint
import bullet
import math
if __name__ == '__main__':
pygame.init()
width = 60 * 20
height = 60 * 14
screen = pygame.display.set_mode((width, height))
bullet_wid, bullet_hig = bullet.Bullet.surface.get_size()
pos_x = 660
pos_y = 780
hero = mytank.MyTank(screen, pos_x, pos_y)
surface = pygame.image.load(mytank.MyTank.hero_up)
dic = ''
surface_e = pygame.image.load('./enemy/enemy_creat.gif') # 敌方坦克初始化图片
enemy_wid, enemy_hig = surface_e.get_size()
enemys = [] # 初始化敌军坦克列表
e_number = 0 # 敌军坦克数量
scouce = 20 # 初始分数计算值
my_life = 20 # 我方生命初始值
font = pygame.font.SysFont('Times:', 20)
while True:
if my_life == 0: # 我方生命归0,游戏结束
exit()
source_txt = font.render('source: '+str(scouce-20), True, (255, 255, 255)) # 显示分数
life_txt = font.render('life: '+'* '*my_life, True, (255, 255, 255)) # 显示生命值
e_number = scouce // 20 # 根据分数增加敌方坦克数量
while len(enemys) < e_number: # 保证场上敌方坦克数量
enemy = Enemy(surface_e, 0, 0, screen, randint(100, 1000), randint(100, 500))
enemys.append(enemy)
screen.fill((0, 0, 0))
hero.bullet_dead() # 我方子弹溢出边界消除
hero.show_tank(surface, hero.pos_x, hero.pos_y) # 我方坦克显示
hero.bullet_show() # 我方子弹显示
for enemy in enemys: # 敌方坦克不允许出地图(你也不想就这样丢失敌方视野对吧?)
if (enemy.pos_x >= 60 * 19 and enemy.speed_x > 0) or (enemy.pos_x <= 0 and enemy.speed_x < 0):
enemy.speed_x = 0 - enemy.speed_x
if (enemy.pos_y >= 60 * 13 and enemy.speed_y > 0) or (enemy.pos_y <= 0 and enemy.speed_y < 0):
enemy.speed_y = 0 - enemy.speed_y
enemy.change_position() # 敌方位置修改
enemy.show_tank(enemy.surface, enemy.pos_x, enemy.pos_y) # 敌方坦克绘制
enemy.move() # 敌方坦克自动移动
enemy.bullet_change() # 敌方子弹位置修改
enemy.bullet_dead() # 敌方子弹溢出边界消除
enemy.bullet_show() # 敌方子弹显示
screen.blit(source_txt, (5, 5))
screen.blit(life_txt, (400, 5))
pygame.display.flip()
for event in pygame.event.get(): # 我方坦克方向键控制
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
surface, dic = hero.move('right')
elif event.key == pygame.K_LEFT:
surface, dic = hero.move('left')
elif event.key == pygame.K_UP:
surface, dic = hero.move('up')
elif event.key == pygame.K_DOWN:
surface, dic = hero.move('down')
if event.key == pygame.K_SPACE:
hero.hit(dic) # 子弹射击控制
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
surface, dic = hero.stop('right')
elif event.key == pygame.K_LEFT:
surface, dic = hero.stop('left')
elif event.key == pygame.K_UP:
surface, dic = hero.stop('up')
elif event.key == pygame.K_DOWN:
surface, dic = hero.stop('down')
if (hero.pos_x >= 60*19 and hero.speed_x > 0) or (hero.pos_x <= 0 and hero.speed_x < 0): # 我方坦克也不能出边界
hero.speed_x = 0
if (hero.pos_y >= 60*13 and hero.speed_y > 0) or (hero.pos_y <= 0 and hero.speed_y < 0):
hero.speed_y = 0
hero.change_position() # 我方坦克位置变更
hero.bullet_change() # 我方坦克子弹变更
for e in enemys[:]: # 遍历敌方坦克
for my_bullet in mytank.MyTank.bullets[:]: # 遍历我方子弹
if math.sqrt((e.pos_x+enemy_wid/2-my_bullet.pos_x-bullet_wid/2)**2+(e.pos_y+enemy_hig/2-my_bullet.pos_y-bullet_hig/2)**2) < (enemy_wid/2+bullet_wid/2): # 相交检测
mytank.MyTank.bullets.remove(my_bullet) # 我方子弹消失
e.life -= 1 # 敌方生命-1
scouce += 1 # 我方分数+1
if e.life == 0: # 完成击杀
enemys.remove(e) # 敌方坦克消除
my_life += 1 # 我方生命+1
for e_bullet in Enemy.enemy_bullets[:]: # 遍历敌方子弹
if math.sqrt((hero.pos_x+enemy_wid/2-e_bullet.pos_x-bullet_wid/2)**2+(hero.pos_y+enemy_hig/2-e_bullet.pos_y-bullet_hig/2)**2) < (enemy_wid/2+bullet_wid/2): # 相交检测
my_life -= 1 # 相交我方生命-1
Enemy.enemy_bullets.remove(e_bullet) # 敌方子弹消除
pygame.time.delay(10)
有兴趣的可以拿去尝试看看能打到多少分,每个py文件独立一个模块,资源图:
bullet:
enemy:(enemy_creat.gif 是我自己复制了一个敌方坦克图然后用ps直接扣空成一张白图,并不是白色,而是什么都没有,拿来作为初始化每一个敌军坦克的初始显示图,因为一移动就会自动变换成对应类型的模样,刚开始创建打印应该什么都没有)
enemy1:
enemy2:
enemy3:
mytank:
效果图: