【问题标题】:Pygame get color of pixel where cursor is locatedPygame获取光标所在像素的颜色
【发布时间】:2015-12-29 18:21:48
【问题描述】:

如何在pygame中直接获取指针下像素的颜色?

我做了很多研究,但答案很害羞。

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    如果使用pygame.display.set_mode 创建的屏幕表面是surface,那么您可以这样做:

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

    【讨论】:

      【解决方案2】:

      @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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多