简体   繁体   English

可以蟒蛇龟样本背景颜色?

[英]can a python turtle sample background colour?

I would like to use python's turtles to effectively simulate a lego robot. 我想用python的海龟来有效地模拟乐高机器人。 The lego robots have the ability to sample the colour that the robot is positioned over. 乐高机器人能够对机器人定位的颜色进行采样。 With this in mind, I would like to create a background maze and have the robot find its way though the maze. 考虑到这一点,我想创建一个背景迷宫,并让机器人通过迷宫找到它的方式。 I hope to use this with grade 10 programming students. 我希望与10年级的编程学生一起使用。

So far, I can create a simple canvas using tkinter and with a coloured rectangle on that canvas. 到目前为止,我可以使用tkinter创建一个简单的画布,并在该画布上使用彩色矩形。 I can place the turtle on the canvas and have them co-exist. 我可以将乌龟放在画布上并让它们共存。 The turtle can be placed on the coloured rectangle. 乌龟可以放在彩色矩形上。

Now I just need to be able to have the colour sampled in some way. 现在我只需要能够以某种方式对颜色进行采样。 This may be done through getting the turtle's position and then sampling that coordinate. 这可以通过获取乌龟的位置然后对该坐标进行采样来完成。 But I am stuck at this point. 但我在这一点上陷入困​​境。

Here's my code so far: 到目前为止,这是我的代码:

from tkinter import Tk, Canvas, Frame, BOTH
import turtle

top = Tk()
C = Canvas(top, height=500, width=600)

Doug = turtle.RawTurtle(C)

rectangle = C.create_rectangle(30, 10, 120, 80, outline="#fb0", fill="#fb0")

Doug.fd(50)
Doug.rt(90)
Doug.fd(50)

C.pack(fill=BOTH, expand=1)
top.mainloop()

I do notice that when I run the code, the turtle's 'trail' is behind the rectangle, suggesting a layering problem. 我注意到,当我运行代码时,乌龟的“踪迹”位于矩形后面,表明存在分层问题。

From what the documentation says about turtle it doesn't have any function to get the color of the current position. 根据文档中关于乌龟的说法,它没有任何功能来获取当前位置的颜色。 My approach would be to use a binary map as walk indicator but this is probably too static to simulate a lego robot environment. 我的方法是使用二进制映射作为步行指示器,但这可能太静态,无法模拟乐高机器人环境。

If you are on windows you can however make use of the win32gui library, which will give you a pixelcolor from a pixel on your screen ( Is it possible to get the color of a particular pixel on the screen with it's X and Y coordinates? ). 如果你在Windows上,你可以使用win32gui库,它将为你提供屏幕上一个像素的像素颜色是否可以用它的X和Y坐标获得屏幕上特定像素的颜色? ) 。 Combine that with the turtle.hideturtle() and turtle.pos() and add some offsets for the parent window and you will probably get the desired functionality. 将它与turtle.hideturtle()turtle.pos()结合使用,并为父窗口添加一些偏移量,您可能会获得所需的功能。 If this is working for you I suggest you create your own module providing this functionality so that the students can focus on solving the labyrinth rather than on windows apis. 如果这对您有用,我建议您创建自己的模块,提供此功能,以便学生可以专注于解决迷宫而不是Windows apis。

You could ask the Canvas what items are beneath the Turtle and report the fill color of any that you are interested in. For instance, you could add all the rectangles you are going to draw for the maze to a rectangles list, and use that list to determine if the turtle is in the rectangle. 您可以向Canvas询问Turtle下面的项目并报告您感兴趣的任何填充颜色。例如,您可以将要为迷宫绘制的所有矩形添加到矩形列表中,并使用该列表确定乌龟是否在矩形中。 Then report the color. 然后报告颜色。

from tkinter import Tk, Canvas, Frame, BOTH
import turtle

top = Tk()
C = Canvas(top, height=500, width=600)

Doug = turtle.RawTurtle(C)

rectangles = []
rectangles.append(C.create_rectangle(30, 10, 120, 80, outline="#fb0", fill="#fb0"))

Doug.fd(50)
Doug.rt(90)
Doug.fd(50)

# For some reason Doug's y-coord is opposite what Canvas uses, so * -1 to fix it... 
overlapping = C.find_overlapping(Doug.xcor(), Doug.ycor() * -1, Doug.xcor(), Doug.ycor() * -1)
for item_id in overlapping:
   if item_id in rectangles:
        print(C.itemcget(item_id, "fill"))

C.pack(fill=BOTH, expand=1)
top.mainloop()

Just as a final thought but unrelated to the question. 正如最后的想法,但与问题无关。 I highly suggest you use lower case names for instantiation of objects (change "C" to "canvas" and "Doug" to "doug"). 我强烈建议您使用小写名称来实例化对象(将“C”更改为“canvas”,将“Doug”更改为“doug”)。 In python and many other languages, upper case means the Class itself, not an instance of it). 在python和许多其他语言中,大写表示Class本身,而不是它的实例)。 I left it alone in my code above so you could try out the fix with minimal changes to your code. 我在上面的代码中单独留下了它,因此您可以尝试对代码进行最少的更改。

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

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