首页 > 解决方案 > Pygame 窗口出现,但没有图像显示

问题描述

我正在关注一本书教程,该教程最初告诉我使用 .bmp 作为图片,因为这是 pygame 识别并可以打开的默认图像文件类型,但它不起作用。我收到一个 pygame.error 说它无法打开它,我怀疑是因为我使用的是 macOS。所以我尝试使用.png,但这也不起作用。

导入pygame

类船():

def __init__(self, screen):
    """Initialize the ship and set its starting position."""
    self.screen = screen

    # Load the ship image and get its rect.
    self.image = pygame.image.load('images/ship.png')
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()

    # Start each new ship at the bottom center of the screen.
    self.rect.centerx = self.screen_rect.centerx
    self.rect.bottom = self.screen_rect.bottom

def blitme(self):
    """Draw the ship at its current location."""
    self.screen.blit(self.image, self.rect)

标签: pythonpygame

解决方案


你有更新显示的主循环吗?(问,因为我在你的代码中看不到它)像这样:

done=False
while done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    done=True
    pygame.display.flip() #Update the screen

为了显示图像,您需要更新窗口,这正是pygame.display.flip()所做的。


推荐阅读