2

I'm creating a program and am attempting to draw a rectangle at the cursor's position if the pixel at the cursor's position isn't already black.

if pygame.Surface.get_at(pygame.mouse.get_pos()) != (0,0,0,255):
    pygame.draw.rect(win, (0,0,0), (x, y, 3, 3))

An error occurs when I try to implement pygame.Surface.get_at() that says ...

TypeError: descriptor 'get_at' for 'pygame.Surface' objects doesn't apply to a 'tuple' object

.. even though the documentation for pygame.Surface.get_at() on https://pygame.org shows that the input for the method is supposed to be a tuple.

get_at()
    get the color value at a single pixel
    get_at((x, y)) -> Color

    Return a copy of the RGBA Color value at the given pixel. If the Surface has no per pixel alpha, then 
    the alpha value will always be 255 (opaque). If the pixel position is outside the area of the Surface 
    an IndexError exception will be raised.

How can I fix this?

notgriffin
  • 23
  • 3

1 Answers1

0

you need to call get_at on a pygame.Surface object, not the class itself.


screen = pygame.display.set_mode(size)

pixel = screen.get_at(pygame.mouse.get_pos()) #gets the pixel on the screen surface

another_surface = pygame.Surface((100, 100))

another_pixel = another_surface.get_at((50, 50)) #gets the pixel from the 'another_surface' surface

The Big Kahuna
  • 2,097
  • 1
  • 6
  • 19
  • Do I absolutely have to create a surface and a screen and put rectangles on both of them to be able to check for a certain pixel? Because then I have to replicate everything that happens on the surface to the screen, and it feels inefficient. – notgriffin Jul 19 '20 at 01:55
  • 1
    @notgriffin no, you dont need both, just one or the other. they were 2 different examples. because the screen is a surface, if you have a surface, use that – The Big Kahuna Jul 19 '20 at 01:58