鱼C论坛

 找回密码
 立即注册
查看: 28|回复: 2

ai求助

[复制链接]
发表于 昨天 23:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
怎么用python写贪吃蛇啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 23:15 | 显示全部楼层
当然!用Python写贪吃蛇是一个很好的练习项目,它能帮你巩固编程基础。下面是一个使用 pygame 库实现的简单贪吃蛇游戏教程。

第一步:安装pygame
如果你还没有安装pygame,请先安装它:

  1. pip install pygame
复制代码


第二步:编写贪吃蛇游戏代码
将以下代码保存为 snake_game.py


  1. import pygame
  2. import random

  3. # 初始化pygame
  4. pygame.init()

  5. # 定义颜色
  6. white = (255, 255, 255)
  7. black = (0, 0, 0)
  8. red = (213, 50, 80)
  9. green = (0, 255, 0)
  10. blue = (50, 153, 213)

  11. # 设置游戏窗口
  12. dis_width = 800
  13. dis_height = 600
  14. dis = pygame.display.set_mode((dis_width, dis_height))
  15. pygame.display.set_caption('贪吃蛇游戏')

  16. # 设置游戏时钟
  17. clock = pygame.time.Clock()

  18. # 蛇的大小和速度
  19. snake_block = 10
  20. snake_speed = 15

  21. # 设置字体
  22. font_style = pygame.font.SysFont(None, 50)
  23. score_font = pygame.font.SysFont(None, 35)

  24. # 显示得分
  25. def your_score(score):
  26.     value = score_font.render("得分: " + str(score), True, white)
  27.     dis.blit(value, [0, 0])

  28. # 绘制蛇
  29. def our_snake(snake_block, snake_list):
  30.     for x in snake_list:
  31.         pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])

  32. # 显示消息
  33. def message(msg, color):
  34.     mesg = font_style.render(msg, True, color)
  35.     dis.blit(mesg, [dis_width / 6, dis_height / 3])

  36. # 游戏主循环
  37. def gameLoop():
  38.     game_over = False
  39.     game_close = False

  40.     # 初始化蛇的位置
  41.     x1 = dis_width / 2
  42.     y1 = dis_height / 2

  43.     # 初始化蛇的移动方向
  44.     x1_change = 0
  45.     y1_change = 0

  46.     # 初始化蛇的身体
  47.     snake_List = []
  48.     Length_of_snake = 1

  49.     # 随机生成食物位置
  50.     foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  51.     foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0

  52.     while not game_over:

  53.         while game_close == True:
  54.             dis.fill(black)
  55.             message("游戏结束! 按Q退出或C重新开始", red)
  56.             your_score(Length_of_snake - 1)
  57.             pygame.display.update()

  58.             for event in pygame.event.get():
  59.                 if event.type == pygame.KEYDOWN:
  60.                     if event.key == pygame.K_q:
  61.                         game_over = True
  62.                         game_close = False
  63.                     if event.key == pygame.K_c:
  64.                         gameLoop()

  65.         for event in pygame.event.get():
  66.             if event.type == pygame.QUIT:
  67.                 game_over = True
  68.             if event.type == pygame.KEYDOWN:
  69.                 if event.key == pygame.K_LEFT:
  70.                     x1_change = -snake_block
  71.                     y1_change = 0
  72.                 elif event.key == pygame.K_RIGHT:
  73.                     x1_change = snake_block
  74.                     y1_change = 0
  75.                 elif event.key == pygame.K_UP:
  76.                     y1_change = -snake_block
  77.                     x1_change = 0
  78.                 elif event.key == pygame.K_DOWN:
  79.                     y1_change = snake_block
  80.                     x1_change = 0

  81.         # 检查是否撞墙
  82.         if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
  83.             game_close = True

  84.         # 更新蛇的位置
  85.         x1 += x1_change
  86.         y1 += y1_change
  87.         dis.fill(black)
  88.         pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
  89.         
  90.         snake_Head = []
  91.         snake_Head.append(x1)
  92.         snake_Head.append(y1)
  93.         snake_List.append(snake_Head)
  94.         
  95.         if len(snake_List) > Length_of_snake:
  96.             del snake_List[0]

  97.         # 检查是否撞到自己
  98.         for x in snake_List[:-1]:
  99.             if x == snake_Head:
  100.                 game_close = True

  101.         our_snake(snake_block, snake_List)
  102.         your_score(Length_of_snake - 1)

  103.         pygame.display.update()

  104.         # 检查是否吃到食物
  105.         if x1 == foodx and y1 == foody:
  106.             foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  107.             foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  108.             Length_of_snake += 1

  109.         clock.tick(snake_speed)

  110.     pygame.quit()
  111.     quit()

  112. gameLoop()
复制代码


第三步:运行游戏
在终端中运行:

  1. python snake_game.py
复制代码


游戏操作说明:
- 使用方向键控制蛇的移动
- 吃到红色食物可以增加长度和得分
- 撞到墙壁或自己的身体游戏结束
- 游戏结束后按Q退出或C重新开始

代码说明:
1. 使用pygame创建游戏窗口和图形
2. 通过列表来存储蛇的身体坐标
3. 使用随机数生成食物位置
4. 检测键盘事件来控制蛇的移动方向
5. 实现碰撞检测(墙壁和自身)
6. 计分系统显示当前得分

你可以根据自己的喜好修改颜色、速度和游戏难度。希望这个简单的贪吃蛇游戏对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 23:16 | 显示全部楼层
FishC 发表于 2025-9-18 23:15
当然!用Python写贪吃蛇是一个很好的练习项目,它能帮你巩固编程基础。下面是一个使用 pygame 库实现的简单 ...

你的模型是?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-19 05:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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