简体   繁体   English

pygame获取光标所在像素的颜色

[英]Pygame get color of pixel where cursor is located

How can I get the color of the pixel directly under the pointer in pygame? 如何在pygame中的指针正下方获得像素的颜色?

I have done a lot of research, but the answer is rather shy. 我做了很多研究,但是答案很害羞。

如果使用pygame.display.set_mode创建的屏幕surfacesurface则可以执行以下操作:

color = surface.get_at(pygame.mouse.get_pos()) # get the color of pixel at mouse position

@Malik's answer is very correct. @Malik的答案非常正确。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM