Pygame:无法在此数据源中查找 [英] Pygame: Can't seek in this data source
问题描述
Traceback (most recent call last):
File "G:/Computing/Project/Main.py", line 32, in <module>
ClubsImages, SpadesImages, HeartsImages, DiamondsImages = LoadImages()
File "G:/Computing/Project/Main.py", line 18, in LoadImages
ClubsImages[i] = pygame.image.load(("Images/",x,".png"))
pygame.error: Can't seek in this data source
运行此代码时出现此错误:
I get this error when I run this code:
def LoadImages():
SpadesImages = {}
ClubsImages = {}
HeartsImages = {}
DiamondsImages = {}
x = 1
for i in range (13):
ClubsImages[i] = pygame.image.load(("Images/",x,".png"))
x+=1
SpadesImages[i] = pygame.image.load(("Images/",x,".png"))
x+=1
HeartsImages[i] = pygame.image.load(("Images/",x,".png"))
x+=1
DiamondsImages[i] = pygame.image.load(("Images/",x,".png"))
x+=1
return ClubsImages, SpadesImages, HeartsImages, DiamondsImages
我以前读过这样的文件,但这是我第一次尝试将其放入数组和循环中.问题不在于循环,所以我已经将其取出并尝试了,但得到了相同的错误.所以我认为这是数组.我看不出有什么理由不能将图像读入数组.
I have read files like this before, except this is the first time I have tried to do it into an array and in a loop. The problem is not the loop so I have taken that out and tried it and I get an identical error. So I am thinking it's the array. I can't see any reason that you couldn't read images into an array.
推荐答案
我很确定问题在于您将元组传递给 pygame.image.load
.根据该函数的文档,它可以采用文件名(作为字符串),或文件对象和可选的名称提示.仔细查看您的来电:
I'm pretty sure the problem is that you're passing a tuple to pygame.image.load
. According to the documentation for the function, it can either take a filename (as a string), or a file object and an optional name hint. Looking closely at your call:
pygame.image.load(("Images/", x, ".png"))
您正在传递 ("Images/", x, ".png")
,这是一个无法解释为文件路径的元组.尝试类似:
You are passing ("Images/", x, ".png")
, a tuple, which could not be interpreted as a filepath. Try something like:
pygame.image.load("Images/" + str(x) + ".png")
这篇关于Pygame:无法在此数据源中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!