我正在创建一个带有乌龟图形的艺术展。这就是我所拥有的。我能够通过单独输入每一行来运行代码。每个单独的代码块都会绘制一个项目(天空,花朵,草等)。如何将所有这些项目放入一个将运行的函数中?或者那是不可能的?
from turtle import *
def art_show():
speed(30)
penup()
goto(-100,160)
pensize(30)
pencolor('deep sky blue')
pendown()
fd(500)
penup()
goto(-100,175)
pendown()
fd(500)
penup()
goto(-100,190)
pendown()
fd(500)
penup()
goto(60,170)
pencolor('yellow')
pendown()
circle(10)
penup()
goto(-100,-100)
pencolor('forest green')
pendown()
fd(500)
penup()
goto(-100,-85)
pendown()
fd(500)
penup()
goto(-100,-70)
pendown()
fd(500)
penup()
goto(-75,-75)
pendown()
pensize(3)
pencolor('dark green')
lt(90)
fd(100)
penup()
goto(-40,-90)
pendown()
fd(200)
penup()
goto(-75, 25)
pendown()
for i in range(6):
pencolor("slateblue")
circle(25)
lt(60)
for i in range(6):
pencolor('dodgerblue')
circle(15)
lt(60)
for i in range(6):
pencolor('deeppink')
circle(5)
lt(60)
penup()
goto(-40, 110)
pendown()
for i in range(6):
pencolor("red")
circle(25)
lt(60)
for i in range(6):
pencolor('pink')
circle(15)
lt(60)
for i in range(6):
pencolor('yellow')
circle(5)
lt(60)
penup()
goto(20,-60)
pencolor('brown')
pendown()
rt(90)
fd(140)
lt(90)
fd(100)
lt(45)
fd(100)
lt(90)
fd(100)
lt(45)
fd(100)
penup()
goto(20, 40)
pendown()
lt(90)
fd(140)
penup()
goto(70,-60)
pendown()
lt(90)
pencolor('black')
fd(45)
rt(90)
fd(30)
rt(90)
fd(45)
答案 0 :(得分:0)
只需添加:
if __name__ == '__main__':
art_show()
到文件的底部。然后它适合我。
答案 1 :(得分:0)
您只需在定义art_show()
后致电art_show()
。下面是对代码的修改,以简化它并解决一些编码风格问题:
from turtle import *
def jump(x, y):
penup()
goto(x, y)
pendown()
def art_show():
pensize(30)
pencolor('deep sky blue') # sky
jump(-100, 160)
fd(500)
jump(400, 175)
bk(500)
jump(-100, 190)
fd(500)
pencolor('yellow') # sun
jump(60, 170)
circle(10)
pencolor('forest green') # lawn
jump(-100, -100)
fd(500)
jump(400, -85)
bk(500)
jump(-100, -70)
fd(500)
pensize(3)
pencolor('dark green') # stems
jump(-75, -75)
lt(90)
fd(100)
jump(-40, -90)
fd(200)
pencolor("slateblue") # left flower
jump(-75, 25)
for _ in range(6):
circle(25)
lt(60)
pencolor('dodgerblue')
for _ in range(6):
circle(15)
lt(60)
pencolor('deeppink')
for _ in range(6):
circle(5)
lt(60)
pencolor("red") # right flower
jump(-40, 110)
for _ in range(6):
circle(25)
lt(60)
pencolor('pink')
for _ in range(6):
circle(15)
lt(60)
pencolor('yellow')
for _ in range(6):
circle(5)
lt(60)
pencolor('brown') # house
jump(20, -60)
rt(90)
fd(140)
for _ in range(2):
lt(90)
fd(100)
lt(45)
fd(100)
jump(20, 40)
lt(90)
fd(140)
pencolor('black') # door
jump(70, -60)
lt(90)
fd(45)
rt(90)
fd(30)
rt(90)
fd(45)
speed('fastest') # if number, only 0 - 10 make sense
art_show()
hideturtle()
mainloop()