简体   繁体   中英

can a python turtle sample background colour?

I would like to use python's turtles to effectively simulate a lego robot. 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.

So far, I can create a simple canvas using tkinter and with a coloured rectangle on that canvas. 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? ). 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. 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.

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. 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"). In python and many other languages, upper case means the Class itself, not an instance of it). I left it alone in my code above so you could try out the fix with minimal changes to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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