鱼C论坛

 找回密码
 立即注册
查看: 60|回复: 2

谁有Python贪吃蛇的代码啊,救急

[复制链接]
发表于 6 天前 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 5 天前 | 显示全部楼层
首先要下载pyfame
  1. pip install pygame
复制代码

代码:
  1. #!/usr/bin/env python
  2. import pygame,sys,time,random
  3. from pygame.locals import *
  4. # 定义颜色变量
  5. redColour = pygame.Color(255,0,0)
  6. blackColour = pygame.Color(0,0,0)
  7. whiteColour = pygame.Color(255,255,255)
  8. greyColour = pygame.Color(150,150,150)

  9. # 定义gameOver函数
  10. def gameOver(playSurface):
  11. gameOverFont = pygame.font.Font('arial.ttf',72)
  12. gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
  13. gameOverRect = gameOverSurf.get_rect()
  14. gameOverRect.midtop = (320, 10)
  15. playSurface.blit(gameOverSurf, gameOverRect)
  16. pygame.display.flip()
  17. time.sleep(5)
  18. pygame.quit()
  19. sys.exit()

  20. # 定义main函数
  21. def main():
  22. # 初始化pygame
  23. pygame.init()
  24. fpsClock = pygame.time.Clock()
  25. # 创建pygame显示层
  26. playSurface = pygame.display.set_mode((640,480))
  27. pygame.display.set_caption('Raspberry Snake')

  28. # 初始化变量
  29. snakePosition = [100,100]
  30. snakeSegments = [[100,100],[80,100],[60,100]]
  31. raspberryPosition = [300,300]
  32. raspberrySpawned = 1
  33. direction = 'right'
  34. changeDirection = direction
  35. while True:
  36. # 检测例如按键等pygame事件
  37. for event in pygame.event.get():
  38. if event.type == QUIT:
  39. pygame.quit()
  40. sys.exit()
  41. elif event.type == KEYDOWN:
  42. # 判断键盘事件
  43. if event.key == K_RIGHT or event.key == ord('d'):
  44. changeDirection = 'right'
  45. if event.key == K_LEFT or event.key == ord('a'):
  46. changeDirection = 'left'
  47. if event.key == K_UP or event.key == ord('w'):
  48. changeDirection = 'up'
  49. if event.key == K_DOWN or event.key == ord('s'):
  50. changeDirection = 'down'
  51. if event.key == K_ESCAPE:
  52. pygame.event.post(pygame.event.Event(QUIT))
  53. # 判断是否输入了反方向
  54. if changeDirection == 'right' and not direction == 'left':
  55. direction = changeDirection
  56. if changeDirection == 'left' and not direction == 'right':
  57. direction = changeDirection
  58. if changeDirection == 'up' and not direction == 'down':
  59. direction = changeDirection
  60. if changeDirection == 'down' and not direction == 'up':
  61. direction = changeDirection
  62. # 根据方向移动蛇头的坐标
  63. if direction == 'right':
  64. snakePosition[0] += 20
  65. if direction == 'left':
  66. snakePosition[0] -= 20
  67. if direction == 'up':
  68. snakePosition[1] -= 20
  69. if direction == 'down':
  70. snakePosition[1] += 20
  71. # 增加蛇的长度
  72. snakeSegments.insert(0,list(snakePosition))
  73. # 判断是否吃掉了树莓
  74. if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
  75. raspberrySpawned = 0
  76. else:
  77. snakeSegments.pop()
  78. # 如果吃掉树莓,则重新生成树莓
  79. if raspberrySpawned == 0:
  80. x = random.randrange(1,32)
  81. y = random.randrange(1,24)
  82. raspberryPosition = [int(x*20),int(y*20)]
  83. raspberrySpawned = 1
  84. # 绘制pygame显示层
  85. playSurface.fill(blackColour)
  86. for position in snakeSegments:
  87. pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20))
  88. pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],20,20))

  89. # 刷新pygame显示层
  90. pygame.display.flip()
  91. # 判断是否死亡
  92. if snakePosition[0] > 620 or snakePosition[0] < 0:
  93. gameOver(playSurface)
  94. if snakePosition[1] > 460 or snakePosition[1] < 0:
  95. for snakeBody in snakeSegments[1:]:
  96. if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
  97. gameOver(playSurface)
  98. # 控制游戏速度
  99. fpsClock.tick(5)

  100. if __name__ == "__main__":
  101. main()
复制代码


来源:脚本之家
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 5 天前 | 显示全部楼层
pygame小游戏:贪吃蛇 Snake
https://fishc.com.cn/thread-152245-1-1.html
(出处: 鱼C论坛)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1

GMT+8, 2021-4-14 09:40

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表