神币小代码
2025-12-20 15:48:49
发布于:广东
包装的turtle.
import turtle
class Drawer:
def __init__(self,step = 50,line_color = "black",fill_color = "red"):
self.t = turtle.Turtle()
self.step = step
self.t.color(line_color,fill_color)
def f(self,step = -1):
if step == -1:
step = self.step
self.t.forward(step)
def l(self,direction = 90):
self.t.left(direction)
def r(self,direction = 90):
self.t.right(direction)
def beg_fill(self):
self.t.begin_fill()
def e_fill(self):
self.t.end_fill()
def rd(self,command):
for i in command:
if i == 'f':
self.f()
if i == 'l':
self.l()
if i == 'r':
self.r()
if i == 'b':
self.beg_fill();
if i == 'e':
self.e_fill();
def comp_command(self,command):
line_sep_commands = command.split("\n")
for line_command in line_sep_commands:
splited_command = line_command.split(" ")
head = splited_command[0]
param = splited_command[1:]
#处理命令头
if head == "f":#前进
if param == []:
self.f()
else:
self.f(int(param[0]))
if head == "l":#左转
if param == []:
self.l()
else:
self.l(int(param[0]))
if head == "r":#右转
if param == []:
self.r()
else:
self.r(int(param[0]))
if head == "bf":
self.beg_fill()
if head == "ef":
self.e_fill()
if head == "sc":
if param == []:
raise Exception("sc command requires 2 parameters")
else:
if len(param)==1:
self.t.color(param[0])
else:
self.t.color(param[0],param[1])
d = Drawer()
#d.rd("bfffflfflfrfflflffrfflffe")
d.comp_command("""sc red black
bf
f 100
r 60
f 100
r 60
f 100
r 60
f 100
r 60
f 100
r 60
f 100
r 60
ef
""")
这里空空如也








有帮助,赞一个