碰撞消除实现关键是遍历玩家坦克和敌军子弹和敌军坦克列表的坐标相交判定 以及 敌军坦克和玩家子弹列表的坐标相较判定
鉴于子弹和坦克都是正方形的图片,使用圆的相交判定来确定是否相交
# 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
def dead(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):
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
import pygame
import tank
from random import randint
import bullet
class Enemy(tank.Tank):
def __init__(self, life, surface, speed_x=0, speed_y=0, screen=0, pos_x=0, pos_y=0, enemy_bullet=[]):
self.life = life
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.enemy_bullet = enemy_bullet
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_bullet.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_bullet.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_bullet.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_bullet.append(bullet1)
def bullet_change(self):
for b in self.enemy_bullet:
b.pos_x += b.speed_x
b.pos_y += b.speed_y
def bullet_show(self):
for b in self.enemy_bullet:
self.screen.blit(bullet.Bullet.surface, (b.pos_x, b.pos_y))
def bullet_dead(self):
for b in self.enemy_bullet:
if b.pos_x <= 0 or b.pos_x >= 60*20:
self.enemy_bullet.remove(b)
if b.pos_y <= 0 or b.pos_y >= 60*14:
self.enemy_bullet.remove(b)
def change_position(self):
self.pos_x += self.speed_x
self.pos_y += self.speed_y
# bullet.py
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
"""new python"""
import pygame
import mytank
from enemy import Enemy
from random import randint
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/enemy1/u.gif')
enemy_wid, enemy_hig = surface_e.get_size()
enemys = []
while True:
rr = randint(1, 3)
while len(enemys) < 6:
enemy = Enemy(randint(1, 6), 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.bullet_change()
enemy.bullet_dead()
enemy.show_tank(enemy.surface, enemy.pos_x, enemy.pos_y)
enemy.bullet_show()
enemy.move()
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()
pygame.time.delay(10)
碰撞消除在各个类的dead方法中实现
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):
enemys.remove(e)
mytank.MyTank.bullets.remove(my_bullet)
放在main.py中 pygame.time.delay(10)前即可完成敌军和我方子弹的碰撞消除,自己和敌方子弹的碰撞消除同理即可,想了一下还是给个运行图吧(随机类型敌军坦克,并且少于6个,自动生成到6个)