当前位置:   article > 正文

讯飞星火、文心一言和通义千问同时编“贪吃蛇”游戏,谁会胜出?_# 蛇的初始速度 snake_speed = [20, 0]

# 蛇的初始速度 snake_speed = [20, 0]

同时向讯飞星火、文心一言和通义千问三个国产AI模型提个相同的问题:

“python 写一个贪吃蛇的游戏代码”

看哪一家AI写的程序直接能用,谁就胜出!

讯飞星火 

讯飞星火给出的代码:

  1. import pygame
  2. import sys
  3. import random
  4. # 初始化pygame
  5. pygame.init()
  6. # 设置屏幕大小
  7. screen_size = (640, 480)
  8. screen = pygame.display.set_mode(screen_size)
  9. # 设置游戏标题
  10. pygame.display.set_caption("贪吃蛇")
  11. # 定义颜色
  12. WHITE = (255, 255, 255)
  13. GREEN = (0, 255, 0)
  14. RED = (255, 0, 0)
  15. # 蛇的初始位置
  16. snake_pos = [[100, 100], [80, 100], [60, 100]]
  17. # 食物的初始位置
  18. food_pos = [300, 300]
  19. # 蛇的初始速度
  20. snake_speed = [20, 0]
  21. # 主循环
  22. while True:
  23. # 处理游戏事件
  24. for event in pygame.event.get():
  25. if event.type == pygame.QUIT:
  26. pygame.quit()
  27. sys.exit()
  28. elif event.type == pygame.KEYDOWN:
  29. if event.key == pygame.K_UP:
  30. snake_speed = [0, -20]
  31. elif event.key == pygame.K_DOWN:
  32. snake_speed = [0, 20]
  33. elif event.key == pygame.K_LEFT:
  34. snake_speed = [-20, 0]
  35. elif event.key == pygame.K_RIGHT:
  36. snake_speed = [20, 0]
  37. # 更新蛇的位置
  38. snake_pos.insert(0, [snake_pos[0][0] + snake_speed[0], snake_pos[0][1] + snake_speed[1]])
  39. # 检查蛇头是否碰到食物
  40. if snake_pos[0] == food_pos:
  41. food_pos = [random.randrange(1, screen_size[0] // 20) * 20, random.randrange(1, screen_size[1] // 20) * 20]
  42. else:
  43. snake_pos.pop()
  44. # 检查蛇头是否碰到墙壁或者蛇身
  45. if snake_pos[0][0] < 0 or snake_pos[0][0] >= screen_size[0] or snake_pos[0][1] < 0 or snake_pos[0][1] >= screen_size[1] or snake_pos[0] in snake_pos[1:]:
  46. pygame.quit()
  47. sys.exit()
  48. # 绘制游戏界面
  49. screen.fill(WHITE)
  50. for pos in snake_pos:
  51. pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 20, 20))
  52. pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], 20, 20))
  53. pygame.display.flip()
  54. # 控制游戏速度
  55. pygame.time.Clock().tick(10)

按F5执行,运行结果如下:

文心一言

文心一言给出的代码:

```
import pygame  
import random  
  
# 初始化pygame  
pygame.init()  
  
# 定义颜色  
WHITE = (255, 255, 255)  
  
# 定义屏幕大小  
SCREEN_WIDTH = 400  
SCREEN_HEIGHT = 400  
  
# 创建屏幕  
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))  
  
# 设置标题  
pygame.display.set_caption("Snake Game")  
  
# 创建时钟对象,用于控制游戏速度  
clock = pygame.time.Clock()  
  
# 创建蛇和食物的初始位置和大小  
snake_parts = [[100, 50], [90, 50], [80, 50]]  
snake_head = [70, 50]  
food_x = random.randint(1, 19) * 20  
food_y = random.randint(1, 19) * 20  
snake_size = 20  
food_size = 20  
  
# 定义蛇和食物的运动方向和速度  
snake_dir = [2, 0]  
snake_speed = 10  
food_speed = 5  
  
# 定义游戏状态:进行中或结束  
game_active = True  
  
# 游戏循环  
while game_active:  
    # 处理事件  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            game_active = False  
        elif event.type == pygame.KEYDOWN:  
            if event.key == pygame.K_UP and snake_dir != [2, 0]: snake_dir = [0, -1]  
            elif event.key == pygame.K_DOWN and snake_dir != [2, 0]: snake_dir = [0, 1]  
            elif event.key == pygame.K_LEFT and snake_dir != [1, 0]: snake_dir = [-1, 0]  
            elif event.key == pygame.K_RIGHT and snake_dir != [3, 0]: snake_dir = [1, 0]  
  
    # 移动蛇头,如果碰到食物,则加长蛇身,并重新生成食物位置  
    snake_head[0] += snake_dir[0] * snake_speed  
    snake_head[1] += snake_dir[1] * snake_speed  
    if snake_head[0] == food_x and snake_head[1] == food_y:  
        food_x = random.randint(1, 19) * 20  
        food_y = random.randint(1, 19) * 20  
        snake_parts.insert(0, list(snake_head))  
    else:  
        snake_parts.pop()  
    # 如果蛇头超出屏幕边界,则游戏结束  
    if snake_head[0] < 0 or snake_head[0] >= SCREEN_WIDTH or snake_head[1] < 0 or snake_head[1] >= SCREEN_HEIGHT:  
        game_active = False  
    # 如果蛇头撞到自己的身体,则游戏结束  
    for part in snake_parts:  
        if part == list(snake_head): continue  
        if part[0] == snake_head[0] and part[1] == snake_head[1]:  
            game_active = False  
            break  
    # 将蛇身和食物绘制到屏幕上,刷新屏幕显示结果  
    screen.fill(WHITE)  
    for part in snake_parts:  
        pygame.draw.rect(screen, WHITE, pygame.Rect(part[0], part[1], snake_size, snake_size))  
    pygame.draw.rect(screen, WHITE, pygame.Rect(food_x, food_y, food_size, food_size))  
    pygame.display.flip()  
    # 控制游戏速度,避免程序运行过快导致游戏体验不佳,同时保证游戏运行流畅度,避免卡顿现象发生。  
    clock.tick(60)  # 每秒60帧,即每秒游戏画面更新60次。
```

按F5执行,报以下错误:

pygame 2.5.0 (SDL 2.28.0, Python 3.11.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "E:\.py\snake.py", line 59, in <module>
    snake_parts.pop()
IndexError: pop from empty list

通义千问

通义千问给出的代码:

```python
import pygame
import sys
import random

# 初始化 Pygame
pygame.init()

# 设置屏幕大小和标题
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption('Snaky')

# 设置游戏速度
clock = pygame.time.Clock()

# 蛇体、食物的大小
cell_size = 10

# 蛇的初始位置、方向和长度
snake_x = 200
snake_y = 200
snake_direction = 'right'
snake_length = 3

# 食物的初始位置
food_x = random.randint(0, 380 - cell_size)
food_y = random.randint(0, 380 - cell_size)

# 蛇移动的方向向量
right = (1, 0)
left = (-1, 0)
up = (0, -1)
down = (0, 1)

# 游戏是否结束
game_over = False

# 游戏主循环
while not game_over:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and snake_direction != 'down':
                snake_direction = 'up'
            elif event.key == pygame.K_DOWN and snake_direction != 'up':
                snake_direction = 'down'
            elif event.key == pygame.K_LEFT and snake_direction != 'right':
                snake_direction = 'left'
            elif event.key == pygame.K_RIGHT and snake_direction != 'left':
                snake_direction = 'right'

    # 更新蛇的位置
    if snake_direction == 'right':
        snake_x += cell_size
    elif snake_direction == 'left':
        snake_x -= cell_size
    elif snake_direction == 'up':
        snake_y -= cell_size
    elif snake_direction == 'down':
        snake_y += cell_size
    
    # 检查蛇是否碰到墙壁或自己的身体
    if snake_x < 0 or snake_x >= 400 - cell_size or snake_y < 0 or snake_y >= 400 - cell_size:
        game_over = True
    for i in range(snake_length - 1, -1, -1):
        if snake_x == snake_body[i][0] and snake_y == snake_body[i][1]:
            game_over = True

    # 检查蛇是否吃到食物
    if snake_x == food_x and snake_y == food_y:
        food_x = random.randint(0, 380 - cell_size)
        food_y = random.randint(0, 380 - cell_size)
        snake_length += 1
        snake_body.append((snake_x, snake_y))

    # 绘制蛇和食物
    screen.fill((0, 0, 0))
    for i in range(snake_length):
        pygame.draw.rect(screen, (255, 255, 255), (snake_body[i][0], snake_body[i][1], cell_size, cell_size))
    pygame.draw.rect(screen, (0, 255, 0), (food_x, food_y, cell_size, cell_size))

    # 更新屏幕
    pygame.display.flip()
    clock.tick(10)

# 退出 Pygame
pygame.quit()
sys.exit()
```
按F5执行,报以下错误:

pygame 2.5.0 (SDL 2.28.0, Python 3.11.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "E:\.py\snake.py", line 67, in <module>
    if snake_x == snake_body[i][0] and snake_y == snake_body[i][1]:
NameError: name 'snake_body' is not defined. Did you mean: 'snake_y'?

讯飞星火胜出!

只有讯飞星火未做任何修改,代码直接拿来就能运行,胜出!

但只说明仅此一个问题的胜出,并非就说讯飞整体就比其它两家强哦!


代码改进

简单做了2个小的优化改进:

1. 代码细分为多个函数 init() repaint() quit_game() ......

2. 游戏界面增加了方格线,使用pygame.draw.lines(),快速布点是关键:

    # 定义线段端点坐标时,根据规律分别写x,y坐标的数据列表
    x,y = (-1,640,640,-1)*16, []
    for i in range(36):
        for _ in range(2):
            y.append(19+i*20)
    # zip()整合x,y为坐标Pair
    points = list(zip(x,y))

完成代码

  1. import pygame
  2. import sys
  3. import random
  4. # 定义颜色
  5. WHITE = (255, 255, 255)
  6. GREEN = (0, 255, 0)
  7. RED = (255, 0, 0)
  8. GREY = (211, 211, 211) # 淡灰色
  9. def init():
  10. global screen, screen_size
  11. global snake_pos, food_pos, snake_speed
  12. # 初始化pygame
  13. pygame.init()
  14. # 设置屏幕大小
  15. screen_size = (640, 480)
  16. screen = pygame.display.set_mode(screen_size)
  17. # 设置游戏标题
  18. pygame.display.set_caption("贪吃蛇")
  19. # 蛇的初始位置
  20. snake_pos = [[100, 100], [80, 100], [60, 100]]
  21. # 食物的初始位置
  22. food_pos = [300, 300]
  23. # 蛇的初始速度
  24. snake_speed = [20, 0]
  25. def repaint():
  26. # 绘制游戏界面
  27. screen.fill(WHITE)
  28. # 定义线段的端点坐标
  29. x,y = (-1,640,640,-1)*16, []
  30. for i in range(36):
  31. for _ in range(2):
  32. y.append(19+i*20)
  33. # 使用pygame.draw.lines()函数绘制线段
  34. points = list(zip(x,y))
  35. pygame.draw.lines(screen, GREY, False, points, 1) # 线宽为1
  36. points = list(zip(y,x))
  37. pygame.draw.lines(screen, GREY, False, points, 1)
  38. # 重画蛇和食物
  39. for pos in snake_pos:
  40. pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 20, 20))
  41. pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], 20, 20))
  42. pygame.display.flip()
  43. def game_quit():
  44. pygame.quit()
  45. sys.exit()
  46. def main():
  47. global screen, screen_size
  48. global snake_pos, food_pos, snake_speed
  49. # 主循环
  50. while True:
  51. # 处理游戏事件
  52. for event in pygame.event.get():
  53. if event.type == pygame.QUIT:
  54. game_quit()
  55. elif event.type == pygame.KEYDOWN:
  56. if event.key == pygame.K_UP:
  57. snake_speed = [0, -20]
  58. elif event.key == pygame.K_DOWN:
  59. snake_speed = [0, 20]
  60. elif event.key == pygame.K_LEFT:
  61. snake_speed = [-20, 0]
  62. elif event.key == pygame.K_RIGHT:
  63. snake_speed = [20, 0]
  64. # 更新蛇的位置
  65. snake_pos.insert(0, [snake_pos[0][0] + snake_speed[0], snake_pos[0][1] + snake_speed[1]])
  66. # 检查蛇头是否碰到食物
  67. if snake_pos[0] == food_pos:
  68. food_pos = [random.randrange(1, screen_size[0] // 20) * 20, random.randrange(1, screen_size[1] // 20) * 20]
  69. else:
  70. snake_pos.pop()
  71. # 检查蛇头是否碰到墙壁或者蛇身
  72. if snake_pos[0][0] < 0 or snake_pos[0][0] >= screen_size[0] or snake_pos[0][1] < 0 or snake_pos[0][1] >= screen_size[1] or snake_pos[0] in snake_pos[1:]:
  73. game_quit()
  74. '''
  75. 此处可增加与用户的交互,如:
  76. if askyesno('title','again?'):
  77. init() # Yes to Play again
  78. else:
  79. game_quit() # No to Exit
  80. '''
  81. # 重画界面及蛇和食物
  82. repaint()
  83. # 控制游戏速度
  84. pygame.time.Clock().tick(10)
  85. if __name__ == "__main__":
  86. init()
  87. main()

更多改进思路:

增加游戏的生命值,比如初始有3条蛇,死完后提示用户“重来”还是“退出游戏”;

增加游戏的得分,比如每吃到一个食物,得分增加10分;结束时写入排行榜;

增加游戏背景音乐和声效,提高游戏可玩性……


擅长pygame编程的你,请继续

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/169510
推荐阅读
相关标签
  

闽ICP备14008679号