python实现贪吃蛇游戏的具体代码

  介绍

这篇文章将为大家详细讲解有关python实现贪吃蛇游戏的具体代码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

python实现贪吃蛇游戏的具体代码

# !/usr/bin/env  python   # __ * __ 编码:utf-8  __ * __   ,   import  pygame sys、时间、随机的   得到pygame.locals  import  *   ,   ,   时间=redColour  pygame.Color (255, 0, 0)   时间=blackColour  pygame.Color (0, 0, 0)   时间=whiteColour  pygame.Color (255255255)   时间=greyColour  pygame.Color (150150150)   ,   def 败阵(playSurface):=,,gameOverFont  pygame.font.Font (& # 39; arial.ttf& # 39;, 72)=,,gameOverSurf  gameOverFont.render (& # 39; Game 在# 39;,真的,greyColour)=,,gameOverRect  gameOverSurf.get_rect ()   ,gameOverRect.midtop =,(320年,10)   ,playSurface.blit (gameOverSurf gameOverRect)   ,pygame.display.flip ()   ,time . sleep (5)   ,pygame.quit ()   ,sys.exit ()   ,   def  main ():   ,pygame.init ()=,,playSurface  pygame.display.set_mode ((640480))=,,fpsClock  pygame.time.Clock ()   ,pygame.display.set_caption (& # 39; Snake 刘# 39;)   ,=,snakePosition  [100100]=,snakeSegments  [[100100], [80100], [60100]]=,raspberryPosition  [300300]   raspberrySpawned =, 1=,direction  & # 39;对# 39;   changeDirection =,方向   ,while 真正的:   ,for  event  pygame.event.get拷贝():   ,if  event.type ==,退出:   ,sys.exit ()   ,elif  event.type ==, KEYDOWN:   ,if  event.key ==, K_RIGHT 或是event.key ==,奥德(& # 39;d # 39;):   时间=changeDirection 才能;& # 39;对# 39;   ,if  event.key ==, K_LEFT 或是event.key ==,奥德(& # 39;一个# 39;):   时间=changeDirection 才能;& # 39;左# 39;   ,if  event.key ==, K_UP 或是event.key ==,奥德(& # 39;w # 39;):   时间=changeDirection 才能;& # 39;了# 39;   ,if  event.key ==, K_DOWN 或是event.key ==,奥德(& # 39;& # 39;):   时间=changeDirection 才能;& # 39;下来# 39;   ,if  event.key ==, K_ESCAPE:   pygame.event.post才能(pygame.event.Event(辞职)   ,#判断是否输入了反方向,与原文有改动==,if  changeDirection  & # 39;对# 39;:=,direction  changeDirection==,if  changeDirection  & # 39;左# 39;:=,direction  changeDirection==,if  changeDirection  & # 39;了# 39;:=,direction  changeDirection==,if  changeDirection  & # 39;下来# 39;:=,direction  changeDirection   ,#根据方向移动蛇头的坐标==,if  direction  & # 39;对# 39;:   ,snakePosition [0], +=20==,if  direction  & # 39;左# 39;:   ,snakePosition [0], -=20==,if  direction  & # 39;了# 39;:   ,snakePosition [1], -=20==,if  direction  & # 39;下来# 39;:   ,snakePosition [1], +=20   ,#增加蛇的长度   ,snakeSegments.insert(0,列表(snakePosition))   ,#判断是否吃掉了树莓   ,if  snakePosition [0],==, raspberryPosition[0],以及snakePosition [1],==, raspberryPosition [1]:   raspberrySpawned =, 0   ,其他的:   ,snakeSegments.pop ()   ,#如果吃掉树莓,则重新生成树莓   ,if  raspberrySpawned ==, 0:=,,x  random.randrange (32)=,,y  random.randrange (24)=,raspberryPosition  [int (x * 20), int (y * 20)]   raspberrySpawned =, 1   ,   ,#刷新pygame显示层   ,playSurface.fill (blackColour)   ,for  position  snakeSegments拷贝:   ,pygame.draw.rect (playSurface whiteColour矩形(位置[0],[1],20日,20))   ,pygame.draw.rect (playSurface redColour矩形(raspberryPosition [0],, raspberryPosition[1], 20日,20))   ,pygame.display.flip ()   ,   ,#判断是否死亡,后面几行和原文有改动   ,if  snakePosition[0],祝辞,620,趁机snakePosition [0], & lt;, 0:   ,游戏结束(playSurface)   ,if  snakePosition[1],在,460年,趁机snakePosition [1], & lt;, 0:   ,游戏结束(playSurface)   ,for  snakeBody  snakeSegments拷贝(1:):   ,if  snakePosition [0],==, snakeBody[0],以及snakePosition [1],==, snakeBody [1]:   ,游戏结束(playSurface)   ,fpsClock.tick (5)   if  __name__ ==,“__main__":   之前,main () python实现贪吃蛇游戏的具体代码