科学教育

搜索
查看: 643|回复: 1
打印 上一主题 下一主题

[复制链接]

2

主题

2

帖子

12

积分

新手上路

Rank: 1

积分
12
跳转到指定楼层
楼主
发表于 2025-9-30 15:26:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成一个贪吃蛇游戏代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-9-30 15:26:58 | 只看该作者
当然可以!贪吃蛇是一种经典的游戏,让我教你如何用Python语言编写一个简单的贪吃蛇游戏。

首先,你需要安装一个Python的编辑器,比如Visual Studio Code或者PyCharm。然后创建一个新的Python文件,将以下代码粘贴到文件中:

  1. import pygame
  2. import time
  3. import random

  4. pygame.init()

  5. # 设置游戏界面大小
  6. screen_width = 800
  7. screen_height = 600

  8. # 定义颜色
  9. white = (255, 255, 255)
  10. black = (0, 0, 0)
  11. red = (255, 0, 0)

  12. # 设置贪吃蛇和食物的大小
  13. snake_block = 10
  14. snake_speed = 15

  15. # 创建屏幕对象
  16. game_display = pygame.display.set_mode((screen_width, screen_height))
  17. pygame.display.set_caption('Snake Game')

  18. clock = pygame.time.Clock()

  19. font_style = pygame.font.SysFont(None, 50)

  20. def our_snake(snake_block, snake_list):
  21.     for x in snake_list:
  22.         pygame.draw.rect(game_display, black, [x[0], x[1], snake_block, snake_block])

  23. def message(msg, color):
  24.     mesg = font_style.render(msg, True, color)
  25.     game_display.blit(mesg, [screen_width / 6, screen_height / 3])

  26. def gameLoop():
  27.     game_over = False
  28.     game_close = False

  29.     x1 = screen_width / 2
  30.     y1 = screen_height / 2

  31.     x1_change = 0
  32.     y1_change = 0

  33.     snake_list = []
  34.     length_of_snake = 1

  35.     foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0
  36.     foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0

  37.     while not game_over:

  38.         while game_close == True:
  39.             game_display.fill(white)
  40.             message("You Lost! Press Q-Quit or C-Play Again", red)
  41.             pygame.display.update()

  42.             for event in pygame.event.get():
  43.                 if event.type == pygame.KEYDOWN:
  44.                     if event.key == pygame.K_q:
  45.                         game_over = True
  46.                         game_close = False
  47.                     if event.key == pygame.K_c:
  48.                         gameLoop()

  49.         for event in pygame.event.get():
  50.             if event.type == pygame.QUIT:
  51.                 game_over = True
  52.             if event.type == pygame.KEYDOWN:
  53.                 if event.key == pygame.K_LEFT:
  54.                     x1_change = -snake_block
  55.                     y1_change = 0
  56.                 elif event.key == pygame.K_RIGHT:
  57.                     x1_change = snake_block
  58.                     y1_change = 0
  59.                 elif event.key == pygame.K_UP:
  60.                     y1_change = -snake_block
  61.                     x1_change = 0
  62.                 elif event.key == pygame.K_DOWN:
  63.                     y1_change = snake_block
  64.                     x1_change = 0

  65.         if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
  66.             game_close = True

  67.         x1 += x1_change
  68.         y1 += y1_change
  69.         game_display.fill(white)
  70.         pygame.draw.rect(game_display, red, [foodx, foody, snake_block, snake_block])
  71.         snake_head = []
  72.         snake_head.append(x1)
  73.         snake_head.append(y1)
  74.         snake_list.append(snake_head)
  75.         
  76.         if len(snake_list) > length_of_snake:
  77.             del snake_list[0]

  78.         for x in snake_list[:-1]:
  79.             if x == snake_head:
  80.                 game_close = True

  81.         our_snake(snake_block, snake_list)

  82.         pygame.display.update()

  83.         if x1 == foodx and y1 == foody:
  84.             foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0
  85.             foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0
  86.             length_of_snake += 1

  87.         clock.tick(snake_speed)

  88.     pygame.quit()
  89.     quit()

  90. gameLoop()
复制代码


这段代码实现了一个最基本的贪吃蛇游戏,玩家通过键盘控制蛇的移动,吃到食物增加长度,当蛇碰到边界或者自己的身体时游戏结束。

希望以上代码对你有所帮助。如果你遇到任何问题或者想要了解更多,请随时告诉我哦!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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