pygame.surfarray.array2d返回先前渲染的工件

时间:2018-10-10 04:12:09

标签: python pygame pygame-surface

我正在尝试获取表示pygame中显示内容的2d数组。但是,每当我调用CustomSerialzer方法然后调用pygame.display.update()时,我都会在数组中得到上一张图像的一部分。

即使在我获取数组之前经过一定的时间,也会发生这种情况。

游戏的实际显示不包含这些工件,并且显示正确。

我正在做的是 1.更新精灵的坐标 2.呼叫pygame.surfarray.array2d并等待用户输入 3.调用pygame.display.update()以将屏幕显示为数组 pygame.surfarray.array2d(self.display) 我确实尝试了pygame.display.get_surface(),但没有区别

以下是外观的示例:

enter image description here enter image description here

原始游戏没有伪像: enter image description here

这里发生了什么或如何调试的任何想法?

这是最小的可验证示例:

self.display = pygame.display.set_mode((self.width, self.height))

这是我的display_image:

import pygame
import display_image as image_test

render_img = image_test.Render()

width = 400
height = 400
IMAGE_SIZE = 120

display = pygame.display.set_mode((width, height))
robot_image = pygame.image.load('robot_blue.png')
robot_image = pygame.transform.scale(robot_image, (IMAGE_SIZE, IMAGE_SIZE))
floor_image = pygame.image.load('floor.png')
floor_image = pygame.transform.scale(floor_image, (IMAGE_SIZE, IMAGE_SIZE))
def wait():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                return

x_position = 0
y_position = 0
display.blit(floor_image, (x_position, y_position))
display.blit(robot_image, (x_position, y_position))
pygame.display.update()
wait()
image = pygame.surfarray.array2d(display)
render_img(image, 400, 400)
x_position = 0
y_position = 120
display.blit(floor_image, (0, 0))
display.blit(robot_image, (x_position, y_position))
pygame.display.update()
wait()
image = pygame.surfarray.array2d(display)
render_img(image, 400, 400)

0 个答案:

没有答案