【发布时间】:2016-02-23 08:25:00
【问题描述】:
所以到目前为止,我有这个代码;
import turtle
import random
def getColor():
selection = random.randint(1,3)
if selection == 1:
return "green"
elif selection == 2:
return "red"
else:
return "blue"
def drawPerson(my_turtle, x, y):
my_turtle.penup()
my_turtle.goto(x,y)
my_turtle.pendown()
my_turtle.pensize(3)
xul.color = getColor()
print(xul.color)
if xul.color == "green":
xul.color("green")
elif xul.color == "red":
xul.color("red")
else:
xul.color("blue")
xul.forward(90)
xul.left(90)
xul.forward(90)
xul.left(90)
xul.forward(90)
xul.left(90)
xul.forward(90)
xul.left(90)
xul = turtle.Turtle()
drawPerson(xul,150,150)
drawPerson(xul,-150,150)
drawPerson(xul,-150,-150)
drawPerson(xul,150,-150)
drawPerson(xul,0,0)
问题是..我可以生成颜色,它打印它绘制所有形状的颜色,但我不知道如何在乌龟绘制之前使颜色实际应用于乌龟。我尝试过类似的东西;
xul.color("getColor")
还有许多其他的东西,但我似乎无法弄清楚。如果有人能给我我缺少的代码行,我将不胜感激!谢谢!
【问题讨论】:
-
哦,对不起,可以删除
if xul.color == "green": xul.color("green") elif xul.color == "red": xul.color("red") else: xul.color("blue")这整套字符串没有做任何事情并返回错误只是我正在测试的东西 -
你必须调用函数,而不是函数名。即
xul.color(getColor()) -
所以我取出
xul.color = getColor()添加而不是xul.color(getColor()),现在它显示了 drawPersons 及其颜色(谢谢),但现在它不打印它正在使用的颜色,而是说> 的方法 Turtle.color 它运行代码,就在它打印 xul.color 时它不会说红色、绿色或蓝色 -
xul.color是一个函数,如果要返回颜色,加括号xul.color(),不要传参数。 -
好吧,谢谢到目前为止,所以我添加了 print(xul.color()) 并在绘制正方形时在框中打印颜色,但是现在它正在显示颜色如果您知道我的意思,那么坐标显示它的 2 倍就像 ('red', 'red')
标签: python random generator turtle-graphics