我想得到一个由pygame显示中每个像素的RGBA代码组成的数组
我尝试过:
for i in range(SCREEN_WIDTH):
for j in range(SCREEN_HEIGHT):
Pixels.append(pygame.Surface.get_at((i, j)))
但是我收到一条错误消息,Surface.get_at对元组不起作用,所以我拆下了一组括号,然后它告诉我Surface.get_at对整数不起作用,所以我很困惑,如何获得所有像素的RGBA值?谢谢
编辑,好的,发表评论后,我发布了完整的可运行代码:
import pygame
pygame.init()
PPM = 15
SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
pos_X = SCREEN_WIDTH/PPM/3
pos_Y = SCREEN_HEIGHT/PPM/3
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
FPS = 24
TIME_STEP = 1.0 / FPS
running = True
lead_x = pos_X*PPM
lead_y = pos_Y*PPM
k = 0
Pixels = []
while running:
screen.fill((255, 255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == K_ESCAPE:
running = False
if k == 0:
for i in range(SCREEN_WIDTH):
for j in range(SCREEN_HEIGHT):
Pixels.append(pygame.Surface.get_at((i, j)))
k +=1
pygame.draw.rect(screen, (128,128,128), [lead_x, lead_y,50,50])
pygame.display.update()
pygame.display.flip() # Update the full display Surface to the screen
pygame.time.Clock().tick(FPS)
pygame.quit()
我得到了这些确切的错误,仅此而已:
Exception has occurred: TypeError
descriptor 'get_at' for 'pygame.Surface' objects doesn't apply to 'tuple' object
答案 0 :(得分:3)
.get_at
是Method Objects的实例函数方法(请参见pygame.Surface
)。因此,必须在pygame.Surface
的实例上调用它。 screen
是表示窗口的Surface对象。因此必须是:
Pixels.append(pygame.Surface.get_at((i, j)))
Pixels.append(screen.get_at((i, j)))
分别
Pixels.append(pygame.Surface.get_at(screen, (i, j)))