Python简单 turtle 程序

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

我在尝试完成 python turtle 程序时遇到问题。当我尝试输入 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) messed up snowman

这是我没有输入参数时的雪人 t.drawsnowman() correct snowman

最佳答案

尝试逐步进行并弄清楚您的代码实际做了什么(或使用调试器)。

  • 如果调用 t.drawSnowman()radius 将为 15
  • 您调用 bottomCircle(),它将绘制一个半径为 radius * 3 = 45
  • 的圆
  • 您在 y 轴上移动 radius * 6 = 90(您现在位于圆的“顶部”)<
  • 您调用 middleCircle(),它将生成一个半径为 radius * 2 = 30
  • 的圆
  • ...

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

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

因此,您的问题是您为 radius 传递了一个值,但您仅将该值用于在 drawSnowman 中移动 turtle ,而不是在任何进一步向下移动其他功能。

关于Python简单 turtle 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757542/

相关文章:

python - 使用 16 位 block 将数字表示为字节

function - 将数组传递给函数

c++ - C++ 函数中的消息传递

python - def - else 语句不起作用

python - 即使我使用opencv-contrib-python更新了opencv之后,我仍然遇到属性错误

python - 如何让python脚本等待linux脚本完成

python - Snakemake选择了运行规则

python - 通过多级函数传递 kwargs,解包其中一些但传递所有函数

python - Scikit-learn 中用于多标签分类的 GridSearch

python - 使用短于 8 位的 PacketField 构建 scapy 数据包