【发布时间】:2015-12-29 18:21:48
【问题描述】:
如何在pygame中直接获取指针下像素的颜色?
我做了很多研究,但答案很害羞。
【问题讨论】:
标签: python python-2.7 pygame
如何在pygame中直接获取指针下像素的颜色?
我做了很多研究,但答案很害羞。
【问题讨论】:
标签: python python-2.7 pygame
如果使用pygame.display.set_mode 创建的屏幕表面是surface,那么您可以这样做:
color = surface.get_at(pygame.mouse.get_pos()) # get the color of pixel at mouse position
【讨论】:
@Malik 的回答非常正确。这是一个工作演示:
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()
【讨论】: