pygame.error.错误:无法打开图像.bmp

2024-04-19 18:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我运行这个代码pygame.error.错误:无法打开船.bmp出现。我在保管档案船.bmp和我的代码在同一个地方。我会很感激你的帮助。在

import sys
import pygame

pygame.init()

screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
bg_color = (0, 13, 114)
pygame.display.set_caption('space invaders')

shipImg = pygame.image.load("ship.bmp")

def ship(x,y):
    screen.blit(shipImg, (x,y))

x = (screen_width * 0.45)
y = (screen_height * 0.8)

crashed = False
while not crashed:

    #keyboard events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    screen.fill(bg_color)
    ship(x,y)
    pygame.display.flip()
pygame.quit()
quit()

Tags: 代码importeventdisplaywidthscreenpygamecolor
3条回答

您是否在Unix操作系统中运行此代码?如果是这样的话,你需要更改你的工作目录来运行这个文件.py! 在终端中用cd命令输入保存文件的目录!然后用相同的路径运行python,这样就可以正常工作了!在

我试过你的代码,效果很好。请确保映像位于根目录中。这是保存脚本/py文件的同一个文件夹。另一个原因可能是文件名中有隐藏字符。这种事在我身上发生过好几次。尝试rename函数,删除包括文件扩展名在内的整个名称,并使用相同的ext重命名文件,全新的。在

使用打印操作系统.getwd()作为脚本/py程序的第一行,以查看实际从哪个文件夹启动。如果您的图像恰好位于主文件夹的子目录中(它碰巧没有出现在操作系统.getwd()),请确保将其包含在图像路径中,而不是只写'船.bmp'加载图像时,写入'子目录/船.bmp“作为你的道路。在

相关问题 更多 >