AI写代码系列(1):用GPT写个小游戏,难吗?Too......Simple

编程入门 行业动态 更新时间:2024-10-10 08:20:55

AI写代码系列(1):用GPT写个<a href=https://www.elefans.com/category/jswz/34/1769974.html style=小游戏,难吗?Too......Simple"/>

AI写代码系列(1):用GPT写个小游戏,难吗?Too......Simple

闲来无事,好奇心起。

前几天无意看到一个网站,好像可以免费用GPT,试了一下,还真的可以用。太好了,终于可以不必科学上网,就能体验一把传说中niubility的GPT了。O(∩_∩)O哈哈~

我先试着敲了几个字,回复的还挺流畅,不过作为一名资深老码农,总感觉不太过瘾。总要折腾点啥才能显示出咱的水平,哼哼哈嘿!!

那干点啥呢?对了,听说GPT很厉害,可以写魂斗罗程序,但是我还没验证过,今天刚好有空,我也试着写一个吧。魂斗罗有点俗了,写个贪吃蛇吧!下面记录下我的验证过程。

第一步,输入:写个贪吃蛇的python代码

然后坐等GPT写代码滴干活:

代码有点长.......只截了一段。

第二步,把代码拷贝到txt文档,改名成.py 文件,安装pyscripter,然后执行。不过,报了一个错:

第三步,把错误拷贝给GPT:

第四步,拷贝 pip install pygame,安装成功。

第五步,执行py脚本,done。贪吃小蛇跑起来啦啦啦:

我平时写代码一般不会写小游戏,那写个函数行不行呢?让写个冒泡排序函数吧:

也挺快,不止代码连示例也写好了,8错,看来GPT对码农来说,还是有点用滴。先写这么多了,感兴趣的小伙伴可以自己去试吧。

只要注册一下,就可以用啦:拓云智能拓云智能Web site created using create-react-app/

附python代码,感兴趣的同学可以自己运行一下试试:

#
# written by 拓云智能,一个便宜又好用的AI平台。登录网址:
#

import pygame

import time

import random


pygame.init()

white = (255, 255, 255)

yellow = (255, 255, 102)

black = (0, 0, 0)

red = (213, 50, 80)

green = (0, 255, 0)

blue = (50, 153, 213)

dis_width = 800

dis_height = 600
 

dis = pygame.display.set_mode((dis_width, dis_height))

pygame.display.set_caption('贪吃蛇游戏')

clock = pygame.time.Clock()

snake_block = 10

snake_speed = 10
 

font_style = pygame.font.SysFont(None, 50)

score_font = pygame.font.SysFont(None, 35)
 

def our_snake(snake_block, snake_list):

    for x in snake_list:

        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
 

def message(msg, color):

    mesg = font_style.render(msg, True, color)

    dis.blit(mesg, [dis_width / 6, dis_height / 3])
 

def gameLoop():

    game_over = False

    game_close = False
 

    x1 = dis_width / 2

    y1 = dis_height / 2
 

    x1_change = 0

    y1_change = 0
 

    snake_List = []

    Length_of_snake = 1
 

    foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0

    foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
 

    while not game_over:

        while game_close:

            dis.fill(blue)

            message("你输了!按Q-退出或C-重新开始", red)

            pygame.display.update()

            for event in pygame.event.get():

                if event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_q:

                        game_over = True

                        game_close = False

                    if event.key == pygame.K_c:

                        gameLoop()

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                game_over = True

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_LEFT:

                    x1_change = -snake_block

                    y1_change = 0

                elif event.key == pygame.K_RIGHT:

                    x1_change = snake_block

                    y1_change = 0

                elif event.key == pygame.K_UP:

                    y1_change = -snake_block

                    x1_change = 0

                elif event.key == pygame.K_DOWN:

                    y1_change = snake_block

                    x1_change = 0
 

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:

            game_close = True

        x1 += x1_change

        y1 += y1_change

        dis.fill(blue)

        pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])

        snake_Head = []

        snake_Head.append(x1)

        snake_Head.append(y1)

        snake_List.append(snake_Head)

        if len(snake_List) > Length_of_snake:

            del snake_List[0]

        for x in snake_List[:-1]:

            if x == snake_Head:

                game_close = True
 

        our_snake(snake_block, snake_List)

        pygame.display.update()

        if x1 == foodx and y1 == foody:

            foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0

            foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0

            Length_of_snake += 1
 

        clock.tick(snake_speed)

    pygame.quit()

    quit()
 

gameLoop()
 

更多推荐

AI写代码系列(1):用GPT写个小游戏,难吗?Too......Simple

本文发布于:2024-03-05 11:48:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1712217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小游戏   代码   系列   AI   Simple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!