Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame: Draw single pixel

Tags:

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).

like image 620
ashur Avatar asked Apr 27 '12 16:04

ashur


People also ask

What is get_rect () in pygame?

The method get_rect() returns a Rect object from an image. At this point only the size is set and position is placed at (0, 0).


1 Answers

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)
like image 67
Gareth Latty Avatar answered Oct 10 '22 04:10

Gareth Latty