当前位置:   article > 正文

python贪吃蛇游戏代码详解,python贪吃蛇最简单代码_贪吃蛇小游戏python代码

贪吃蛇小游戏python代码

大家好,小编为大家解答python贪吃蛇游戏代码详解外加中文的问题。很多人还不知道python贪吃蛇游戏代码详解,现在让我们一起来看看吧!

简单的贪吃蛇小游戏的 Python 代码:

  1. import pygame
  2. import random
  3. # 初始化
  4. pygame.init()
  5. # 游戏窗口大小
  6. width = 800
  7. height = 600
  8. # 设置游戏窗口
  9. screen = pygame.display.set_mode((width, height))
  10. pygame.display.set_caption("Snake Game")
  11. # 颜色
  12. white = (255, 255, 255)
  13. black = (0, 0, 0)
  14. red = (255, 0, 0)
  15. green = (0, 255, 0)
  16. # 蛇的大小
  17. block_size = 10
  18. # 字体
  19. font_style = pygame.font.SysFont(None, 30)
  20. # 显示消息
  21. def message(msg, color):
  22. message = font_style.render(msg, True, color)
  23. screen.blit(message, [width / 6, height / 3])
  24. # 绘制蛇
  25. def draw_snake(block_size, snake_List):
  26. for x in snake_List:
  27. pygame.draw.rect(screen, green, [x[0], x[1], block_size, block_size])
  28. # 游戏循环
  29. def gameLoop():
  30. game_over = False
  31. game_close = False
  32. # 蛇的初始位置
  33. x1 = width / 2
  34. y1 = height / 2
  35. # 蛇的移动距离
  36. x1_change = 0
  37. y1_change = 0
  38. # 初始长度
  39. snake_List = []
  40. Length_of_snake = 1
  41. # 食物的位置
  42. foodx = round(random.randrange(0, width - block_size) / 10.0) * 10.0
  43. foody = round(random.randrange(0, height - block_size) / 10.0) * 10.0
  44. # 游戏循环
  45. while not game_over:
  46. # 游戏结束
  47. while game_close == True:
  48. screen.fill(white)
  49. message("You Lost! Press Q-Quit or C-Play Again", red)
  50. pygame.display.update()
  51. # 判断按键
  52. for event in pygame.event.get():
  53. if event.type == pygame.KEYDOWN:
  54. if event.key == pygame.K_q:
  55. game_over = True
  56. game_close = False
  57. if event.key == pygame.K_c:
  58. gameLoop()
  59. # 判断按键
  60. for event in pygame.event.get():
  61. if event.type == pygame.QUIT:
  62. game_over = True
  63. if event.type == pygame.KEYDOWN:
  64. if event.key == pygame.K_LEFT:
  65. x1_change = -block_size
  66. y1_change = 0
  67. elif event.key == pygame.K_RIGHT:
  68. x1_change = block_size
  69. y1_change = 0
  70. elif event.key == pygame.K_UP:
  71. y1_change = -block_size
  72. x1_change = 0
  73. elif event.key == pygame.K_DOWN:
  74. y1_change = block_size
  75. x1_change = 0
  76. # 判断蛇是否出界
  77. if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
  78. game_close = True
  79. # 移动蛇
  80. x1 += x1_change
  81. y1 += y1_change
  82. # 绘制食物
  83. screen.fill(white)
  84. pygame.draw.rect(screen, red, [foodx, foody, block_size, block_size])
  85. # 蛇的长度
  86. snake_Head = []
  87. snake_Head.append(x1)
  88. snake_Head.append(y1)
  89. snake_List.append(snake_Head)
  90. if len(snake_List) > Length_of_snake:
  91. del snake_List[0]
  92. # 判断蛇是否吃到食物
  93. for x in snake_List[:-1]:
  94. if x == snake_Head:
  95. game_close = True
  96. # 绘制蛇
  97. draw_snake(block_size, snake_List)
  98. pygame.display.update()
  99. # 判断蛇是否吃到食物
  100. if x1 == foodx and y1 == foody:
  101. foodx = round(random.randrange(0, width - block_size) / 10.0) * 10.0
  102. foody = round(random.randrange(0, height - block_size) / 10.0) * 10.0
  103. Length_of_snake += 1
  104. # 刷新屏幕
  105. pygame.display.update()
  106. # 退出游戏
  107. pygame.quit()
  108. quit()
  109. # 运行游戏
  110. gameLoop()

文章知识点与官方知识档案匹配,可进一步学习相关知识
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/741743
推荐阅读
相关标签
  

闽ICP备14008679号