[英]list append not working (list always empty)
I'm making a game and I created a loop in a class that creates a list with every sprite I need from my sprite sheet.我正在制作游戏,并在 class 中创建了一个循环,该循环创建了一个列表,其中包含我需要的精灵表中的每个精灵。 The problem is that when I try to blit an element of the list I always get the error: IndexError: list index out of range Here's the code of the function in the class (simplified):
问题是,当我尝试 blit 列表的一个元素时,我总是得到错误: IndexError: list index out of range 下面是 class 中 function 的代码(简化):
def sprites(self):
i = 1
boxwidth = 0
boxwidthend = False
sprites = []
for i in range(1,0,5):
while i <= 2:
boxheight = 0
spritesurface = pygame.Surface((self.width, self.heightside), pygame.SRCALPHA)
spritesurface.blit(self.type, (0,0), (0 + boxwidth, 0, self.width, self.heightside))
sprite = pygame.transform.scale(spritesurface, (self.width * self.scale, self.heightside * self.scale))
sprites.append(sprite)
while i <= 5:
boxheight = 49
if boxwidthend == False:
boxwidth = 0
boxwidthend = True
spritesurface = pygame.Surface((self.width, self.heightup), pygame.SRCALPHA)
spritesurface.blit(self.type, (0,0), (0 + boxwidth, boxheight, self.width, self.heightup))
sprite = pygame.transform.scale(spritesurface, (self.width * self.scale, self.heightup * self.scale))
sprites.append(sprite)
boxwidth += 25
i += 1
return sprites
and here's the main code section (simplified):这是主要代码部分(已简化):
sprites_class = GetSprites("human", 3)
spritelist = sprites_class.sprites()
while True:
ROOT.blit(spritelist[3], (playermovement.x, playermovement.y))
Summarizing the comments:总结评论:
Range takes three parameters范围需要三个参数
So if you want to loop numbers 1
through 5
, all you need is Range(6)
as the optional start
parameter defaults to 0
and the optional step
parameter defaults to 1
.所以如果你想循环数字
1
到5
,你只需要Range(6)
因为可选的start
参数默认为0
,可选的step
参数默认为1
。
while i<=2:
will result in an infinite loop as there is no way for the value of i
to change inside the loop. while i<=2:
将导致无限循环,因为i
的值无法在循环内更改。 Once you enter it, it will go on forever.一旦你输入它,它将永远显示 go。 Instead you want an
if
and elif
相反,你想要一个
if
和elif
def sprites(self):
boxwidth = 0
boxwidthend = False
sprites = []
for i in range(6):
if i <= 2:
print("i <= 2")
boxwidth += 1
boxheight = 0
spritesurface = pygame.Surface((self.width, self.heightside), pygame.SRCALPHA)
spritesurface.blit(self.type, (0,0), (0 + 25 * boxwidth, 0, self.width, self.heightside))
sprite = pygame.transform.scale(spritesurface, (self.width * self.scale, self.heightup * self.scale))
sprites.append(sprite)
elif i <= 5:
print("i > 2")
boxheight = 48
if boxwidthend == False:
boxwidth = 0
boxwidthend = True
spritesurface = pygame.Surface((self.width, self.heightup), pygame.SRCALPHA)
spritesurface.blit(self.type, (0,0), (0 + 25 * boxwidth, boxheight, self.width, self.heightup))
sprite = pygame.transform.scale(spritesurface, (self.width * self.scale, self.heightup * self.scale))
sprites.append(sprite)
return sprites
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.