【发布时间】:2019-07-21 05:24:26
【问题描述】:
我正在尝试将图像作为标题屏幕加载到我正在制作的游戏中,但我不断收到“无法在此数据源中查找”的错误消息,我不知道自己做错了什么。
我已将图像移动到我的项目文件夹中,但它似乎没有帮助。
import pygame
import time
import random
pygame.init()
(width, height) = (1366, 768)
black = (0, 0, 0)
white = (225, 225, 225)
red = (225, 0, 0)
background_color = white
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Battle of the Forgotten Friend')
screen.fill(background_color)
clock = pygame.time.Clock()
titlescreen = pygame.image.load('Images/titlescreen.png')
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.image.load(titlescreen)
screen.blit(titlescreen, (0, 0))
pygame.display.flip()
clock.tick(15)
Traceback (most recent call last): <Event(17-VideoExpose {})> File "C:/Users/Ian/PycharmProjects/Rpg/Levelandattack.py", line 128, in <module> game_intro() <Event(16-VideoResize {'size': (1366, 768), 'w': 1366, 'h': 768})> File "C:/Users/Ian/PycharmProjects/Rpg/Levelandattack.py", line 34, in game_intro pygame.image.load(titlescreen) <Event(1-ActiveEvent {'gain': 0, 'state': 1})> pygame.error: Can't seek in this data source
【问题讨论】:
-
首先使用
import os进行检查,然后使用print(os.path.exists('titlescreen.png'))。您的图像必须位于相对于该特定模块的路径中。如果您说包含代码的src文件夹和包含游戏资源的img文件夹,那么您必须将路径更改为'../img/titlescreen.png'。 -
我第一次看到此错误消息 - 显示完整消息。如果找不到图像,则显示
pygame.error: Couldn't open titlescreen.png(至少在 Linux 上) -
我尝试了这两种方法,完全被摧毁了。 print(os.path.exists('titlescreen.png')) 的输出结果为 true。我被难住了。