【发布时间】: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