简体   繁体   English

你能让 Python Turtle 检测它是否接触到特定颜色吗?

[英]Can you make a Python Turtle detect if it's touching a specific colour?

Is it possible for a turtle to detect whether it is touching a specific colour, or not, without using a grid system (ie allocating each cell a colour.) I am trying to create a pixelated world that a creature will navigate, and interact with, depending on what sort of tile it is touching, which will be determined based on the tile's colour.海龟是否有可能在不使用网格系统的情况下检测它是否正在触摸特定颜色(即为每个单元分配一种颜色。)我正在尝试创建一个生物将导航并与之交互的像素化世界,取决于它所接触的瓷砖类型,这将根据瓷砖的颜色来确定。

The best you could try to replicate this Scratch project in Python would be a grid based system using the python framework, pygame.您可以尝试在 Python 中复制此 Scratch 项目的最佳方法是使用 python 框架 pygame 的基于网格的系统。

This would mean you need to code the background, user, interface, commands, collisions.这意味着您需要对背景、用户、界面、命令、冲突进行编码。 A bigger feat to do all by your own hands.一个更大的壮举,由您自己完成。

My files indicate that this would be a good video series to get started:我的文件表明这将是一个很好的入门视频系列:

Setup: https://youtu.be/VO8rTszcW4s设置: https://youtu.be/VO8rTszcW4s

Creating The Game: https://youtu.be/3UxnelT9aCo创建游戏: https://youtu.be/3UxnelT9aCo

I hope your endeavor is fruitful!我希望你的努力是有成果的!

We can force turtle to do this by dropping down to the tkinter level.我们可以通过下降到 tkinter 级别来强制海龟执行此操作。 Although we think of things that the turtle draws as dead ink (as opposed to shaped turtles or stamps), they are in fact live ink from tkinter's point of view -- which is why we can clear an individual turtle's drawings and call undo() .尽管我们认为海龟绘制的东西是死墨水(与成形的海龟或印章相反),但从 tkinter 的角度来看,它们实际上是活墨水——这就是为什么我们可以清除单个海龟的绘图并调用undo() . Here's a fragile example that does this:这是一个执行此操作的脆弱示例:

from turtle import Screen, Turtle
from random import random

WIDTH, HEIGHT = 800, 800
DIAMETER = 200

def chameleon(x, y):
    turtle.ondrag(None)
    overlapping = canvas.find_overlapping(x, -y, x, -y)  # adjust to tkinter coordinates

    if overlapping:
        color = canvas.itemcget(overlapping[0], "fill")

        if color:
            turtle.fillcolor(color)

    turtle.goto(x, y)
    turtle.ondrag(chameleon)

screen = Screen()
screen.setup(WIDTH, HEIGHT)
canvas = screen.getcanvas()

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()

for x in range(-WIDTH//2, WIDTH//2, DIAMETER):
    for y in range(-HEIGHT//2, HEIGHT//2, DIAMETER):
        turtle.goto(x + DIAMETER/2, y + DIAMETER/2)
        color = random(), random(), random()
        turtle.dot(DIAMETER - 1, color)

turtle.home()
turtle.shape('turtle')
turtle.shapesize(2)
turtle.showturtle()
turtle.ondrag(chameleon)

screen.mainloop()

As you drag the turtle around the screen, it will pick up color from things drawn on the screen.当您在屏幕上拖动海龟时,它会从屏幕上绘制的内容中获取颜色。 This isn't a clear turtle that you're seeing through, it's reading the ink, which you can confirm for yourself as you move over the background.这不是你看到的透明乌龟,它正在阅读墨水,当你在背景上移动时,你可以自己确认。 This code may be turtle implementation specific.此代码可能是特定于海龟实现的。

I'm not sure how well this will scale up (or more likely scale down towards pixel size objects) but should give you an idea what's possible if you're willing to embrace turtle's tkinter underpinnings, or simply use tkinter itself.我不确定这将如何放大(或更可能缩小到像素大小的对象),但应该让你知道如果你愿意接受turtle的 tkinter 基础,或者只是使用 tkinter 本身会发生什么。

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

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