Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame.error:无法';t open image.bmp_Python_Pygame_Loading Image - Fatal编程技术网

Python pygame.error:无法';t open image.bmp

Python pygame.error:无法';t open image.bmp,python,pygame,loading-image,Python,Pygame,Loading Image,每当我运行此代码时,都会显示消息pygame.error:cannotopenship.bmp。我将ship.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) py

每当我运行此代码时,都会显示消息pygame.error:cannotopenship.bmp。我将ship.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()

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

是否在Unix操作系统中运行此代码?如果是这样,您需要更改工作目录以运行此file.py!在终端类型中,使用cd命令将文件保存到您的目录!然后用相同的路径运行python,就可以了

使用print os.getwd()作为脚本/py程序的第一行,查看实际从哪个文件夹开始。如果您的图像恰好位于主文件夹的子目录中(该子目录恰好不出现在os.getwd()的输出上),请确保将其包含在图像路径中,以便在加载图像时不只写入“ship.bmp”,您编写了“subdirectory/ship.bmp”作为您的路径。

您是否尝试使用绝对路径?您是否正在学习Eric Matthes的“Python速成班”?我猜你是在记事本++这样的编辑器中运行代码的吧@Finch下面的评论将帮助您发现工作目录,它可能是IDE的二进制路径。按照#kirill.z的建议从命令行运行它可以解决您的问题。