python坦克大战(4)玩家坦克和敌对坦克

mac2022-06-30  122

坦克大战4

1.玩家坦克

class Player: """玩家管理""" def __init__(self,view_manager,screen): #生命 self.hp = 100 #杀敌数量 self.kill = 0 #动作 self.move = pv.MOVE_STAND #记录最近一次保持的动作;初始化的时候是朝上 self.lastmove = pv.MOVE_UP #移动速度 self.speed = 5 #玩家坐标 self.x = view_manager.screen_width / 2 self.y = view_manager.screen_height * 3 / 4 #玩家发射的子弹 self.bullets = [] #当前炮口的中心坐标 self.guncenterx = -1 self.guncentery = -1 #发射炮弹的间隔 self.firedelay = 0.3 #上次发射炮弹的时间 self.firetime = datetime.datetime.now() - datetime.timedelta(days=1) #字体 self.font = view_manager.font[1] def is_die(self): if self.hp <= 0: return True else: return False #控制角色移动 def move_position(self,view_manager,screen): if self.move == pv.MOVE_STAND: return # 更新角色位置 if self.move == pv.MOVE_LEFT: self.x-=self.speed #左边界判断 if self.x <= 0: self.x = 0 elif self.move == pv.MOVE_RIGHT: self.x+=self.speed #右边界判断 bodysize = view_manager.player_body[3].get_rect() if self.x + bodysize.width >= view_manager.screen_width : self.x = view_manager.screen_width - bodysize.width elif self.move == pv.MOVE_UP: self.y-=self.speed #右边界判断 if self.y <= 0: self.y = 0 elif self.move == pv.MOVE_DOWN: self.y+=self.speed #下边界判断 bodysize = view_manager.player_body[1].get_rect() if self.y + bodysize.height >= view_manager.screen_height : self.y = view_manager.screen_height - bodysize.height #当前图像范围 def getrect(self ,view_manager): if self.lastmove == pv.MOVE_UP : return view_manager.player_body[0].get_rect() if self.lastmove == pv.MOVE_DOWN: return view_manager.player_body[1].get_rect() if self.lastmove == pv.MOVE_LEFT: return view_manager.player_body[2].get_rect() if self.lastmove == pv.MOVE_RIGHT: return view_manager.player_body[3].get_rect() return None #绘制角色 def draw(self,view_manager,screen): direct = self.move # 绘制角色站立的位置 if self.move == pv.MOVE_STAND: #未进行任何操作的时候,保持最后一次控制的方向 direct = self.lastmove if direct == pv.MOVE_UP: screen.blit(view_manager.player_body[0],(self.x,self.y)) if direct == pv.MOVE_DOWN: screen.blit(view_manager.player_body[1],(self.x,self.y)) if direct == pv.MOVE_LEFT: screen.blit(view_manager.player_body[2],(self.x,self.y)) if direct == pv.MOVE_RIGHT: screen.blit(view_manager.player_body[3],(self.x,self.y)) #绘制矩形角色血量条 pygame.draw.rect(screen,pv.COLOR_RED,Rect((self.x + 5 , self.y - 5),(self.hp , 5))) #绘制杀敌数量 killtxt = self.font.render('击毁:' + str(self.kill) ,1,pv.COLOR_RED) screen.blit(killtxt,(5,5)) #角色控制 def work(self,screen,view_manager): #角色移动 self.move_position(view_manager,screen) #绘制角色 self.draw(view_manager,screen) #移除已经失效的炮弹 for b in self.bullets: if b.is_effect == False: self.bullets.remove(b) #绘制角色发射的炮弹 for b in self.bullets: b.move_position(view_manager,screen) b.draw(view_manager,screen) #炮口的坐标 def guncenter(self,view_manager): if self.lastmove == pv.MOVE_UP : bodyrect = view_manager.player_body[0].get_rect() self.guncenterx = self.x + bodyrect.width / 2 - 4 self.guncentery = self.y return if self.lastmove == pv.MOVE_DOWN: bodyrect = view_manager.player_body[1].get_rect() self.guncenterx = self.x + bodyrect.width / 2 - 4 self.guncentery = self.y + bodyrect.height return if self.lastmove == pv.MOVE_LEFT: bodyrect = view_manager.player_body[2].get_rect() self.guncenterx = self.x self.guncentery = self.y + bodyrect.height / 2 - 4 return if self.lastmove == pv.MOVE_RIGHT: bodyrect = view_manager.player_body[3].get_rect() self.guncenterx = self.x + bodyrect.width self.guncentery = self.y + bodyrect.height / 2 - 4 return #发射炮弹 def fire(self,screen,view_manager): #每间隔1秒发射一次 firepass = (datetime.datetime.now() - self.firetime).microseconds / 1000000 if firepass < self.firedelay : return self.firetime = datetime.datetime.now() #更新炮口坐标 self.guncenter(view_manager) #创建炮弹 #根据当前的面向设置炮弹 newbullet = Bullet(view_manager,screen,self.guncenterx,self.guncentery,self.lastmove) #模拟爆炸 #newbullet.is_bomb = True #记录炮弹 self.bullets.append(newbullet) #播放音乐:发射 view_manager.sounds[0].play()

2.敌对坦克

class Monster: """敌人处理""" def __init__(self,view_manager,screen): #血量 self.hp = 100 #动作 self.move = pv.MOVE_DOWN #记录最近一次保持的动作;初始化的时候是朝上 self.lastmove = pv.MOVE_DOWN #移动速度 self.speed = 4 #随机出现坐标在屏幕上半部分 self.x = random.randint(100,view_manager.screen_width - 300) self.y = random.randint(0,view_manager.screen_height / 2) #发射的子弹 self.bullets = [] #当前炮口的中心坐标 self.guncenterx = -1 self.guncentery = -1 #发射炮弹的间隔 self.firedelay = 1 #上次发射炮弹的时间 self.firetime = datetime.datetime.now() - datetime.timedelta(days=1) #更改方向的间隔 self.changemovedelay = random.randint(0,3) #上次更改移动方向的时间 self.changemovetime = datetime.datetime.now() - datetime.timedelta(days=1) #控制移动 def move_position(self,view_manager,screen): #重新生成移动方向 changepass = (datetime.datetime.now() - self.changemovetime).seconds if changepass > self.changemovedelay : self.changemovetime = datetime.datetime.now() #随机生成4个方向状态 self.move = random.randint(1,4) self.lastmove = self.move #随机下次变向间隔 self.changemovedelay = random.randint(0,3) #更新角色位置 # 站立不动 if self.move == pv.MOVE_STAND: return # 向左 if self.move == pv.MOVE_LEFT: self.x-=self.speed #左边界判断 if self.x <= 0: self.x = 0 self.move = pv.MOVE_RIGHT self.lastmove = self.move # 向右 elif self.move == pv.MOVE_RIGHT: self.x+=self.speed #右边界判断 bodysize = view_manager.monster_body[3].get_rect() if self.x + bodysize.width >= view_manager.screen_width : self.x = view_manager.screen_width - bodysize.width self.move = pv.MOVE_LEFT self.lastmove = self.move # 向上 elif self.move == pv.MOVE_UP: self.y-=self.speed #右边界判断 if self.y <= 0: self.y = 0 self.move = pv.MOVE_DOWN self.lastmove = self.move # 向下 elif self.move == pv.MOVE_DOWN: self.y+=self.speed #下边界判断 bodysize = view_manager.monster_body[1].get_rect() if self.y + bodysize.height >= view_manager.screen_height : self.y = view_manager.screen_height - bodysize.height self.move = pv.MOVE_UP self.lastmove = self.move #当前主体范围 def getrect(self ,view_manager): if self.lastmove == pv.MOVE_UP : return view_manager.monster_body[0].get_rect() if self.lastmove == pv.MOVE_DOWN: return view_manager.monster_body[1].get_rect() if self.lastmove == pv.MOVE_LEFT: return view_manager.monster_body[2].get_rect() if self.lastmove == pv.MOVE_RIGHT: return view_manager.monster_body[3].get_rect() return None #绘制角色 def draw(self,view_manager,screen): direct = self.move # 绘制角色站立的位置 if direct == pv.MOVE_STAND: #未进行任何操作的时候,保持最后一次控制的方向 direct = self.lastmove if direct == pv.MOVE_UP: screen.blit(view_manager.monster_body[0],(self.x,self.y)) if direct == pv.MOVE_DOWN: screen.blit(view_manager.monster_body[1],(self.x,self.y)) if direct == pv.MOVE_LEFT: screen.blit(view_manager.monster_body[2],(self.x,self.y)) if direct == pv.MOVE_RIGHT: screen.blit(view_manager.monster_body[3],(self.x,self.y)) #绘制矩形角色血量条 pygame.draw.rect(screen,pv.COLOR_RED,Rect((self.x + 5 , self.y - 5),(self.hp , 5))) #控制 def work(self,screen,view_manager): #角色移动 self.move_position(view_manager,screen) #绘制角色 self.draw(view_manager,screen) #发射炮弹 self.fire(screen,view_manager) #移除已经失效的炮弹 for b in self.bullets: if b.is_effect == False: self.bullets.remove(b) #绘制角色发射的炮弹 for b in self.bullets: b.move_position(view_manager,screen) b.draw(view_manager,screen) #炮口的坐标 def guncenter(self,view_manager): if self.lastmove == pv.MOVE_UP : bodyrect = view_manager.monster_body[0].get_rect() self.guncenterx = self.x + bodyrect.width / 2 - 4 self.guncentery = self.y return if self.lastmove == pv.MOVE_DOWN: bodyrect = view_manager.monster_body[1].get_rect() self.guncenterx = self.x + bodyrect.width / 2 - 4 self.guncentery = self.y + bodyrect.height return if self.lastmove == pv.MOVE_LEFT: bodyrect = view_manager.monster_body[2].get_rect() self.guncenterx = self.x self.guncentery = self.y + bodyrect.height / 2 - 4 return if self.lastmove == pv.MOVE_RIGHT: bodyrect = view_manager.monster_body[3].get_rect() self.guncenterx = self.x + bodyrect.width self.guncentery = self.y + bodyrect.height / 2 - 4 return #发射炮弹 def fire(self,screen,view_manager): #每间隔1秒发射一次 firepass = (datetime.datetime.now() - self.firetime).seconds if firepass < self.firedelay : return self.firetime = datetime.datetime.now() #更新炮口坐标 self.guncenter(view_manager) #创建炮弹 #根据当前的面向设置炮弹从炮口中央发射 newbullet = Bullet(view_manager,screen,self.guncenterx,self.guncentery,self.lastmove) #记录炮弹 self.bullets.append(newbullet) #播放音乐:发射 view_manager.sounds[0].play()

python坦克大战(5)战斗处理与炮弹

最新回复(0)