Python Pygame获取光标所在像素的颜色
如何在pygame中直接获得指针下像素的颜色Python Pygame获取光标所在像素的颜色,python,python-2.7,pygame,Python,Python 2.7,Pygame,如何在pygame中直接获得指针下像素的颜色 我做了很多研究,但答案很难回答。如果使用pygame.display.set_mode创建的屏幕表面是surface,则可以执行以下操作: color = surface.get_at(pygame.mouse.get_pos()) # get the color of pixel at mouse position 如果使用pygame.display.set_mode创建的屏幕表面为surface,则可以执行以下操作: color = surf
我做了很多研究,但答案很难回答。如果使用
pygame.display.set_mode
创建的屏幕表面是surface
,则可以执行以下操作:
color = surface.get_at(pygame.mouse.get_pos()) # get the color of pixel at mouse position
如果使用
pygame.display.set_mode
创建的屏幕表面为surface
,则可以执行以下操作:
color = surface.get_at(pygame.mouse.get_pos()) # get the color of pixel at mouse position
@马利克的回答非常正确。下面是一个工作演示:
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()
@马利克的回答非常正确。下面是一个工作演示:
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()