【问题标题】:Python simple turtle progamPython简单的乌龟程序
【发布时间】:2014-05-10 13:17:52
【问题描述】:

我在尝试完成一个 python 乌龟程序时遇到了麻烦。当我尝试输入 x 和 y 坐标以及函数的半径值时 t.drawSnowman(x = 25,y = 25,radius = 25) 程序在我输入值时行为异常。但是如果我省略上面的参数而只使用 t.drawSnowman() 程序按预期工作但我不是能够创建雪人的各种实例。

我真的很想帮助我弄清楚如何输入参数并仍然具有程序功能。

这是我的代码

import turtle

class MyTurtle(turtle.Turtle):
""""""


def __init__(self):
    """Turtle Constructor"""
    turtle.Turtle.__init__(self, shape="turtle")

def drawNose(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw nose '''
    self.pendown()
    self.goto(-(radius) * (0.25),(radius * 6)+(radius * 4)+(radius))
    self.goto(0,(radius * 6)+(radius * 4)+(radius)+(radius * (0.25)))
    self.goto((radius) * (0.25),(radius * 6)+(radius * 4)+(radius))
    self.goto(0,(radius * 6)+(radius * 4)+(radius))
    self.penup()

def leftEye(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw left eye'''
    self.pendown()
    self.circle(radius*(.25))
    self.penup()
def rightEye(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw right eye'''
    self.pendown()
    self.circle(radius*(.25))
    self.penup()
def bottomOfHat(self, x=0, y=0, radius = 15, circle_color = "black"):
    ''' draw the long part of the hat at the bottom '''
    self.goto(0,(radius * 6)+(radius * 4)+(radius * 2))
    self.pendown()
    self.goto(-(radius),(radius * 6)+(radius * 4)+(radius * 2))
    self.goto(-(radius),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
    self.goto(radius,(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
    self.goto(radius,(radius * 6)+(radius * 4)+(radius * 2))
    self.goto(0,(radius * 6)+(radius * 4)+(radius * 2))
    self.penup()

def topOfHat(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw the top bigger part of the hat'''
    self.goto(radius*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))
    self.pendown()
    self.goto(radius*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * 2))
    self.goto(-(radius)*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * 2))
    self.goto(-(radius)*(.75),(radius * 6)+(radius * 4)+(radius * 2)+(radius * (0.5)))

def bottomCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draws the bottom circle'''
    self.pendown()
    self.circle(radius * 3)
    self.penup()
def middleCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw the middle circle'''
    self.pendown()
    self.circle(radius * 2)
    self.penup()
def topCircle(self, x=0, y=0, radius = 15, circle_color = "black"):
    '''draw the top circle'''
    self.pendown()
    self.circle(radius)
    self.penup()
def drawSnowman(self, x=0, y=0, radius = 15, circle_color = "black"):
    self.bottomCircle()
    self.goto(0,radius * 6)
    self.middleCircle()
    self.goto(0,(radius * 6)+(radius * 4))
    self.topCircle()
    self.goto(-(radius) * (0.5),(radius * 6)+(radius * 4)+(radius))
    self.leftEye()
    self.goto((radius) * (0.5),(radius * 6)+(radius * 4)+(radius))
    self.rightEye()
    self.goto(0,(radius * 6)+(radius * 4)+(radius))
    self.drawNose()
    self.bottomOfHat()
    self.topOfHat()



t = MyTurtle()
t.hideturtle()
radius = 15
t.drawSnowman(x = 25,y = 25,radius = 25)

这是我使用参数时的雪人图片 t.drawsnowman(x = 25 y=25 半径 = 25)

这是我不输入参数时的雪人 t.drawsnowman()

【问题讨论】:

  • 您将 x 和 y 参数传递给 drawSnowman,但您没有在内部使用它们。我可以看到你只使用半径。你说的绘图不起作用是什么意思?
  • 你应该发布编译输出,这样我们就可以看到它是如何不工作的。我假设你正在使用这个:docs.python.org/2/library/turtle.html (?)
  • 是的,我正在使用海龟图形,因此没有任何编译输出,因为所需的结果是绘制的图片,在本例中是雪人。通过绘图不起作用我的意思是当我运行程序时,我会在屏幕上看到雪人的不同部分。我没有将三个雪球放在一起,而是在每个雪球之间留出了 20 像素的空间。
  • 你能解释一下为什么里面没有使用 x 和 y 参数吗,我不明白为什么没有。

标签: python function python-3.x turtle-graphics


【解决方案1】:

尝试逐步了解您的代码实际执行的操作(或使用调试器)。

  • 如果你打电话给t.drawSnowman()radius15
  • 您调用bottomCircle() 将绘制一个半径为radius * 3 = 45 的圆
  • 你在 y 轴上移动 radius * 6 = 90(你现在“在”圆的顶部
  • 你打电话给middleCircle(),这将是一个半径为radius * 2 = 30的圆
  • ...

现在,看看如果你用参数调用你的函数会发生什么:

  • 如果你打电话给t.drawSnowman(x = 25,y = 25,radius = 25)radius25
  • 您调用bottomCircle() 将绘制一个半径为radius * 3 = 45 的圆。注意局部变量radius15,因为它是该参数的默认值,并且您不会将传递给drawSnowman 的值传递给bottomCircle 方法。
  • 您在 y 轴上移动 radius * 6 = 150(您现在远离圆)
  • 您调用middleCircle(),这将是一个半径为radius * 2 = 30 的圆。注意局部变量radius 也是15
  • ...

所以,您的问题是您为radius 传递了一个值,但您只使用该值将海龟移动到drawSnowman,而不是在任何其他函数中进一步向下移动。

【讨论】:

  • 谢谢,您的回答非常完美,帮助我完成了程序。我忙着看图纸,这让我分心,没有真正查看我的代码以了解实际情况。
猜你喜欢
  • 2015-12-20
  • 1970-01-01
  • 2023-03-28
  • 2017-08-09
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
相关资源
最近更新 更多