Reputation: 882
How can I get the color of the pixel directly under the pointer in pygame?
I have done a lot of research, but the answer is rather shy.
Upvotes: 0
Views: 2247
Reputation: 19805
@Malik's answer is very correct. And here is a working demo:
import pygame
import sys
pygame.init()
surface = pygame.display.set_mode( (200, 200) )
last_color = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
surface.fill( (0,0,255) )
pygame.draw.rect( surface, (255,0,0), (0, 0, 100, 100) )
pygame.draw.rect( surface, (0,255,0), (100, 100, 100, 100) )
color = surface.get_at(pygame.mouse.get_pos())
if last_color != color:
print(color)
last_color = color
pygame.display.update()
pygame.quit()
Upvotes: 0
Reputation: 16711
If the surface of the screen created with pygame.display.set_mode
is surface
then you can do this:
color = surface.get_at(pygame.mouse.get_pos()) # get the color of pixel at mouse position
Upvotes: 4