如何使用 Python turtle.py 读取像素颜色 [英] How to read pixel colours using Python turtle.py

查看:96
本文介绍了如何使用 Python turtle.py 读取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为涉及 Python 的 turtle 库的学校课程准备练习.

I'm preparing exercises for school classes involving Python's turtle library.

学生们已经在画了很棒的图片,但我希望他们能够检测现有的图片和颜色,以便修改他们程序的行为.

The students are already drawing terrific pictures, but I want them to be able to detect existing pictures and colours in order to modify the behaviour of their program.

例如,我想为他们提供使用乌龟绘制迷宫的代码,然后他们可以编写代码来在迷宫中导航乌龟(别担心,我会开始更简单).

For example I would like to provide them with code which draws a maze using turtle, and then they can write the code to navigate the turtle around the maze (don't worry, I'll start simpler).

有没有办法检测乌龟已经绘制的像素的颜色?

Is there a way to detect the colour of the pixels already drawn by the turtle?

谢谢!

推荐答案

Turtle 使用 Tkinter 画布,您可以使用 turtle.getcanvas() 获得,并且根据 this 如果不使用将画布转换为图片(位图)的解决方法,您将无法读取像素的颜色并读取位图.

Turtle uses Tkinter canvas, which you can get using turtle.getcanvas(), and according to this you cannot read the colour of a pixel without using a workaround of converting the canvas to a picture (bitmap) and read the bitmap.

您可以尝试保留一个开放数组作为画布的位图,并在您在画布上绘制新元素时自行更新它,尽管这似乎不切实际,除非迷宫简单且方形".

You could try to keep an open array to work as the bitmap of your canvas and update it yourself as you draw new elements on the canvas, although that seems impractical unless the maze is simple and 'squary'.

这篇关于如何使用 Python turtle.py 读取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆