同时向讯飞星火、文心一言和通义千问三个国产AI模型提个相同的问题:
“python 写一个贪吃蛇的游戏代码”
看哪一家AI写的程序直接能用,谁就胜出!
讯飞星火
讯飞星火给出的代码:
import pygameimport sysimport random# 初始化pygamepygame.init()# 设置屏幕大小screen_size = (640, 480)screen = pygame.display.set_mode(screen_size)# 设置游戏标题pygame.display.set_caption("贪吃蛇")# 定义颜色WHITE = (255, 255, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)# 蛇的初始位置snake_pos = [[100, 100], [80, 100], [60, 100]]# 食物的初始位置food_pos = [300, 300]# 蛇的初始速度snake_speed = [20, 0]# 主循环while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake_speed = [0, -20] elif event.key == pygame.K_DOWN: snake_speed = [0, 20] elif event.key == pygame.K_LEFT: snake_speed = [-20, 0] elif event.key == pygame.K_RIGHT: snake_speed = [20, 0] # 更新蛇的位置 snake_pos.insert(0, [snake_pos[0][0] + snake_speed[0], snake_pos[0][1] + snake_speed[1]]) # 检查蛇头是否碰到食物 if snake_pos[0] == food_pos: food_pos = [random.randrange(1, screen_size[0] // 20) * 20, random.randrange(1, screen_size[1] // 20) * 20] else: snake_pos.pop() # 检查蛇头是否碰到墙壁或者蛇身 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:]: pygame.quit() sys.exit() # 绘制游戏界面 screen.fill(WHITE) for pos in snake_pos: pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 20, 20)) pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], 20, 20)) pygame.display.flip() # 控制游戏速度 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))
完成代码
import pygameimport sysimport random# 定义颜色WHITE = (255, 255, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)GREY = (211, 211, 211) # 淡灰色def init(): global screen, screen_size global snake_pos, food_pos, snake_speed # 初始化pygame pygame.init() # 设置屏幕大小 screen_size = (640, 480) screen = pygame.display.set_mode(screen_size) # 设置游戏标题 pygame.display.set_caption("贪吃蛇") # 蛇的初始位置 snake_pos = [[100, 100], [80, 100], [60, 100]] # 食物的初始位置 food_pos = [300, 300] # 蛇的初始速度 snake_speed = [20, 0]def repaint(): # 绘制游戏界面 screen.fill(WHITE) # 定义线段的端点坐标 x,y = (-1,640,640,-1)*16, [] for i in range(36): for _ in range(2): y.append(19+i*20) # 使用pygame.draw.lines()函数绘制线段 points = list(zip(x,y)) pygame.draw.lines(screen, GREY, False, points, 1) # 线宽为1 points = list(zip(y,x)) pygame.draw.lines(screen, GREY, False, points, 1) # 重画蛇和食物 for pos in snake_pos: pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 20, 20)) pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], 20, 20)) pygame.display.flip()def game_quit(): pygame.quit() sys.exit()def main(): global screen, screen_size global snake_pos, food_pos, snake_speed # 主循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: game_quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: snake_speed = [0, -20] elif event.key == pygame.K_DOWN: snake_speed = [0, 20] elif event.key == pygame.K_LEFT: snake_speed = [-20, 0] elif event.key == pygame.K_RIGHT: snake_speed = [20, 0] # 更新蛇的位置 snake_pos.insert(0, [snake_pos[0][0] + snake_speed[0], snake_pos[0][1] + snake_speed[1]]) # 检查蛇头是否碰到食物 if snake_pos[0] == food_pos: food_pos = [random.randrange(1, screen_size[0] // 20) * 20, random.randrange(1, screen_size[1] // 20) * 20] else: snake_pos.pop() # 检查蛇头是否碰到墙壁或者蛇身 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:]: game_quit() ''' 此处可增加与用户的交互,如: if askyesno('title','again?'): init() # Yes to Play again else: game_quit() # No to Exit ''' # 重画界面及蛇和食物 repaint() # 控制游戏速度 pygame.time.Clock().tick(10)if __name__ == "__main__": init() main()
更多改进思路:
增加游戏的生命值,比如初始有3条蛇,死完后提示用户“重来”还是“退出游戏”;
增加游戏的得分,比如每吃到一个食物,得分增加10分;结束时写入排行榜;
增加游戏背景音乐和声效,提高游戏可玩性……
擅长pygame编程的你,请继续