【问题标题】:pygame.error: file not a windows.bmp file (have looked at other similar questions but unsuccessful so far)pygame.error: file not a windows.bmp file(已经查看了其他类似的问题,但到目前为止不成功)
【发布时间】:2014-03-03 23:05:36
【问题描述】:

我是 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.image.get_extended() 返回什么?
  • 这与其他问题有何不同?你试过哪些答案?
  • 啊,谢谢 Bartlomiej,我现在就看看这些,看看我能不能解决它:)
  • 这看起来像一个路径错误。你的目录结构是什么,你是如何运行你的代码的?您的代码需要一个从 .py 文件位置开始的子目录结构。

标签: python python-2.7 pygame


【解决方案1】:

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

我认为您必须如何布置以及您的代码在哪里查看的演示如下 - 在此示例中,您将在/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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 2022-12-02
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多