感觉这个游戏可能有bug,但是实践又看不出来(因为容易莫名其妙的死亡)
1.这次新增加了 墙体类
2.实现了创建地图的方法
3.增加了碰撞逻辑
`
class Tank(pygame.sprite.Sprite):
my_all_bullet = pygame.sprite.Group()
enemy_all_bullet = pygame.sprite.Group()
def __init__(self,screen,team=0):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.all_bullet = pygame.sprite.Group()
if team == 0:
self.imgs = {
'w': pygame.image.load('E:/图片/heroW.png'),
's': pygame.image.load('E:/图片/heroS.png'),
'a': pygame.image.load('E:/图片/heroA.png'),
'd': pygame.image.load('E:/图片/heroD.png')
}
else:
self.imgs = {
'w': pygame.image.load('E:/图片/eW.png'),
's': pygame.image.load('E:/图片/eS.png'),
'a': pygame.image.load('E:/图片/eA.png'),
'd': pygame.image.load('E:/图片/eD.png')
}
self.team = team
self.direction = 'w'
self.img = self.imgs[self.direction]
# tank初始化位置
self.rect = self.img.get_rect()
if team == 0:
self.speed = 5
self.rect.topleft = [300, 600]
else:
self.ran = [0, 4]
self.speed = random.randint(1, 5)
self.rect.topleft = [random.random()*SCREEN_X, random.random()*SCREEN_Y]
def control(self):
# 按键控制
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w]:
self.direction = 'w'
self.rect.top -= self.speed
if key_pressed[pygame.K_s]:
self.direction = 's'
self.rect.top += self.speed
if key_pressed[pygame.K_a]:
self.direction = 'a'
self.rect.left -= self.speed
if key_pressed[pygame.K_d]:
self.direction = 'd'
self.rect.left += self.speed
if key_pressed[pygame.K_j]:
b = Bullet(self.direction,self.rect)
self.all_bullet.add(b)
Tank.my_all_bullet.add(b)
def ai(self):
self.ran[0] += 1
if self.ran[0] % 30 == 0:
bb = Bullet(self.direction, self.rect)
self.all_bullet.add(bb)
Tank.enemy_all_bullet.add(bb)
if self.ran[1] % 4 == 0:
self.direction = 'w'
self.rect.top -= self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
elif self.ran[1] % 4 == 1:
self.direction = 's'
self.rect.top += self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
elif self.ran[1] % 4 == 2:
self.direction = 'a'
self.rect.left -= self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
elif self.ran[1] % 4 == 3:
self.direction = 'd'
self.rect.left += self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
def display(self):
self.img = self.imgs[self.direction]
self.screen.blit(self.img, self.rect)
for i in self.all_bullet:
i.move()
self.all_bullet.draw(self.screen)
@classmethod
# 清空子弹
def clear_all(cls):
cls.my_all_bullet.empty()
cls.enemy_all_bullet.empty()
class Bullet(pygame.sprite.Sprite):
def init(self,dir,rect):
pygame.sprite.Sprite.init(self)
self.image = pygame.image.load('E:/图片/bullet4.png')
self.direction = dir
self.rect = self.image.get_rect()
if self.direction == 'w':
self.rect.left = rect.left + rect.width/2 - self.rect.width/2
self.rect.top = rect.top - self.rect.top
elif self.direction == 's':
self.rect.left = rect.left + rect.width / 2 - self.rect.width / 2
self.rect.top = rect.top + rect.height
elif self.direction == 'a':
self.rect.left = rect.left - rect.width / 2 - self.rect.width / 2
self.rect.top = rect.top + rect.width/2 - self.rect.width/2
elif self.direction == 'd':
self.rect.left = rect.left + rect.width
self.rect.top = rect.top + rect.width/2 - self.rect.width/2
self.speed = 7
def move(self):
if self.direction == 'w':
self.rect.top -= self.speed
elif self.direction == 's':
self.rect.top += self.speed
elif self.direction == 'a':
self.rect.left -= self.speed
elif self.direction == 'd':
self.rect.left += self.speed
class Hard(pygame.sprite.Sprite):
hard_wall = pygame.sprite.Group()
def __init__(self,screen):
pygame.sprite.Sprite.__init__(self)
self.img = pygame.image.load('E:/图片/0.png')
self.rect = self.img.get_rect()
self.rect.topleft = [random.random() * SCREEN_X, random.random() * SCREEN_Y]
self.screen = screen
def show(self):
self.screen.blit(self.img, self.rect)
@classmethod
def clear(cls):
cls.hard_wall.empty()
class Sound:
def init(self):
pygame.mixer.init()
pygame.mixer.music.load("E:/图片/back.mp3")
pygame.mixer.music.set_volume(0.3)
# 音量大小
self._bomb = pygame.mixer.Sound("E:/图片/gunfire.mp3")
self._horry = pygame.mixer.Sound("E:/图片/oo.mp3")
self._hit = pygame.mixer.Sound("E:/图片/hit.mp3")
def back(self):
pygame.mixer.music.play(-1)
# 背景音乐,-1为循环播放
def boost(self):
pygame.mixer.Sound.play(self._bomb)
# 炸弹音乐
def horry(self):
pygame.mixer.Sound.play(self._horry)
def hit(self):
pygame.mixer.Sound.play(self._hit)
class Manager:
def init(self):
self.screen = pygame.display.set_mode((SCREEN_X,SCREEN_Y))
self.title = pygame.display.set_caption('坦克大战')
self.players = pygame.sprite.Group()
self.enemys = pygame.sprite.Group()
self.maps = pygame.sprite.Group()
self.sound = Sound()
def player(self):
#生成1辆我方坦克
p = Tank(self.screen)
self.players.add(p)
def enemy(self,x):
#生成x辆敌方坦克
e = [Tank(self.screen, 1) for _ in range(x)]
self.enemys.add(e)
def map(self,x):
m2 = [Hard(self.screen) for _ in range(x)]
self.maps.add(m2)
Hard.hard_wall.add(m2)
def remain(self):
self.sound.horry()
print('!!!!!!!!!!')
Tank.clear_all()
Hard.clear()
mm = Manager()
mm.main()
def main(self):
pygame.init()
self.sound.back()
self.player()
self.enemy(7)
self.map(8)
while True:
self.screen.fill(BACK)
for i in self.players:
i.control()
i.display()
for j in self.enemys:
j.ai()
j.display()
for k in self.maps:
k.show()
time.sleep(0.1)#防止坦克跑得太快
pygame.display.update()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
#我方坦克与敌方坦克的碰撞(碰撞后坦克会消失)
tanks_pump = pygame.sprite.groupcollide(self.players, self.enemys, True, True)
if tanks_pump:
self.remain()
self.sound.boost()
#玩家子弹打敌方坦克
i_hit_e = pygame.sprite.groupcollide(Tank.my_all_bullet, self.enemys, True, True)
if i_hit_e:
print(1)
self.sound.boost()
#敌方子弹打玩家坦克
e_hit_i = pygame.sprite.groupcollide(self.players, Tank.enemy_all_bullet, True, True)
if e_hit_i:
print(2)
self.remain()
self.sound.boost()
#我方子弹碰到敌方子弹
bullet_pump = pygame.sprite.groupcollide(Tank.my_all_bullet,Tank.enemy_all_bullet,True,True)
if bullet_pump:
print(3)
self.sound.hit()
#我方坦克撞墙
i_hit_wall = pygame.sprite.groupcollide(Hard.hard_wall,self.players,False,True)
if i_hit_wall:
print(4)
self.remain()
self.sound.boost()
#敌方坦克撞墙
e_hit_wall = pygame.sprite.groupcollide(Hard.hard_wall, self.enemys, False, True)
if e_hit_wall:
print(5)
self.sound.boost()
#我方子弹撞墙
i_bullet_wall = pygame.sprite.groupcollide(Hard.hard_wall, Tank.my_all_bullet, False, True)
if i_bullet_wall:
print(6)
self.sound.hit()
#敌方子弹撞墙
e_bullet_wall = pygame.sprite.groupcollide(Hard.hard_wall, Tank.enemy_all_bullet, False, True)
if e_bullet_wall:
print(7)
self.sound.hit()
m = Manager()
m.main()
`