欢迎光临散文网 会员登陆 & 注册

零编程知识怎么在AI帮助做一个贪吃蛇的游戏

2023-05-22 17:23 作者:希声碎碎念  | 我要投稿

突发奇想,我没有任何编程知识,能不能让 ChatGPT 手把手教我做一个简单的游戏,我只要负责复制粘贴就行?

开始尝试:

写一个贪吃蛇的游戏,并告诉我在电脑上怎么玩这个游戏,写出详细的步骤。我对于编程的知识是0.



他写出了整个流程,但是没有把完整代码写出来,于是我就继续要求。


这回他写出了代码,于是我让他写出更具体的执行操作步骤。



好极了,很详细的流程,感觉马上就要成功了。

但是,不出意外的话马上就要出意外了。我从 python 官网下了一个压缩包,解压之后出现了一大堆我看不懂的东西:


没办法,继续提问:



看懂了,我下载的这个不需要安装。

然后又出现了一些奇怪的问题……




看来 AI 也会写出错误的代码,没办法,就把错误提供给他,让他给我修改就好了。

但是他告诉我在哪里修改,我并不会修改,因为我双击打开代码文件没有反应,也许我又漏装了什么……

但是有个最简单的解决方案就是:重新用记事本新建一个就好了。

所以我选择让他把修改完的完整代码给我,而不是告诉我修改的步骤:


游戏可以运行之后,我就发现速度太快了,于是我要求增加速度调整功能。


又或者增加更多的功能。


又经历了多次报错和反复拉锯(是的,AI 竟然嫌弃代码太长不愿意写,那就重开一个窗口把要求复制过来提问,然后 AI 就乖乖地给了全部代码)。

最后,游戏成功运行了,打开游戏长这样:


先选择速度,然后可以点击开始游戏:


吃了果实会变长。


如果撞墙了就会提示:


OK,上面就是我人生中编写的第一个游戏的全过程,代码就开源(是这么说对吧……)在下面了!

哈哈,好中二的感觉~


import tkinter as tk

import random


class SnakeGame:

    def __init__(self):

        self.window = tk.Tk()

        self.window.title("贪吃蛇游戏")

        

        self.speed = tk.IntVar()

        self.speed.set(15)  # 默认速度为15

        

        self.create_speed_slider()

        self.create_start_button()

        

        self.canvas = None

        self.snake = [(100, 50), (90, 50), (80, 50)]  # 贪吃蛇初始位置

        self.direction = "Right"

        self.food = self.create_food()

        self.game_over = False

        

        self.window.mainloop()

    

    def create_speed_slider(self):

        speed_label = tk.Label(self.window, text="请选择速度")

        speed_label.pack()

        

        speed_slider = tk.Scale(self.window, from_=1, to=30, orient=tk.HORIZONTAL, variable=self.speed)

        speed_slider.pack()

    

    def create_start_button(self):

        start_button = tk.Button(self.window, text="开始游戏", command=self.start_game)

        start_button.pack()

    

    def start_game(self):

        self.window.destroy()

        

        self.window = tk.Tk()

        self.window.title("贪吃蛇游戏")

        self.window.bind("<Key>", self.change_direction)

        

        self.canvas = tk.Canvas(self.window, width=500, height=500)

        self.canvas.pack()

        

        self.draw_snake()

        self.draw_food()

        

        self.move_snake()

        

        self.window.mainloop()

    

    def create_food(self):

        x = random.randint(1, 49) * 10

        y = random.randint(1, 49) * 10

        return x, y

    

    def draw_snake(self):

        self.canvas.delete(tk.ALL)

        

        for x, y in self.snake:

            self.canvas.create_rectangle(x, y, x+10, y+10, fill="green")

    

    def draw_food(self):

        x, y = self.food

        self.canvas.create_oval(x, y, x+10, y+10, fill="red")

    

    def move_snake(self):

        if not self.game_over:

            head_x, head_y = self.snake[0]

            

            if self.direction == "Up":

                head_y -= 10

            elif self.direction == "Down":

                head_y += 10

            elif self.direction == "Left":

                head_x -= 10

            elif self.direction == "Right":

                head_x += 10

            

            self.snake.insert(0, (head_x, head_y))

            

            if self.check_collision():

                self.game_over = True

            

            if head_x == self.food[0] and head_y == self.food[1]:

                self.food = self.create_food()

            else:

                self.snake.pop()

            

            self.draw_snake()

            self.draw_food()

            

            self.window.after(1000 // self.speed.get(), self.move_snake)

        else:

            self.show_game_over()

    

    def change_direction(self, event):

        if event.keysym == "Up" and self.direction != "Down":

            self.direction = "Up"

        elif event.keysym == "Down" and self.direction != "Up":

            self.direction = "Down"

        elif event.keysym == "Left" and self.direction != "Right":

            self.direction = "Left"

        elif event.keysym == "Right" and self.direction != "Left":

            self.direction = "Right"

    

    def check_collision(self):

        head_x, head_y = self.snake[0]

        

        if (

            head_x < 0 or head_x >= 500 or

            head_y < 0 or head_y >= 500 or

            (head_x, head_y) in self.snake[1:]

        ):

            return True

        

        return False

    

    def show_game_over(self):

        self.canvas.delete(tk.ALL)

        

        game_over_label = tk.Label(self.window, text="你输了")

        game_over_label.pack()

        

        restart_button = tk.Button(self.window, text="复活", command=self.restart_game)

        restart_button.pack()

    

    def restart_game(self):

        self.game_over = False

        self.snake = [(100, 50), (90, 50), (80, 50)]

        self.direction = "Right"

        self.food = self.create_food()

        self.start_game()


SnakeGame()



最后再感叹一下,gpt的发展太快了,随着插件的不断丰富,已经可以实现非常多的功能,大家一定要去尝试~



零编程知识怎么在AI帮助做一个贪吃蛇的游戏的评论 (共 条)

分享到微博请遵守国家法律