简体   繁体   English

我似乎无法让我的两只眼睛为 Turtle 工作

[英]I can't seem to get my 2 eyes working for Turtle

So what am I missing here?那么我在这里错过了什么? I simply need to have 2 eyes that are filled in with the color red.我只需要有两只用红色填充的眼睛。 I'm not sure what is missing for the "def eye", but I'm sure that's where I have made a mistake.我不确定“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()

I think you are trying to do this:我认为您正在尝试这样做:

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()

You haven't positioned correctly!你没有正确定位! I think in your code you created eye function but never use that, but you don't need that function anyway.我认为在您的代码中您创建了eye功能但从未使用过它,但无论如何您都不需要该功能。 We can do anything circular in DCircle function.我们可以在DCircle函数中做任何循环。

Edit : You can Mickey function this then you will get as you say 2 eyes and 2 ears!编辑:您可以使用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")

I don't think you need to make that eye function!我认为你不需要做那个eye功能!

What's you are doing wrong in your code :你在代码中做错了什么:

  1. First thing, you haven't call that eye function, and you are expecting to work.首先,你还没有调用那个眼功能,你期待着工作。 You have to call that function first then only it will work.您必须先调用该函数,然后才能工作。
  2. There is an error in t.color(red) you can't write color name like this, color name should always be string! t.color(red)有一个错误你不能像这样写颜色名称,颜色名称应该总是字符串!
  3. If you have fixed all above wrong things, then also you only get left eye cuz you have hard coded the position of turtle like this t.setposition(-50,40) .如果你已经修复了以上所有错误的东西,那么你也只能得到左眼,因为你已经像这样t.setposition(-50,40)硬编码了乌龟的位置。
  4. If you to hard code the eyes then, you have to make 2 function.如果您对眼睛进行硬编码,则必须创建 2 个函数。 One for left and second for eye.一个用于左眼,第二个用于眼睛。
  5. Twist : You don't have to create new functions, you can use DCircle function to do all these things, you have already made dynamic function to make any type of circle then, why you need to make new function and over complicate the things. Twist :您不必创建新函数,您可以使用DCircle函数来完成所有这些事情,您已经制作了动态函数来制作任何类型的圆,那么为什么您需要制作新函数并且使事情变得过于复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM