科学教育

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

帮我生成一个贪吃蛇代码

[复制链接]

1

主题

2

帖子

9

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-11-4 09:49:42 | 只看该作者
当我们在编写贪吃蛇游戏时,首先需要考虑游戏的基本元素,包括蛇的移动、食物的生成、游戏结束的判断等。接下来我会给你一个简单的贪吃蛇代码:

  1. import pygame
  2. import random

  3. pygame.init()

  4. # 设置屏幕大小
  5. screen_width = 600
  6. screen_height = 400

  7. # 设置颜色
  8. black = (0, 0, 0)
  9. white = (255, 255, 255)
  10. red = (255, 0, 0)

  11. # 初始化屏幕
  12. screen = pygame.display.set_mode((screen_width, screen_height))
  13. pygame.display.set_caption('贪吃蛇游戏')

  14. # 蛇的初始位置和长度
  15. snake_pos = [100, 50]
  16. snake_body = [[100, 50], [90, 50], [80, 50]]
  17. # 食物的初始位置
  18. food_pos = [random.randrange(1, screen_width//10)*10, random.randrange(1, screen_height//10)*10]
  19. food_spawn = True

  20. # 设置方向
  21. direction = 'RIGHT'
  22. change_to = direction

  23. # 设置速度
  24. snake_speed = 15
  25. clock = pygame.time.Clock()

  26. # 定义游戏结束函数
  27. def game_over():
  28.     font_style = pygame.font.SysFont(None, 50)
  29.     message = font_style.render('Game Over!', True, red)
  30.     screen.blit(message, [screen_width/2-100, screen_height/2-50])
  31.     pygame.display.flip()
  32.     pygame.time.wait(2000)
  33.     pygame.quit()

  34. # 游戏主循环
  35. while True:
  36.     for event in pygame.event.get():
  37.         if event.type == pygame.QUIT:
  38.             pygame.quit()

  39.         # 检测键盘输入
  40.         elif event.type == pygame.KEYDOWN:
  41.             if event.key == pygame.K_RIGHT:
  42.                 change_to = 'RIGHT'
  43.             if event.key == pygame.K_LEFT:
  44.                 change_to = 'LEFT'
  45.             if event.key == pygame.K_UP:
  46.                 change_to = 'UP'
  47.             if event.key == pygame.K_DOWN:
  48.                 change_to = 'DOWN'

  49.     # 碰撞检测
  50.     if snake_pos[0] >= screen_width or snake_pos[0] < 0 or snake_pos[1] >= screen_height or snake_pos[1] < 0:
  51.         game_over()
  52.    
  53.     for block in snake_body[1:]:
  54.         if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
  55.             game_over()

  56.     # 更新蛇的位置
  57.     if change_to == 'RIGHT' and not direction == 'LEFT':
  58.         direction = 'RIGHT'
  59.     if change_to == 'LEFT' and not direction == 'RIGHT':
  60.         direction = 'LEFT'
  61.     if change_to == 'UP' and not direction == 'DOWN':
  62.         direction = 'UP'
  63.     if change_to == 'DOWN' and not direction == 'UP':
  64.         direction = 'DOWN'

  65.     if direction == 'RIGHT':
  66.         snake_pos[0] += 10
  67.     if direction == 'LEFT':
  68.         snake_pos[0] -= 10
  69.     if direction == 'UP':
  70.         snake_pos[1] -= 10
  71.     if direction == 'DOWN':
  72.         snake_pos[1] += 10

  73.     snake_body.insert(0, list(snake_pos))
  74.     if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
  75.         food_spawn = False
  76.     else:
  77.         snake_body.pop()

  78.     if not food_spawn:
  79.         food_pos = [random.randrange(1, screen_width//10)*10, random.randrange(1, screen_height//10)*10]
  80.         food_spawn = True
  81.    
  82.     screen.fill(black)
  83.    
  84.     for pos in snake_body:
  85.         pygame.draw.rect(screen, white, pygame.Rect(pos[0], pos[1], 10, 10))
  86.    
  87.     pygame.draw.rect(screen, red, pygame.Rect(food_pos[0], food_pos[1], 10, 10))

  88.     pygame.display.update()
  89.    
  90.     clock.tick(snake_speed)
复制代码


这是一个简单的贪吃蛇小游戏的Python代码,通过上面的代码,你可以在Python的环境中执行并运行这个贪吃蛇游戏。希望对你有所帮助!
回复 支持 反对

使用道具 举报

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

本版积分规则

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