首页 > 解决方案 > pygame 无法重新定位图像

问题描述

我有一个位于屏幕中心的图像列表。

def relocate_images(self, images):
    for image in images:
        print('the center is ' + str(image.get_rect().center))
        x_diff = int(self.width/2) - image.get_rect().x #self.width is the width of the screen
        y_diff = int(self.height/2) - image.get_rect().y #self.height is the height of the screen
        image.get_rect().move(x_diff, y_diff)
        print('the center is ' + str(image.get_rect().center))

屏幕上显示的中心是相同的。所以价值没有签署到图像?

标签: pythonimagepygame

解决方案


图像没有位置。该方法image.get_rect()返回一个与图像大小相同的矩形,但始终位于 (0, 0) 位置。您自己必须创建和管理所有图像的所有位置。我建议创建一个矩形列表,将其传递给该方法,并使用相同的矩形列表进行 blitting。或者,创建一个包含图像及其对应矩形的类。


推荐阅读