python - 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)
解决方案
你有更新显示的主循环吗?(问,因为我在你的代码中看不到它)像这样:
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()
所做的。
推荐阅读
- tensorflow-lite - 如何在 Mobile Net 中对量化的 TensorFlow lite 模型使用批量标准化?
- character-encoding - 如何在使用 Azure 复制活动将数据从 CSV 文件复制到镶木地板时处理数据编码问题?
- reactjs - 接口的辅助函数
- java - AndroidR, isDhcpLeaseCacheEnabled 总是返回 true。如何禁用它?
- google-play - Googleplay purchase.products.acknowledge 返回 400 not a valid state 和 409 cocurrentUpdate
- excel - 如果单元格为空,则用公式填充单元格
- python - 在 Python 的箱线图中仅显示平均 SEM 和 95% CI
- django - Fargate oneoff 任务继续运行
- syntax - Slurm作业名的语法?
- gcloud - gcloud 组件安装错误,Windows 10 WSL 1 上的权限被拒绝,即使在使用 sudo 权限执行后也是如此