【问题标题】:I can't seem to get my 2 eyes working for Turtle我似乎无法让我的两只眼睛为 Turtle 工作
【发布时间】:2021-07-28 02:33:42
【问题描述】:

那么我在这里错过了什么?我只需要两只眼睛都被红色填充。我不确定“def eye”缺少什么,但我确定那是我犯错的地方。

import turtle

t = turtle.Turtle()

def DCircle(x,y,radius,color):
    t.penup()
    t.setposition(x,y)
    t.pendown()
    t.fillcolor(color)
    t.begin_fill()
    t.color(color)
    t.circle(radius)
    t.end_fill()

def eye(color, radius):
    t.penup()
    t.setposition(-50,40)
    t.pendown()
    t.fillcolor(color)
    t.color(red)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

def Mickey():
    r = 50
    DCircle(0,0,2*r,'blue')
    DCircle(-135,115,r,'red')
    DCircle(135,115,r,'red')

def Main():
    Mickey()
    turtle.done()
    turtle.bye()
    turtle.getscreen()._root.mainloop()

Main()

【问题讨论】:

  • 我认为这段代码运行良好,你的预期输出是什么,你现在得到的输出是什么?
  • 嗯,我应该有两个圆形的眼睛,里面都是红色的。你有两只眼睛在红色填充的蓝色脸上吗?
  • 哦!你是说里面的蓝色!蓝圈外没有。
  • 我得到了红色的圆形耳朵。蓝色圆脸。但我没有看到红色的圆形眼睛。
  • 是的,在蓝色圆圈内。

标签: python python-turtle


【解决方案1】:

我认为您正在尝试这样做:

import turtle

t = turtle.Turtle()

def DCircle(x,y,color,radius):
    t.penup()
    t.setposition(x,y)
    t.pendown()
    t.fillcolor(color)
    t.begin_fill()
    t.color(color)
    t.circle(radius)
    t.end_fill()

def Mickey():
    r = 50
    DCircle(0,0,'blue',2*r)
    DCircle(-50,40,"red",r)
    DCircle(50,40,"red",r)

def Main():
    Mickey()
    turtle.done()
    turtle.bye()
    turtle.getscreen()._root.mainloop()

Main()

你没有正确定位!我认为在您的代码中您创建了 eye 函数但从未使用过它,但无论如何您都不需要该函数。我们可以在DCircle 函数中做任何循环。

编辑: 您可以Mickey 使用此功能,然后您将得到如您所说的 2 只眼睛和 2 只耳朵!

def Mickey():
    r = 50
    DCircle(0,0,2*r,'blue')
    DCircle(-135,115,r,'red')
    DCircle(135,115,r,'red')
    DCircle(-50,100,20,"red")
    DCircle(50,100,20,"red")

我认为您不需要使eye 起作用!

你在代码中做错了什么:

  1. 首先,您还没有调用该眼睛功能,并且您期望工作。您必须先调用该函数,然后它才会起作用。
  2. t.color(red) 中有一个错误,您不能这样写颜色名称,颜色名称应始终为字符串!
  3. 如果你已经修复了以上所有错误的东西,那么你也只会得到左眼,因为你已经像这样t.setposition(-50,40) 硬编码了海龟的位置。
  4. 如果你对眼睛进行硬编码,你必须做 2 个函数。一个用于左侧,第二个用于眼睛。
  5. Twist : 你不必创建新函数,你可以使用DCircle函数来做所有这些事情,你已经做了动态函数来制作任何类型的圆圈,为什么你需要创造新功能,让事情变得过于复杂。

【讨论】:

  • 不不,这不是我想要做的……耳朵需要保持在原位。我只需要添加眼睛。我的想法是这只是我的“def eye”声明。
  • 总共应该是4个红色圆圈,2个耳朵,2个眼睛。我只是失去了眼睛,我试图弄清楚为什么会这样......
  • 你有没有提到你想在那个大蓝色之外再做 4 个圆圈?
  • 我以为我做到了。应该是米老鼠的照片。所以 2 只耳朵和 2 只眼睛。蓝色圆圈本质上应该是脸。在那张蓝色的脸上应该有两只眼睛是红色的圆圈。我以为我的“def eye”会起作用,但你说不要使用它。因此,如果您查看我粘贴的原始代码并删除“def eye”部分,这就是我开始使用的代码。但我不知道如何再次将新代码粘贴到评论上。
  • 如果你可以从单个函数做同样的事情,为什么你需要添加另一个函数并使正确的事情复杂化!
猜你喜欢
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多