Pygame: Draw single pixel

You can do this with surface.set_at():

surface.set_at((x, y), color)

You can also use pygame.gfxdraw.pixel():

from pygame import gfxdraw
gfxdraw.pixel(surface, x, y, color)

Do note, however, the warning:

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.

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)

Leave a Comment