如何在pygame中居中图像?

0 python pygame

我正在使用 python 2.7,我想知道是否有办法在 pygame 中居中图像。我已将屏幕设置为全屏,这可能吗?如果有帮助我会附上代码,谢谢。导入 pygame 导入 o​​s

    _image_library = {}
    def get_image(path):
            global _image_library
            image = _image_library.get(path)
            if image == None:
                    canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
                    image = pygame.image.load(canonicalized_path)
                    _image_library[path] = image
            return image

    pygame.init()
    screen = pygame.display.set_mode((0, 0))
    done = False
    clock = pygame.time.Clock()

    while not done:
            for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                            done = True

            screen.fill((0, 0, 0))

            screen.blit(get_image('sw.png'), (0, 0)

            pygame.display.flip()
            clock.tick(60)
Run Code Online (Sandbox Code Playgroud)

Gio*_*o B 5

是的,这是可能的。您必须创建图像的 rect 对象:

image = "insert image"
rect = image.get_rect ()
Run Code Online (Sandbox Code Playgroud)

进而:

rect.center = ("coordinates of center")
Run Code Online (Sandbox Code Playgroud)