import turtle as t
from turtle import *
from math import sin, pi, sqrt
setup(500, 500)
Screen()
title("Turtle Art")
showturtle()
screen = Screen()
yertle = Turtle()
turn = 90
speed = 10
moves = []
def k1():
t.forward(speed)
moves.append(1)
def k2():
t.left(turn)
moves.append(2)
def k3():
t.right(turn)
moves.append(3)
def k4():
t.backward(speed)
moves.append(4)
def k102():
if input("Would you like to save this? (y/n) ") == "y":
name = input("What would you like to call your masterpiece? ")
my_file = open("./%s" % (name), "w")
global moves
moves = str(moves)
my_file.write(moves)
my_file.close()
print("It has been saved as:", name)
else:
t.clear
t.onkey(k1, "Up") #move forwards
t.onkey(k2, "Left") #turn left
t.onkey(k3, "Right") #turn right
t.onkey(k4, "Down") # move backwards
t.onkey(k102, "p") #save page
listen()
mainloop()
這將其保存爲當前文件夾中的文本文件中的列表(看起來像這樣:[1,1,2,1,3,3 ,1])。我想知道如何通過移動根據1,2,3或4來讓龜重現這一點。有什麼方法可以輕鬆地從文本文件中讀取嗎?如何讓蟒蛇根據文本文件移動
這個作品完全謝謝你,我會把它添加到我的項目中 – Gurneyguy