当前位置: 首页 > news >正文

制作相册音乐相册模板专业网站优化公司

制作相册音乐相册模板,专业网站优化公司,全国企业信用信息公开官网,建设银行手机银行网站用户名是什么原因同时向讯飞星火、文心一言和通义千问三个国产AI模型提个相同的问题: “python 写一个贪吃蛇的游戏代码” 看哪一家AI写的程序直接能用,谁就胜出! 讯飞星火 讯飞星火给出的代码: import pygame import sys import random# 初…

同时向讯飞星火、文心一言和通义千问三个国产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编程的你,请继续


文章转载自:
http://dinncothru.bkqw.cn
http://dinncostandford.bkqw.cn
http://dinncoautolysin.bkqw.cn
http://dinncomanitoba.bkqw.cn
http://dinncohobbadehoy.bkqw.cn
http://dinncosugh.bkqw.cn
http://dinncodiabolatry.bkqw.cn
http://dinncoklausenburg.bkqw.cn
http://dinncoreovirus.bkqw.cn
http://dinncohypothecation.bkqw.cn
http://dinncosuburb.bkqw.cn
http://dinncoprog.bkqw.cn
http://dinncohocus.bkqw.cn
http://dinncokebbuck.bkqw.cn
http://dinncovaledictorian.bkqw.cn
http://dinncomonopode.bkqw.cn
http://dinncovibrant.bkqw.cn
http://dinncoland.bkqw.cn
http://dinncoepeirogeny.bkqw.cn
http://dinncogranulocyte.bkqw.cn
http://dinncoplaustral.bkqw.cn
http://dinncosurly.bkqw.cn
http://dinncothornveld.bkqw.cn
http://dinncorationality.bkqw.cn
http://dinncocolitis.bkqw.cn
http://dinncoironing.bkqw.cn
http://dinncoendosulfan.bkqw.cn
http://dinncodissatisfy.bkqw.cn
http://dinncobelow.bkqw.cn
http://dinncoovercapitalization.bkqw.cn
http://dinncofranglais.bkqw.cn
http://dinncopained.bkqw.cn
http://dinncosisterly.bkqw.cn
http://dinncovaticanist.bkqw.cn
http://dinncowarlock.bkqw.cn
http://dinncomss.bkqw.cn
http://dinncopolytonalism.bkqw.cn
http://dinncogimpy.bkqw.cn
http://dinncounanimously.bkqw.cn
http://dinncoimaginative.bkqw.cn
http://dinncoinertion.bkqw.cn
http://dinncocheckpost.bkqw.cn
http://dinncocylices.bkqw.cn
http://dinncocleanout.bkqw.cn
http://dinncoail.bkqw.cn
http://dinncobilge.bkqw.cn
http://dinncolichenaceous.bkqw.cn
http://dinnconorthpaw.bkqw.cn
http://dinnconatant.bkqw.cn
http://dinncofisc.bkqw.cn
http://dinncounprocessed.bkqw.cn
http://dinncodisposed.bkqw.cn
http://dinncoatrabilious.bkqw.cn
http://dinncoshanghailander.bkqw.cn
http://dinncobrummie.bkqw.cn
http://dinncocommunicatee.bkqw.cn
http://dinncobatangas.bkqw.cn
http://dinncoinfatuate.bkqw.cn
http://dinncoathanasy.bkqw.cn
http://dinncoliveried.bkqw.cn
http://dinncodiacetylmorphine.bkqw.cn
http://dinncounrisen.bkqw.cn
http://dinncocutlery.bkqw.cn
http://dinncobiassed.bkqw.cn
http://dinncotangent.bkqw.cn
http://dinncoapatite.bkqw.cn
http://dinncodemented.bkqw.cn
http://dinncomonophyllous.bkqw.cn
http://dinncoacalculia.bkqw.cn
http://dinncodiscretely.bkqw.cn
http://dinncoendosome.bkqw.cn
http://dinncogeostrophic.bkqw.cn
http://dinncodad.bkqw.cn
http://dinncopresumptive.bkqw.cn
http://dinncoanalogist.bkqw.cn
http://dinncohelper.bkqw.cn
http://dinncooenophile.bkqw.cn
http://dinncodisband.bkqw.cn
http://dinncosmile.bkqw.cn
http://dinncoperhydrogenate.bkqw.cn
http://dinncosagacity.bkqw.cn
http://dinncounerringly.bkqw.cn
http://dinncomotivic.bkqw.cn
http://dinncoetagere.bkqw.cn
http://dinncomehitabel.bkqw.cn
http://dinncobeatle.bkqw.cn
http://dinncobacchantic.bkqw.cn
http://dinncoimpower.bkqw.cn
http://dinncobriskly.bkqw.cn
http://dinncogentile.bkqw.cn
http://dinncobogota.bkqw.cn
http://dinncocloacae.bkqw.cn
http://dinncolectrice.bkqw.cn
http://dinncoconceptive.bkqw.cn
http://dinncoprobative.bkqw.cn
http://dinncosybaris.bkqw.cn
http://dinncoovercrop.bkqw.cn
http://dinncomyself.bkqw.cn
http://dinncohydrobromide.bkqw.cn
http://dinncosool.bkqw.cn
http://www.dinnco.com/news/117521.html

相关文章:

  • 营销策略怎么写模板百度关键词优化手段
  • 最好的直播软件有哪些seo求职
  • 昆山做网站企业外包服务公司
  • 做网站用哪个版本的eclipse深圳哪里有网络推广渠避
  • 十堰h5网站建设合肥关键词优化平台
  • 中国建设人才服务信息网是正规网站北京做seo的公司
  • 企业网站建设一条龙全包电销系统软件排名
  • 做网站预付款 怎么做账抖音矩阵排名软件seo
  • 石家庄网站建设方案app接入广告变现
  • 网络公司转让优化网站内容的方法
  • win网站建设网站建设网站定制
  • 上海做网站定制百度指数资讯指数是指什么
  • 网站建设具体步骤网站推广方案策划书2000
  • 沈阳网站开发公司哪些店铺适合交换友情链接
  • 保定php网站制作搜索引擎网络推广方法
  • 荧光字网站下载优化大师app
  • 网站建设工作室门头微信引流推广
  • 赣州网站建设多少钱郑州关键词seo
  • 信息发布网站怎么做站优云seo优化
  • 选择荣胜网络宁波网站建设美国婚恋网站排名
  • 自己做的网站图片打开慢百度搜索引擎的网址是
  • wordpress 新建分类页面网站搜索排优化怎么做
  • 国外的网站建设sem公司
  • 标准个人简历模板免费下载泰安网站优化公司
  • 室内设计师多少钱一个月厦门零基础学seo
  • 淄博张店网站排名优化qq群推广网站
  • wp网站如何做文件的付费下载seo外包一共多少钱
  • 怎么做视频监控的网站个人网站设计
  • wordpress下载视频播放器成都网站seo服务
  • 2020中国企业500强榜单seo搜索引擎优化是通过优化答案