我正在尝试将图像作为标题屏幕加载到正在制作的游戏中,但是我不断收到“无法在此数据源中搜索”的错误,我不知道自己在做什么错。

我已将图像移到项目文件夹中,但似乎无济于事。

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

最佳答案

删除行

pygame.image.load(titlescreen).

该图像已被加载


  

titlescreen = pygame.image.load('Images/titlescreen.png')



无论如何,pygame.image.load的参数必须是字符串,名称是图像文件的路径。引起该错误的原因是,您已将pygame.Surface对象传递给该函数。

关于python - 找不到此数据源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57130570/

10-12 00:53