提示信息

用 Python 实现经典贪吃蛇游戏

落年 已关注

实现经典贪吃蛇游戏可以使用 Python 的 pygame 库来进行。下面是一个简单的贪吃蛇游戏的实现示例:

  1. import pygame
  2. import time
  3. import random
  4. pygame.init()
  5. # 定义一些常量
  6. WHITE = (255, 255, 255)
  7. YELLOW = (255, 255, 102)
  8. BLACK = (0, 0, 0)
  9. RED = (213, 50, 80)
  10. GREEN = (0, 255, 0)
  11. BLUE = (50, 153, 213)
  12. DISPLAY_WIDTH = 800
  13. DISPLAY_HEIGHT = 600
  14. SNAKE_BLOCK = 10
  15. SNAKE_SPEED = 15
  16. # 初始化显示窗口
  17. display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
  18. pygame.display.set_caption('贪吃蛇')
  19. clock = pygame.time.Clock()
  20. snake_font = pygame.font.SysFont(None, 50)
  21. def display_score(score):
  22. value = snake_font.render("Your Score: " + str(score), True, BLACK)
  23. display.blit(value, [0, 0])
  24. def draw_snake(snake_block, snake_list):
  25. for block in snake_list:
  26. pygame.draw.rect(display, BLACK, [block[0], block[1], snake_block, snake_block])
  27. def game_loop():
  28. game_over = False
  29. game_close = False
  30. x = DISPLAY_WIDTH / 2
  31. y = DISPLAY_HEIGHT / 2
  32. x_change = 0
  33. y_change = 0
  34. snake_list = []
  35. length_of_snake = 1
  36. food_x = round(random.randrange(0, DISPLAY_WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
  37. food_y = round(random.randrange(0, DISPLAY_HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
  38. while not game_over:
  39. while game_close:
  40. display.fill(BLUE)
  41. display_score(length_of_snake - 1)
  42. pygame.display.update()
  43. for event in pygame.event.get():
  44. if event.type == pygame.KEYDOWN:
  45. if event.key == pygame.K_q:
  46. game_over = True
  47. game_close = False
  48. if event.key == pygame.K_c:
  49. game_loop()
  50. for event in pygame.event.get():
  51. if event.type == pygame.QUIT:
  52. game_over = True
  53. if event.type == pygame.KEYDOWN:
  54. if event.key == pygame.K_LEFT:
  55. x_change = -SNAKE_BLOCK
  56. y_change = 0
  57. elif event.key == pygame.K_RIGHT:
  58. x_change = SNAKE_BLOCK
  59. y_change = 0
  60. elif event.key == pygame.K_UP:
  61. y_change = -SNAKE_BLOCK
  62. x_change = 0
  63. elif event.key == pygame.K_DOWN:
  64. y_change = SNAKE_BLOCK
  65. x_change = 0
  66. if x >= DISPLAY_WIDTH or x < 0 or y >= DISPLAY_HEIGHT or y < 0:
  67. game_close = True
  68. x += x_change
  69. y += y_change
  70. display.fill(BLUE)
  71. pygame.draw.rect(display, GREEN, [food_x, food_y, SNAKE_BLOCK, SNAKE_BLOCK])
  72. snake_head = []
  73. snake_head.append(x)
  74. snake_head.append(y)
  75. snake_list.append(snake_head)
  76. if len(snake_list) > length_of_snake:
  77. del snake_list[0]
  78. for block in snake_list[:-1]:
  79. if block == snake_head:
  80. game_close = True
  81. draw_snake(SNAKE_BLOCK, snake_list)
  82. display_score(length_of_snake - 1)
  83. pygame.display.update()
  84. if x == food_x and y == food_y:
  85. food_x = round(random.randrange(0, DISPLAY_WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
  86. food_y = round(random.randrange(0, DISPLAY_HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
  87. length_of_snake += 1
  88. clock.tick(SNAKE_SPEED)
  89. pygame.quit()
  90. quit()
  91. game_loop()

运行游戏步骤:

  1. 确保安装了 pygame 库,可以通过 pip install pygame 安装。
  2. 复制上述代码到一个 Python 文件中,比如 snake_game.py
  3. 运行脚本:python snake_game.py
  4. 使用方向键(上下左右)来控制蛇的移动。
  5. Q 结束游戏,按 C 重启游戏。

此实现可以用作理解 pygame 的基础,随后可以在此基础上加上更多的功能和改进,比如不同难度级别、音效、不同的画面等。

    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
落年 关注 已关注

最近一次登录:2024-11-20 22:26:44   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图