海龟图形检索重叠形状的颜色

2024-10-03 15:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一颗红色的星星部分刻在一个紫色的正方形里,这个正方形包含一个橙色的圆圈。我正在提取用户单击的点的颜色。当我点击方块内的圆圈时,返回的颜色是紫色,而不是橙色。当我点击方块内的红星部分时,程序也会返回紫色。我怎样才能纠正这个问题?谢谢您。在

import turtle

def border(height,color):

    height = float(height)
    length = height *(1.9)
    length = round(length,2)

    # Draws a rectangle.
    turtle.begin_fill()
    turtle.color(color)
    turtle.down()
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.end_fill()

def big_shape(vertices, steps, length):
    turtle.color("red")
    turtle.begin_fill()
    for i in range(vertices):
        turtle.forward(length)
        turtle.right(steps*360.0/vertices)
    turtle.end_fill()

def textbox_click(rawx,rawy):
    turtle.up()
    turtle.setposition(rawx,rawy)
    turtle.down()
    rawy = -rawy
    canvas = turtle.getcanvas()
    canvas.pack(fill="both", expand=True)
    ids = canvas.find_overlapping(rawx, rawy, rawx, rawy)
    if ids: # if list is not empty
        index = ids[0]
        color = canvas.itemcget(index, "fill")
        if color != '':
            print(color.lower())

def getcoordinates():
    turtle.onscreenclick(turtle.goto)
    turtle.onscreenclick(modifyglobalvariables) # Here's the change!
    turtle.onscreenclick(textbox_click)

def modifyglobalvariables(rawx,rawy):
    global xclick
    global yclick
    xclick = int(rawx//1)
    yclick = int(rawy//1)
    print(xclick)
    print(yclick)

def main():
    border(150,"purple") 
    turtle.begin_fill()
    turtle.down()
    turtle.color("purple")
    turtle.up()

    # Creates the big shape

    x1=150
    y1=3
    turtle.setposition(x1,y1) 
    big_shape(5,2,50)
    turtle.begin_fill()
    turtle.down()
    turtle.up()

    # Circle
    x1=70
    y1=-107
    turtle.setposition(x1,y1) 
    turtle.begin_fill()
    turtle.circle(50)
    turtle.color("orange")
    turtle.end_fill()

    getcoordinates()

    turtle.done()
main()

Tags: rightdeffilllengthcolordowncanvasforward
2条回答

我建议用不同的方法来解决这个问题。我建议您完全在turtle内部工作并使绘制的对象处于活动状态,而不是使用tkinter基础来查找非活动对象的颜色。我们可以让每一张画都有一个海龟光标,这样我们就可以点击海龟,并询问它们的颜色,这是一个更简单的问题:

import turtle

def rectangle(height):
    length = height * 2

    turtle.begin_poly()
    for _ in range(2):
        turtle.forward(length)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
    turtle.end_poly()

    return turtle.get_poly()

def star(vertices, steps, length):
    angle = steps * 360.0 / vertices

    turtle.begin_poly()
    for _ in range(vertices):
        turtle.forward(length)
        turtle.right(angle)
    turtle.end_poly()

    return turtle.get_poly()

def circle(radius):
    turtle.begin_poly()
    turtle.circle(radius)
    turtle.end_poly()

    return turtle.get_poly()

def display_color(turtle):
    print(turtle.fillcolor())

def main():
    # Use the "default" turtle to draw the others
    turtle.penup()
    turtle.hideturtle()
    turtle.setheading(90)
    turtle.speed('fastest')

    screen.register_shape('rectangle', rectangle(150))
    screen.register_shape('star', star(5, 2, 50))
    screen.register_shape('circle', circle(50))

    rectangle_turtle = turtle.Turtle('rectangle')
    rectangle_turtle.penup()
    rectangle_turtle.color('purple')
    rectangle_turtle.onclick(lambda x, y: display_color(rectangle_turtle))

    star_turtle = turtle.Turtle('star')
    star_turtle.penup()
    star_turtle.setposition(150, 3)
    star_turtle.color('red')
    star_turtle.onclick(lambda x, y: display_color(star_turtle))

    circle_turtle = turtle.Turtle('circle')
    circle_turtle.penup()
    circle_turtle.setposition(70, -107)
    circle_turtle.color('orange')
    circle_turtle.onclick(lambda x, y: display_color(circle_turtle))

screen = turtle.Screen()

main()

screen.mainloop()

现在您应该能够单击任何填充的彩色区域,您将看到在控制台窗口中打印的颜色名称。(单击窗口本身使其处于活动状态后。)

enter image description here

我看到两个问题

首先:previous question查看我的代码-您必须获得最后一个元素ids[-1],而不是第一个ids[0],才能获得最顶层的元素。在

第二步:你把乌龟移动到点击的地方,所以现在turle是最上面的-所以你可以在你得到颜色后移动鼠标,然后你仍然可以使用

 index = ids[-1]

或者你必须从末尾得到第二个ids[-2],但是你必须检查ids是否至少有两个元素

^{pr2}$

相关问题 更多 >