Skip to main content
 首页 » 编程设计

python之pygame.error : file not a windows. bmp 文件(看过其他类似的问题,但到目前为止没有成功)

2025年05月04日7zdz8207

我是 pygame 的新手,正在使用“使用 Python 和 Pygame 开始游戏开发”一书。我几乎已经输入了示例代码并保持相同 “pygame 错误:文件不是 windows.bmp 文件” 并希望能够像书中的示例那样加载 jpg/png。我很确定我在正确的目录中工作,我想使用的图像与示例中的格式相同。我也搜索了解决方案,但似乎没有一个答案对我有用。 书中的代码如下(我有 python 2.7.4、Ubuntu 13.04 和(我认为)pygame 1.2.15):

background_image_filename = 'sushiplate.jpg' 
mouse_image_filename = 'fugu.png' 
 
import pygame 
from pygame.locals import * 
from sys import exit 
 
pygame.init() 
 
screen = pygame.display.set_mode((640, 480), 0, 32) 
pygame.display.set_caption("Hello, World!") 
background = pygame.image.load(background_image_filename).convert() 
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha() 
 
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            exit() 
 
    screen.blit(background, (0,0)) 
    x, y = pygame.mouse.get_pos() 
    x-= mouse_cursor.get_width() / 2 
    y-= mouse_cursor.get_height() / 2 
    screen.blit(mouse_cursor, (x, y)) 
 
    pygame.display.update() 

到目前为止我的代码版本:

import os.path 
background = os.path.join('Documents/Python/Pygame','Background.jpg') 
cursor_image = os.path.join('Documents/Python/Pygame','mouse.png') 
 
import pygame 
from pygame.locals import * 
from sys import exit 
 
pygame.init() 
 
screen = pygame.display.set_mode((640, 480), 0, 32) 
pygame.display.set_caption("Hello, World!") 
background = pygame.image.load(background).convert() 
mouse_cursor = pygame.image.load(cursor_image).convert_alpha() 
while True: 
    for event in pygame.event.get(): 
            if event.type == QUIT: 
                    exit() 
    screen.blit(background, (0,0)) 
    x, y = pygame.mouse.get_pos() 
    x-= mouse_cursor.get_width() / 2 
    y-= mouse_cursor.get_height() / 2 
    screen.blit(mouse_cursor, (x, y)) 
    pygame.display.update() 

感谢您的帮助:)

请您参考如下方法:

我几乎可以保证您的路径是错误的。在您的代码中,您输入了相对路径,这意味着 pygame 正在工作目录(您执行代码的目录)的子文件夹中查找您的 Assets 。

下面是我认为您必须如何布局以及代码所在位置的演示 - 在此示例中,您将在 /home/your_username/Documents/my_games 中打开一个命令提示符。 (或 ~/Documents/my_games )并且您将运行 python your_game_script.py .

|---home 
   |---your_username 
       |---Documents 
           |---some_subfolder 
           |---my_games 
               |---your_game_script.py 
               |---Documents 
                   |---Python 
                       |---Pygame 
                           |---Background.jpg 
                           |---mouse.png 

这可行,但我怀疑您没有以这种方式设置文件夹,这就是它不起作用的原因。如果您运行交互式 python在与游戏脚本相同的文件夹中提示,请尝试以下操作:

import os 
os.path.isfile('Documents/Python/Pygame/Background.jpg')  
os.path.isfile('Documents/Python/Pygame/mouse.png')  

我怀疑两者的结果都是错误的 - 这意味着无法在该子文件夹位置找到文件。我建议您的游戏文件采用以下结构:

|---my_game 
    |---your_game_script.py 
    |---images 
        |---Background.jpg 
        |---mouse.png 

然后在your_game_script.py您可以通过以下方式加载文件:

background = 'images/Background.jpg' #relative path from current working dir 
cursor_image = 'images/mouse.png'    #relative path from current working dir 
 
import pygame 
from pygame.locals import * 
from sys import exit 
 
pygame.init() 
 
screen = pygame.display.set_mode((640, 480), 0, 32) 
pygame.display.set_caption("Hello, World!") 
background = pygame.image.load(background).convert() 
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()