Pygame:绘制单个像素 [英] Pygame: Draw single pixel
问题描述
我正在寻找允许我在显示屏上绘制单个像素的方法.例如,当我单击鼠标时,我希望单击像素的位置改变颜色.我知道如何读取鼠标位置,但我找不到简单的像素绘制(有 screen.fill 方法,但它不能按我想要的方式工作).
I'm looking for method that allow me to draw single pixel on display screen. For example when I click mouse, I want the position of clicked pixel to change color. I know how to read mouse pos, but I could not find simple pixel draw ( there is screen.fill method but it's not working as I want).
推荐答案
您可以使用 surface.set_at()
:
You can do this with surface.set_at()
:
surface.set_at((x, y), color)
您也可以使用pygame.gfxdraw.pixel()
:
You can also use pygame.gfxdraw.pixel()
:
from pygame import gfxdraw
gfxdraw.pixel(surface, x, y, color)
但是请注意警告:
EXPERIMENTAL!: 意味着这个 api 可能会改变,或者在以后消失pygame 发布.如果您使用它,您的代码将与下一个pygame 发布.
EXPERIMENTAL!: meaning this api may change, or dissapear in later pygame releases. If you use this, your code will break with the next pygame release.
您可以使用 surface.fill()
也做这项工作:
You could use surface.fill()
to do the job too:
def pixel(surface, color, pos):
surface.fill(color, (pos, (1, 1)))
您也可以简单地绘制一条起点和终点相同的线:
You can also simply draw a line with the start and end points as the same:
def pixel(surface, color, pos):
pygame.draw.line(surface, color, pos, pos)
这篇关于Pygame:绘制单个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!