python使用pygame模块实现坦克大战游戏

  

本文实例为大家分享了pygame模块实现坦克大战游戏的具体代码,供大家参考,具体内容如下

  

首先,第一步,游戏简单素材的准备。

  

 python使用pygame模块实现坦克大战游戏”>炮弹,<img src=   utf - 8编码:   #作者:comi   从gameloop进口*   从pygame进口*   进口pygame、sys、时间      if __name__==癬_main__”:   游戏玩家=()#声明一个类对象   player.game_start(继续)#调用开始函数   而球员。玩:#进入游戏运行   player.new() #开始游戏   player.screen.fill(黑)   player.game_start(“游戏结束”)#游戏结束   time . sleep(1.5) #可以不要      

这里可以根据自己的需要进行更改相关代码

  

接下来游戏的主循环

        utf - 8编码:   #作者:comi   从设置导入*   从pygame进口*   从精灵导入*   进口pygame,系统   vec=pygame.math.Vector2         类游戏:#游戏类包含循环等   def __init__(自我):#初始化   pygame.init () # pygame初始化   pygame.display.set_caption(继续)#游戏窗口左上角名称   自我。屏幕=pygame.display。set_mode((宽度、高度))#游戏窗口的大小   自我。FpsClock=pygame.time.Clock() #设置游戏的刷新率   自我。玩=True #进入游戏的状态   self.running=True #游戏运行的状态   自我。等待=True #游戏等待的状态   自我。Pblood=100 #玩家血量   自我。Eblood=100 #敌人血的量   自我。运动员=()#声明一个游戏玩家对象   自我。敌人的敌人=()#声明一个敌人对象   自我。all_groups=pygame.sprite.Group() #通过pygame自带的组来判断碰撞检测   自我。player_groups=pygame.sprite.Group ()   自我。Map_groups=pygame.sprite.Group ()   自我。Enemy_groups=pygame.sprite.Group ()      def新(自我):#开始一个游戏   self.player_groups.add (self.player) #将玩家添加到玩家组   self.all_groups.add (self.player) #将玩家添加到所有组      self.Enemy_groups.add (self.enemy)   self.all_groups.add (self.enemy)      在Map1 platfroms: #地图   p=平台(* platfroms) #取出所有值   self.Map_groups.add (p)   self.all_groups.add (p)      self.run() #调用函数运行游戏      def game_start(自我,文本):#游戏的开始界面   自我。text_draw(宽/2,高度/4,64年,文本)#文本   自我。text_draw(宽/2,高* 3/4,25日,按任意键继续,)#文本   pygame.display.update() #更行展示   而自我。等待:#实现按键等待开始效果   在pygame.event.get事件():   如果事件。类型==pygame.QUIT:   pygame.quit ()   西斯退出()   如果事件。类型==pygame.KEYDOWN:   自我。等待=False      def更新(自我):#画面更新   self.Map_groups.update ()   self.player_groups.update ()   self.enemy.Bullet_groups.update (self.enemy.flag) #通过按键判断子弹方向   self.player.Bullet_groups.update (self.player.flag)   self.Enemy_groups.update ()=pygame.sprite.groupcollide (self.player。Bullet_groups,自我。Map_groups,真、假)#子弹碰墙消失=pygame.sprite.groupcollide (self.enemy。Bullet_groups,自我。Map_groups,真、假)      PMC=pygame.sprite.spritecollide (self.player self.Map_groups,假,假)#撞的墙   如果PMC:   key_pressed=pygame.key.get_pressed ()   如果key_pressed (pygame.K_a):   self.player.pos。x=self.player.pos。x +差距   如果key_pressed (pygame.K_d):   self.player.pos。x=self.player.pos。x -差距   如果key_pressed (pygame.K_w):   self.player.pos。y=self.player.pos。y +差距   如果key_pressed (pygame.K_s):   self.player.pos。y=self.player.pos。y -差距      EMC=pygame.sprite.spritecollide (self.enemy self.Map_groups,假,假)#撞的墙   如果EMC:   key_pressed=pygame.key.get_pressed ()   如果key_pressed (pygame.K_LEFT):   self.enemy.pos。x=self.enemy.pos。x +差距   如果key_pressed (pygame.K_RIGHT):   self.enemy.pos。x=self.enemy.pos。x -差距   如果key_pressed (pygame.K_UP):   self.enemy.pos。y=self.enemy.pos。y +差距   如果key_pressed (pygame.K_DOWN):   self.enemy.pos。y=self.enemy.pos。y -差距      def运行(自我):   而self.running:   self.FpsClock.tick (Fps) #设置帧率   self.events() #获取事件   self.draw_pic() #画出图片   self.update ()      f的自我。Eblood & lt;=0: #的敌人   self.screen.fill(黑)   自我。game_start (P1赢!)   time . sleep (1.5)   self.running=False   自我。玩=False      如果自我。Pblood & lt;=0: #球员   self.screen.fill(黑)   自我。game_start (P2赢!)   time . sleep (1.5)   self.running=False   自我。玩=False      def text_draw(自我,x, y,大小、文本):#文本展示函数   自我。字体=pygame.font.Font (freesansbold。ttf,大小)#字体大,小   自我。text_surf=self.font。呈现(文本、真实、红色)#颜色   自我。text_rect=self.text_surf.get_rect() #矩形   self.text_rect。中心=(x, y) #位置   self.screen.blit(自我。text_surf self.text_rect) #覆盖展示      def draw_pic(自我):   self.screen.fill(白色)#背景   self.text_draw(900年,50岁,30岁,“保持”)#文本   自我。text_draw(900、100、30,“会”)      自我。text_draw(820年,150年,20岁的“P1:”)   自我。text_draw(820年,200年,20岁的“P2:”)      自我。text_draw(900年,250年,20岁的“注意!”)   自我。text_draw(900300年,20岁的“子弹”)   自我。text_draw(900年,350年,20岁的“控制!”)   自我。bar_draw (850、145、self.Pblood) #血条=pygame.sprite.groupcollide (self.enemy。Bullet_groups,自我。player_groups,真、假)#血条减少   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python使用pygame模块实现坦克大战游戏