当前位置:   article > 正文

用 Python 做“贪吃蛇”,在线吃不饱_python贪吃蛇小程序运行环境需求

python贪吃蛇小程序运行环境需求

一、基本环境配置

●版本:Python3

●系统:Windows

●相关模块:pygame

pip install pygame安装即可

二、实现效果

三、实现代码

  1. import random, pygame, sys
  2. from pygame.locals import *
  3. import time
  4. '''
  5. '''
  6. FPS = 1
  7. ##WINDOWWIDTH = 640
  8. #WINDOWHEIGHT = 480
  9. WINDOWWIDTH = 600
  10. WINDOWHEIGHT = 480
  11. CELLSIZE = 40
  12. assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size."
  13. assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size."
  14. CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
  15. CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)
  16. # R G B
  17. WHITE = (255, 255, 255)
  18. BLACK = ( 0, 0, 0)
  19. RED = (255, 0, 0)
  20. GREEN = ( 0, 255, 0)
  21. DARKGREEN = ( 0, 155, 0)
  22. DARKGRAY = ( 40, 40, 40)
  23. BGCOLOR = BLACK
  24. UP = 'up'
  25. DOWN = 'down'
  26. LEFT = 'left'
  27. RIGHT = 'right'
  28. direction = UP
  29. DIRECTION = [UP,DOWN,LEFT,RIGHT]
  30. HEAD = 0 # syntactic sugar: index of the worm's head
  31. distance = []
  32. for y in range(CELLHEIGHT):
  33. distance.append([])
  34. for x in range(CELLWIDTH):
  35. distance[y].append(8888)
  36. def into_queue(grid, queue, visited, worm,apple):
  37. x,y = grid
  38. if (x, y) == (apple['x'],apple['y']):
  39. return False
  40. elif x < 0 or x >= CELLWIDTH:
  41. return False
  42. elif y < 0 or y >= CELLHEIGHT:
  43. return False
  44. elif (x, y) in queue:
  45. return False
  46. elif (x, y) in visited:
  47. return False
  48. else:
  49. return True
  50. def is_snake(x,y,worm):
  51. for body in worm:
  52. if body['x'] == x and body['y'] == y:
  53. return True
  54. return False
  55. def cal_distance(worm,apple):
  56. queue = [(apple['x'],apple['y'])]
  57. visited = []
  58. found = False
  59. for y in range(CELLHEIGHT):
  60. for x in range(CELLWIDTH):
  61. distance[y][x] = 9999
  62. distance[apple['y']][apple['x']] = 0
  63. while len(queue) != 0:
  64. head = queue[0]
  65. visited.append(head)
  66. up_grid = head[0], head[1] - 1
  67. down_grid = head[0], head[1] + 1
  68. left_grid = head[0] - 1, head[1]
  69. right_grid = head[0] + 1, head[1]
  70. for grid in [up_grid, down_grid, left_grid, right_grid]:
  71. if into_queue(grid, queue, visited,worm,apple):
  72. if grid[0] == worm[HEAD]['x'] and grid[1] == worm[HEAD]['y']:
  73. found = True
  74. if not is_snake(grid[0],grid[1],worm):
  75. queue.append(grid)
  76. distance[grid[1]][grid[0]] = distance[head[1]][head[0]] + 1
  77. queue.pop(0)
  78. return found
  79. def main():
  80. global FPSCLOCK, DISPLAYSURF, BASICFONT
  81. pygame.init()
  82. FPSCLOCK = pygame.time.Clock()
  83. DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
  84. BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
  85. pygame.display.set_caption('Snaky')
  86. showStartScreen()
  87. while True:
  88. runGame()
  89. showGameOverScreen()
  90. def can_move(grid, worm):
  91. x,y = grid
  92. if x < 0 or x >= CELLWIDTH:
  93. return False
  94. elif y < 0 or y >= CELLHEIGHT:
  95. return False
  96. elif is_snake(x, y,worm):
  97. return False
  98. elif (x, y) == (worm[HEAD]['x'], worm[HEAD]['y']):
  99. return False
  100. else:
  101. return True
  102. def update_dirc(now, direc):
  103. loc = {'x':0,'y':0}
  104. if direc == UP:
  105. loc = {'x':now['x'],'y':now['y']-1}
  106. elif direc == DOWN:
  107. loc = {'x':now['x'],'y':now['y']+1}
  108. elif direc == RIGHT:
  109. loc = {'x':now['x']+1,'y':now['y']}
  110. elif direc == LEFT:
  111. loc = {'x':now['x']-1,'y':now['y']}
  112. return loc
  113. def virtual_run(wormCoords, apple,direction):
  114. wormCoords = list(wormCoords)
  115. food_eated = False
  116. while not food_eated:
  117. cal_distance(wormCoords,apple)
  118. four_dis = [99999, 99999, 99999, 99999]
  119. if can_move((wormCoords[HEAD]['x'], wormCoords[HEAD]['y'] - 1), wormCoords):
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/396586
推荐阅读
相关标签
  

闽ICP备14008679号