gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:49 26 4
gpt4 key购买 nike

我是 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()

关于python - pygame.error : file not a windows. bmp 文件(看过其他类似的问题,但到目前为止没有成功),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22159456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com