Map类
1.之前没有Map类的时候,都是在main方法中显示地图。随着砖块类型和地图类型的增加,这样会把Manager类搞的很麻烦
2.有了Map类,砖块类就一个构造方法就差不多了
3.可以把一些性质类似的砖块放到一个Brick类里
4.Map类中可以包含各种方块,随机生成各种地图
5.实现了墙壁的碰撞,即墙壁不能叠加
6.坦克撞墙会再生成一个坦克
`
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()
self.ran = [0, 4]
if team == 0:
self.speed = 5
self.rect.topleft = [random.randrange(30,SCREEN_X,80),random.randrange(30,SCREEN_Y,20)]
else:
self.speed = random.randint(1, 5)
self.rect.topleft = [random.random()*SCREEN_X, random.random()*SCREEN_Y]
def __boundary(self):
# 地图是上下左右贯通的
# up
if self.rect.top <= 0-20:
self.rect.top = SCREEN_Y + 10
# down
if self.rect.top >= SCREEN_Y + 20:
self.rect.top = 0 - 20
# left
if self.rect.left <= 0-20:
self.rect.left = SCREEN_X + 10
# right
if self.rect.left >= SCREEN_X + 20:
self.rect.left = 0 - 20
def control(self):
self.__boundary()
# 按键控制(键盘连续监听)
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]:
#控制子弹发射的频率
rrr = random.randint(1, 5)
if rrr == 3:
b = Bullet(self.direction, self.rect)
self.all_bullet.add(b)
Tank.my_all_bullet.add(b)
def ai(self):
self.__boundary()
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(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.r = rect
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 = 10
self.distance = 200
def move(self):
# 子弹距离坦克的位置太远则会消失
if (abs(self.rect.top - self.r.top) > self.distance
or abs(self.rect.left - self.r.left) > self.distance):
self.kill()
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 Brick(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
#self.style = style
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]
class Map:
def init(self,screen):
self.hard_block = pygame.sprite.Group()
self.screen = screen
def clear(self):
self.hard_block.empty()
def map1(self, a,style=-1):
mm = [Brick() for _ in range(a)]
for i in range(a):
if style == 0:#横着的方块
mm[0].rect.topleft = [10,random.randrange(int(SCREEN_Y/4),int(3*SCREEN_Y/4),10)]
mm[i].rect.top = mm[i-1].rect.top
mm[i].rect.left = mm[i-1].rect.left + 55
elif style == 1:#竖着的方块
mm[0].rect.topleft = [random.randrange(int(SCREEN_X/5),int(5*SCREEN_X/6),15),10]
mm[i].rect.top = mm[i - 1].rect.top + 55
mm[i].rect.left = mm[i - 1].rect.left
elif style == 2:#斜着的方块
mm[0].rect.topleft = [random.randrange(int(SCREEN_X/3),int(5*SCREEN_X/6),20),
random.randrange(int(SCREEN_Y/5),int(2*SCREEN_Y/5),10)]
mm[i].rect.top = mm[i - 1].rect.top + 55
mm[i].rect.left = mm[i - 1].rect.left + 55
self.hard_block.add(mm)
def display(self):
for i in self.hard_block:
self.screen.blit(i.img, i.rect)
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:
#计时器的id(id是什么数字不重要)
id = 0
id2 = 0
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 = Map(self.screen)
self.sound = Sound()
def player(self):
#生成1辆我方坦克
p = Tank(self.screen)
self.players.add(p)
def enemy(self,x=1):
#生成x辆敌方坦克
e = [Tank(self.screen, 1) for _ in range(x)]
self.enemys.add(e)
def remain(self):
self.sound.horry()
print('!!')
Tank.clear()
self.maps.clear()
mm = Manager()
mm.main()
def re(self,x):
pygame.time.set_timer(Manager.id2, 15000)
self.maps.clear()
print('hsdjfhdskfjhdjkfhdjfhd')
self.maps.map1(x,1)
def main(self):
pygame.init()
self.sound.back()
self.player()
self.maps.map1(10,1)
pygame.time.set_timer(Manager.id,random.randrange(3500,8500,100))
while True:
self.screen.fill(BACK)
for i in self.players:
i.control()
i.display()
for j in self.enemys:
j.ai()
j.display()
self.maps.display()
time.sleep(0.1)#防止坦克跑得太快
pygame.display.update()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif e.type == Manager.id:
self.enemy()
self.maps.map1(1)
elif e.type == Manager.id2:
self.re(5)
#我方坦克与敌方坦克的碰撞(碰撞后坦克会消失)
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(self.maps.hard_block,self.players,False,True)
if i_hit_wall:
print(4)
self.remain()
self.sound.boost()
#敌方坦克撞墙
e_hit_wall = pygame.sprite.groupcollide(self.maps.hard_block, self.enemys, False, True)
if e_hit_wall:
print(5)
self.enemy()#再创建一个敌方坦克
self.sound.boost()
#我方子弹撞墙
i_bullet_wall = pygame.sprite.groupcollide(self.maps.hard_block, Tank.my_all_bullet,
False, True)
if i_bullet_wall:
print(6)
self.sound.hit()
#敌方子弹撞墙
e_bullet_wall = pygame.sprite.groupcollide(self.maps.hard_block,
Tank.enemy_all_bullet, False, True)
if e_bullet_wall:
print(7)
self.sound.hit()
#检测墙体之间的碰撞
for i in self.maps.hard_block:
for j in self.maps.hard_block:
if i != j:
walls_pump = pygame.sprite.collide_rect(i,j)
if walls_pump:
self.maps.hard_block.remove(i)
`
坦克类和子弹类都没变,所以在这里不显示
1.这次在brick类里增加了砖块,以后还要增加更多方块
2.添加一个方块的流程:brick类中写好简单逻辑,map类中写好显示方法和地图方法。main方法中调用map类的方法,并且增加碰撞的逻辑
class Brick(pygame.sprite.Sprite):
#基岩--hard_block:不能消失,所有坦克不能推开
#砖块--brick_block:可以打掉,所有坦克不能推开
#羊毛--wool:不能消失,所有坦克可以推开
#石头--stone:可以打掉,所有坦克可以推开
def init(self,attack=0):
pygame.sprite.Sprite.init(self)
self.attack = attack
if 1 <= self.attack <= 10:
self.img = pygame.image.load('E:/图片/1.png')
elif self.attack <= 0:
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]
class Map:
def init(self,screen):
self.brick_block = pygame.sprite.Group()
self.hard_block = pygame.sprite.Group()
self.screen = screen
def clear(self):
self.hard_block.empty()
self.brick_block.empty()
def map_total(self):
self.map1(4,2)
self.map1(8,0)
def map1(self, a,style=-1):
mm = [Brick() for _ in range(a)]
for i in range(a):
if style == 0:#横着的方块
mm[0].rect.topleft = [10,random.randrange(int(SCREEN_Y/4),int(3*SCREEN_Y/4),10)]
mm[i].rect.top = mm[i-1].rect.top
mm[i].rect.left = mm[i-1].rect.left + 55
elif style == 1:#竖着的方块
mm[0].rect.topleft = [random.randrange(int(SCREEN_X/5),int(5*SCREEN_X/6),15),10]
mm[i].rect.top = mm[i - 1].rect.top + 55
mm[i].rect.left = mm[i - 1].rect.left
elif style == 2:#斜着的方块
mm[0].rect.topleft = [random.randrange(int(SCREEN_X/3),int(5*SCREEN_X/6),20),
random.randrange(int(SCREEN_Y/5),int(2*SCREEN_Y/5),10)]
mm[i].rect.top = mm[i - 1].rect.top + 55
mm[i].rect.left = mm[i - 1].rect.left + 55
self.hard_block.add(mm)
def map2(self,a=1):
mm = [Brick(3) for _ in range(a)]
self.brick_block.add(mm)
def display_all(self):
for i in self.hard_block:
self.screen.blit(i.img, i.rect)
for j in self.brick_block:
self.screen.blit(j.img, j.rect)
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:
#计时器的id(id是什么数字不重要)
id = 0
id2 = 0
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 = Map(self.screen)
self.sound = Sound()
self.ran = [False,False]
def player(self):
#生成1辆我方坦克
p = Tank(self.screen)
self.players.add(p)
def enemy(self,x=1):
#生成x辆敌方坦克
e = [Tank(self.screen, 1) for _ in range(x)]
self.enemys.add(e)
def remain(self):
self.sound.horry()
print('!!')
Tank.clear()
self.maps.clear()
mm = Manager()
mm.main()
def re(self,x):
pygame.time.set_timer(Manager.id2, 15000)
self.maps.clear()
print('hsdjfhdskfjhdjkfhdjfhd')
self.maps.map1(x,1)
def main(self):
pygame.init()
self.sound.back()
self.player()
self.maps.map_total()
pygame.time.set_timer(Manager.id,random.randrange(3500,8500,100))
while True:
self.screen.fill(BACK)
for i in self.players:
i.control()
i.display()
for j in self.enemys:
j.ai()
j.display()
self.maps.display_all()
time.sleep(0.1)#防止坦克跑得太快
pygame.display.update()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif e.type == Manager.id:
self.enemy()
self.maps.map2(1)
elif e.type == Manager.id2:
self.re(5)
#我方坦克与敌方坦克的碰撞
tanks_pump = pygame.sprite.groupcollide(self.players, self.enemys, True, True)
if tanks_pump:
self.sound.boost()
self.remain()
#玩家子弹打敌方坦克
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.sound.boost()
self.remain()
#我方子弹碰到敌方子弹
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(self.maps.hard_block,self.players,False,True)
i_hit_brick = pygame.sprite.groupcollide(self.maps.brick_block,self.players,
True,True)
if i_hit_wall or i_hit_brick:
print(4)
self.sound.boost()
self.remain()
#敌方坦克撞墙
e_hit_wall = pygame.sprite.groupcollide(self.maps.hard_block, self.enemys, False, True)
e_hit_brick = pygame.sprite.groupcollide(self.maps.brick_block,self.enemys,True,True)
if e_hit_wall or e_hit_brick:
print(5)
self.enemy()#再创建一个敌方坦克
self.sound.boost()
#我方子弹撞墙
i_bullet_wall = pygame.sprite.groupcollide(self.maps.hard_block, Tank.my_all_bullet,
False, True)
if i_bullet_wall:
print(6)
self.sound.hit()
i_bullet_brick = pygame.sprite.groupcollide(self.maps.brick_block,Tank.my_all_bullet,
False,True)
if i_bullet_brick:
self.sound.hit()
for k in i_bullet_brick.keys():
k.attack -= 1
if k.attack == 0:
self.maps.brick_block.remove(k)
#敌方子弹撞墙
e_bullet_wall = pygame.sprite.groupcollide(self.maps.hard_block,
Tank.enemy_all_bullet, False, True)
if e_bullet_wall:
print(7)
self.sound.hit()
e_bullet_brick = pygame.sprite.groupcollide(self.maps.brick_block, Tank.enemy_all_bullet,
False, True)
if e_bullet_brick:
self.sound.hit()
for kk in e_bullet_brick.keys():
kk.attack -= 1
if kk.attack == 0:
self.maps.brick_block.remove(kk)
#检测墙体之间的碰撞(保证墙体不会重合)
for i in self.maps.hard_block:
for j in self.maps.hard_block:
if i != j:
walls_pump = pygame.sprite.collide_rect(i,j)
if walls_pump:
self.maps.hard_block.remove(i)
for k in self.maps.brick_block:
for l in self.maps.brick_block:
if k != l:
bricks_pump = pygame.sprite.collide_rect(k,l)
if bricks_pump:
self.maps.brick_block.remove(k)
pygame.sprite.groupcollide(self.maps.hard_block,self.maps.brick_block
,False,True)