同時向訊飛星火、文心一言和通義千問三個國產AI模型提個相同的問題:
“python 寫一個貪吃蛇的游戲代碼”
看哪一家AI寫的程序直接能用,誰就勝出!
訊飛星火?
訊飛星火給出的代碼:
import pygame
import sys
import random# 初始化pygame
pygame.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 pygame
import sys
import random# 定義顏色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GREY = (211, 211, 211) # 淡灰色def init():global screen, screen_sizeglobal snake_pos, food_pos, snake_speed# 初始化pygamepygame.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) # 線寬為1points = 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_sizeglobal 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 againelse:game_quit() # No to Exit'''# 重畫界面及蛇和食物repaint()# 控制游戲速度pygame.time.Clock().tick(10)if __name__ == "__main__":init()main()
更多改進思路:
增加游戲的生命值,比如初始有3條蛇,死完后提示用戶“重來”還是“退出游戲”;
增加游戲的得分,比如每吃到一個食物,得分增加10分;結束時寫入排行榜;
增加游戲背景音樂和聲效,提高游戲可玩性……
擅長pygame編程的你,請繼續