零編程知識(shí)怎么在AI幫助做一個(gè)貪吃蛇的游戲

突發(fā)奇想,我沒有任何編程知識(shí),能不能讓 ChatGPT 手把手教我做一個(gè)簡單的游戲,我只要負(fù)責(zé)復(fù)制粘貼就行?

開始嘗試:
寫一個(gè)貪吃蛇的游戲,并告訴我在電腦上怎么玩這個(gè)游戲,寫出詳細(xì)的步驟。我對于編程的知識(shí)是0.


他寫出了整個(gè)流程,但是沒有把完整代碼寫出來,于是我就繼續(xù)要求。

這回他寫出了代碼,于是我讓他寫出更具體的執(zhí)行操作步驟。


好極了,很詳細(xì)的流程,感覺馬上就要成功了。
但是,不出意外的話馬上就要出意外了。我從 python 官網(wǎng)下了一個(gè)壓縮包,解壓之后出現(xiàn)了一大堆我看不懂的東西:

沒辦法,繼續(xù)提問:


看懂了,我下載的這個(gè)不需要安裝。
然后又出現(xiàn)了一些奇怪的問題……



看來 AI 也會(huì)寫出錯(cuò)誤的代碼,沒辦法,就把錯(cuò)誤提供給他,讓他給我修改就好了。
但是他告訴我在哪里修改,我并不會(huì)修改,因?yàn)槲译p擊打開代碼文件沒有反應(yīng),也許我又漏裝了什么……
但是有個(gè)最簡單的解決方案就是:重新用記事本新建一個(gè)就好了。
所以我選擇讓他把修改完的完整代碼給我,而不是告訴我修改的步驟:

游戲可以運(yùn)行之后,我就發(fā)現(xiàn)速度太快了,于是我要求增加速度調(diào)整功能。

又或者增加更多的功能。

又經(jīng)歷了多次報(bào)錯(cuò)和反復(fù)拉鋸(是的,AI 竟然嫌棄代碼太長不愿意寫,那就重開一個(gè)窗口把要求復(fù)制過來提問,然后 AI 就乖乖地給了全部代碼)。
最后,游戲成功運(yùn)行了,打開游戲長這樣:

先選擇速度,然后可以點(diǎn)擊開始游戲:

吃了果實(shí)會(huì)變長。

如果撞墻了就會(huì)提示:

OK,上面就是我人生中編寫的第一個(gè)游戲的全過程,代碼就開源(是這么說對吧……)在下面了!
哈哈,好中二的感覺~
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)? # 默認(rèn)速度為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="復(fù)活", 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的發(fā)展太快了,隨著插件的不斷豐富,已經(jīng)可以實(shí)現(xiàn)非常多的功能,大家一定要去嘗試~
