植物大战僵尸6 #
- 僵尸类的创建及移动
- 僵尸类的碰撞检测及攻击
- 僵尸类的窗口显示及更新
僵尸类的创建及功能 #
本小节主要介绍僵尸类class Zombie()的创建及移动方法;同时在主程序类class MainGame()中添加储存僵尸的列表及初始化方法
#9 僵尸类
class Zombie(pygame.sprite.Sprite):
def __init__(self,x,y):
super(Zombie, self).__init__()
self.image = pygame.image.load('imgs/zombie.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.hp = 1000
self.damage = 2
self.speed = 1
self.live = True
self.stop = False
#9 僵尸的移动
def move_zombie(self):
if self.live and not self.stop:
self.rect.x -= self.speed
if self.rect.x < -80:
#8 调用游戏结束方法
MainGame().gameOver()
#9 将僵尸加载到地图中
def display_zombie(self):
MainGame.window.blit(self.image,self.rect)
#class MainGame():
# 新增存储所有僵尸的列表
zombie_list = []
count_zombie = 0
produce_zombie = 100
# 新增初始化僵尸的方法
def init_zombies(self):
for i in range(1, 7):
dis = random.randint(1, 5) * 200
zombie = Zombie(800 + dis, i * 80)
MainGame.zombie_list.append(zombie)
#9 僵尸类
class Zombie(pygame.sprite.Sprite):
def __init__(self,x,y):
super(Zombie, self).__init__()
self.image = pygame.image.load('imgs/zombie.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.hp = 1000
self.damage = 2
self.speed = 1
self.live = True
self.stop = False
#9 僵尸的移动
def move_zombie(self):
if self.live and not self.stop:
self.rect.x -= self.speed
if self.rect.x < -80:
#8 调用游戏结束方法
MainGame().gameOver()
#9 将僵尸加载到地图中
def display_zombie(self):
MainGame.window.blit(self.image,self.rect)
#class MainGame():
# 新增存储所有僵尸的列表
zombie_list = []
count_zombie = 0
produce_zombie = 100
# 新增初始化僵尸的方法
def init_zombies(self):
for i in range(1, 7):
dis = random.randint(1, 5) * 200
zombie = Zombie(800 + dis, i * 80)
MainGame.zombie_list.append(zombie)
#9 僵尸类 class Zombie(pygame.sprite.Sprite): def __init__(self,x,y): super(Zombie, self).__init__() self.image = pygame.image.load('imgs/zombie.png') self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.hp = 1000 self.damage = 2 self.speed = 1 self.live = True self.stop = False #9 僵尸的移动 def move_zombie(self): if self.live and not self.stop: self.rect.x -= self.speed if self.rect.x < -80: #8 调用游戏结束方法 MainGame().gameOver() #9 将僵尸加载到地图中 def display_zombie(self): MainGame.window.blit(self.image,self.rect) #class MainGame(): # 新增存储所有僵尸的列表 zombie_list = [] count_zombie = 0 produce_zombie = 100 # 新增初始化僵尸的方法 def init_zombies(self): for i in range(1, 7): dis = random.randint(1, 5) * 200 zombie = Zombie(800 + dis, i * 80) MainGame.zombie_list.append(zombie)
僵尸类的碰撞检测及攻击 #
本小节主要介绍僵尸碰到植物时的攻击触发及攻击功能设计。
class Zombie(pygame.sprite.Sprite):
#9 判断僵尸是否碰撞到植物,如果碰撞,调用攻击植物的方法
def hit_plant(self):
for plant in MainGame.plants_list:
if pygame.sprite.collide_rect(self,plant):
#8 僵尸移动状态的修改
self.stop = True
self.eat_plant(plant)
#9 僵尸攻击植物
def eat_plant(self,plant):
#9 植物生命值减少
plant.hp -= self.damage
#9 植物死亡后的状态修改,以及地图状态的修改
if plant.hp <= 0:
a = plant.rect.y // 80 - 1
b = plant.rect.x // 80
map = MainGame.map_list[a][b]
map.can_grow = True
plant.live = False
#8 修改僵尸的移动状态
self.stop = False
class Zombie(pygame.sprite.Sprite):
#9 判断僵尸是否碰撞到植物,如果碰撞,调用攻击植物的方法
def hit_plant(self):
for plant in MainGame.plants_list:
if pygame.sprite.collide_rect(self,plant):
#8 僵尸移动状态的修改
self.stop = True
self.eat_plant(plant)
#9 僵尸攻击植物
def eat_plant(self,plant):
#9 植物生命值减少
plant.hp -= self.damage
#9 植物死亡后的状态修改,以及地图状态的修改
if plant.hp <= 0:
a = plant.rect.y // 80 - 1
b = plant.rect.x // 80
map = MainGame.map_list[a][b]
map.can_grow = True
plant.live = False
#8 修改僵尸的移动状态
self.stop = False
class Zombie(pygame.sprite.Sprite): #9 判断僵尸是否碰撞到植物,如果碰撞,调用攻击植物的方法 def hit_plant(self): for plant in MainGame.plants_list: if pygame.sprite.collide_rect(self,plant): #8 僵尸移动状态的修改 self.stop = True self.eat_plant(plant) #9 僵尸攻击植物 def eat_plant(self,plant): #9 植物生命值减少 plant.hp -= self.damage #9 植物死亡后的状态修改,以及地图状态的修改 if plant.hp <= 0: a = plant.rect.y // 80 - 1 b = plant.rect.x // 80 map = MainGame.map_list[a][b] map.can_grow = True plant.live = False #8 修改僵尸的移动状态 self.stop = False
僵尸类的窗口显示及更新 #
本小节主要介绍僵尸类的地图加载和窗口显示更新。
在主程序类class MainGame()中添加僵尸初始化self.init_zombies()
同时,由于游戏界面需要不断更新,需在主程序类class MainGame()中的开始游戏函数def start_game()中的循环while not GAMEOVER:中添加僵尸加载函数load_zombies()并对僵尸进行计数。
#class MainGame():
#9将所有僵尸加载到地图中
def load_zombies(self):
for zombie in MainGame.zombie_list:
if zombie.live:
zombie.display_zombie()
zombie.move_zombie()
# v2.0 调用是否碰撞到植物的方法
zombie.hit_plant()
else:
MainGame.zombie_list.remove(zombie)
#1 开始游戏
def start_game(self):
#9 调用初始化僵尸的方法
self.init_zombies()
while not GAMEOVER:
#9 调用展示僵尸的方法
self.load_zombies()
#9 计数器增长,每数到100,调用初始化僵尸的方法
MainGame.count_zombie += 1
if MainGame.count_zombie == MainGame.produce_zombie:
self.init_zombies()
self.init_zombies()
MainGame.count_zombie = 0
#class MainGame():
#9将所有僵尸加载到地图中
def load_zombies(self):
for zombie in MainGame.zombie_list:
if zombie.live:
zombie.display_zombie()
zombie.move_zombie()
# v2.0 调用是否碰撞到植物的方法
zombie.hit_plant()
else:
MainGame.zombie_list.remove(zombie)
#1 开始游戏
def start_game(self):
#9 调用初始化僵尸的方法
self.init_zombies()
while not GAMEOVER:
#9 调用展示僵尸的方法
self.load_zombies()
#9 计数器增长,每数到100,调用初始化僵尸的方法
MainGame.count_zombie += 1
if MainGame.count_zombie == MainGame.produce_zombie:
self.init_zombies()
self.init_zombies()
MainGame.count_zombie = 0
#class MainGame(): #9将所有僵尸加载到地图中 def load_zombies(self): for zombie in MainGame.zombie_list: if zombie.live: zombie.display_zombie() zombie.move_zombie() # v2.0 调用是否碰撞到植物的方法 zombie.hit_plant() else: MainGame.zombie_list.remove(zombie) #1 开始游戏 def start_game(self): #9 调用初始化僵尸的方法 self.init_zombies() while not GAMEOVER: #9 调用展示僵尸的方法 self.load_zombies() #9 计数器增长,每数到100,调用初始化僵尸的方法 MainGame.count_zombie += 1 if MainGame.count_zombie == MainGame.produce_zombie: self.init_zombies() self.init_zombies() MainGame.count_zombie = 0
小结 #
本节主要介绍了植物大战游戏中僵尸类的移动、攻击触发及窗口显示