【问题标题】:python canvas.find_overlapping appears to have inverted y-axispython canvas.find_overlapping 似乎有倒置的 y 轴
【发布时间】:2019-03-15 21:46:18
【问题描述】:

我正在尝试在 Python 乌龟下找到画布的颜色。我使用canvas.find_overlapping,但只有在我否定ycor 时才会成功,这意味着与显示的内容相比,画布对象中的y 轴是倒置的。是我的代码有问题还是y轴倒转了?

import turtle

wn = turtle.Screen()

maze_drawer = turtle.Turtle()
maze_drawer.color("purple")
maze_drawer.speed("fastest")
path_width = 15 

def get_pixel_color(x, y):
    c = turtle.Screen().getcanvas()
    # -y should not work??
    items = c.find_overlapping(x, -y, x, -y)
    if len(items) > 0:
        return c.itemcget(items[0], "fill") # get 0 object (canvas)

# draw simplified maze
wall_len = 0
for i in range(10): 
    maze_drawer.left(90)
    wall_len += path_width
    maze_drawer.forward(wall_len)

# navigate maze from center
maze_runner = turtle.Turtle()
maze_runner.color("green")
maze_runner.penup()
maze_runner.goto(-path_width, -path_width)
# test in y dir: maze_runner.setheading(90)

clear = True
while(clear):
    maze_runner.forward(1)
    color_at_turtle = get_pixel_color(maze_runner.xcor(), maze_runner.ycor())
    if (color_at_turtle == "purple"): 
        clear = False

wn.exitonclick()

【问题讨论】:

  • turtle 模块反转所有 y 坐标,以获得传统的海龟图形的 Y 轴向上行为。如果您绕过模块并直接使用 Tkinter,则需要自己进行反转。

标签: python pixel turtle-graphics tkinter-canvas


【解决方案1】:

在海龟中巧妙地使用 tkinter 像素检测!如果倒 Y 坐标比较麻烦,可以从海龟的角度翻转一下:

from turtle import Screen, Turtle

screen = Screen()
width, height = screen.window_width() / 2, screen.window_height() / 2
screen.setworldcoordinates(-width, height, width, -height)  # flip Y coordinate

那么你的代码就不必考虑否定 Y,只要你知道你画的是颠倒的:

from turtle import Screen, Turtle

PATH_WIDTH = 15

def get_pixel_color(x, y):
    canvas = screen.getcanvas()
    items = canvas.find_overlapping(x, y, x, y)

    if items:
        return canvas.itemcget(items[0], "fill")  # get 0 object (canvas)

    return None

screen = Screen()
width, height = screen.window_width() / 2, screen.window_height() / 2
screen.setworldcoordinates(-width, height, width, -height)

maze_drawer = Turtle(visible=False)
maze_drawer.color("purple")
maze_drawer.speed("fastest")

# draw simplified maze
wall_len = 0

for _ in range(20):
    maze_drawer.left(90)
    wall_len += PATH_WIDTH
    maze_drawer.forward(wall_len)

# navigate maze from center
maze_runner = Turtle()
maze_runner.color("dark green", "green")
maze_runner.penup()
maze_runner.goto(-PATH_WIDTH, -PATH_WIDTH)

def run_maze():
    maze_runner.forward(1)

    x, y = maze_runner.position()
    color_at_turtle = get_pixel_color(x, y)

    if color_at_turtle == "purple":
        maze_runner.backward(PATH_WIDTH - 1)
        maze_runner.left(90)
        x, y = maze_runner.position()

    if -width < x < width and -height < y < height:
        screen.ontimer(run_maze, 10)

run_maze()

screen.exitonclick()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多