【问题标题】:descriptor 'get_at' for 'pygame.Surface' objects doesn't apply to a 'tuple' object“pygame.Surface”对象的描述符“get_at”不适用于“元组”对象
【发布时间】:2020-07-19 01:17:38
【问题描述】:

我正在创建一个程序,如果光标所在位置的像素不是黑色,我正在尝试在光标所在位置绘制一个矩形。

if pygame.Surface.get_at(pygame.mouse.get_pos()) != (0,0,0,255):
    pygame.draw.rect(win, (0,0,0), (x, y, 3, 3))

当我尝试实现显示 ... 的 pygame.Surface.get_at() 时发生错误

TypeError: descriptor 'get_at' for 'pygame.Surface' objects doesn't apply to a 'tuple' object

.. 即使https://pygame.org 上 pygame.Surface.get_at() 的文档显示该方法的输入应该是一个元组。

get_at()
    get the color value at a single pixel
    get_at((x, y)) -> Color

    Return a copy of the RGBA Color value at the given pixel. If the Surface has no per pixel alpha, then 
    the alpha value will always be 255 (opaque). If the pixel position is outside the area of the Surface 
    an IndexError exception will be raised.

我该如何解决这个问题?

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    您需要在 pygame.Surface 对象上调用 get_at,而不是类本身。

    
    screen = pygame.display.set_mode(size)
    
    pixel = screen.get_at(pygame.mouse.get_pos()) #gets the pixel on the screen surface
    
    another_surface = pygame.Surface((100, 100))
    
    another_pixel = another_surface.get_at((50, 50)) #gets the pixel from the 'another_surface' surface
    
    

    【讨论】:

    • 我是否绝对必须创建一个表面和一个屏幕并在它们两者上放置矩形才能检查某个像素?因为那时我必须将表面上发生的一切复制到屏幕上,感觉效率低下。
    • @notgriffin 不,你不需要两者,只需要一个或另一个。他们是两个不同的例子。因为屏幕是一个表面,如果你有一个表面,就用那个
    猜你喜欢
    • 2020-08-12
    • 2021-09-14
    • 2020-03-18
    • 2022-01-21
    • 2020-11-01
    • 2021-10-26
    • 2022-11-26
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多