Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame获取光标所在像素的颜色_Python_Python 2.7_Pygame - Fatal编程技术网

Python 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中直接获得指针下像素的颜色


我做了很多研究,但答案很难回答。

如果使用
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()