我正在按照本指南尝试在 Pygame 窗口中显示基本的 PNG 图像。我的图像是一个简单的 150x150 绿色球,没有透明度,名为ball.png,位于与我的 Python 脚本相同的目录中:
然而,当我启动程序时,出现的只是一个大小和位置正确的有问题的图像:
我使用此代码来显示图像(与上面链接的教程中给出的代码相同):
import pygame
import os
_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((400, 300))
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((255, 255, 255))
screen.blit(get_image('ball.png'), (20, 20))
pygame.display.flip()
clock.tick(60)
Run Code Online (Sandbox Code Playgroud)
为什么会出现这种情况呢?代码有缺陷吗?我尝试用不同的 PNG 替换,但这只会改变有问题的图像的外观 - 图像仍然无法正确显示。我使用的是 OS X 10.11 El Capitan、Python 3.5.0 和 Pygame 1.9.2。
| 归档时间: |
|
| 查看次数: |
784 次 |
| 最近记录: |