我是一名学生,上了第一堂python课,我们正在Turtle
画图像。为了节省时间,我尝试创建一个镜像函数,该函数可以将一个函数结果沿y轴翻转给定数量的像素。为了简单起见,我创建了一些函数来加快编码速度。这是我的代码的工作方式:
units, north, east, south, west = desiredPixelsPerUnit, 90, 0, 270, 180
def main():
eye(30, 15)
eye(40, 15)
def left():
myTurtle.left(90)
def draw(count):
distance = units * count
myturtle.forward(distance)
def singleStep(xDirection, yDirection, stepCount):
down()
for step in range(stepCount):
if yDirection == north:
point(north)
draw(1)
if xDirection == east:
point(east)
draw(1)
etc..
def eye(xPosition, yPosition):
....
draw(3)
left()
draw(2)
left()
....
singleStep(east, north, 1)
singleStep(west, north, 2)
etc....
所有这一切给我以下
eye()
在main中运行两次的结果:
我试图创建的是一个传递给另一个函数的函数,然后将查看正在执行的函数。如果是,left()
或者right()
相反。如果是点,则(x, y)
添加180
to
x。如果它有一个在函数内部调用的函数,则它还会检查它的左边或右边。这样的事情。
def mirror(function):
for action in function:
if action == left():
execute right()
...
elif action == singleStep():
newFunction = singleStep()
for action in newFunction:
if:
statement above
else:
statement below
else:
execute initial action
我还是编码和编程的新手。我试过使用数组,关联数组和列表,eval
以及更多。试图弄清楚该问题所花的时间比为左右哈哈写一个单独的指令列表要长得多,但是我真的很想弄清楚如何做这样的事情。
您可以直接退出left()
并退出right()
。只需创建自己的函数your_left()
,your_right()
然后每次都使用它们即可。然后使用一个名为的全局变量mirror
。该变量将为您充当标志。因此,您只需设置mirror
为True
要镜像输出的时间即可。
你的功能your_right()
和your_left()
将寻找这样的事情:
def your_right():
left() if mirror else right()
def your_left():
right() if mirror else left()
然后,您可以随意镜像所有输出。我希望我能帮忙!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句