简体   繁体   English

列表 append 不工作(列表始终为空)

[英]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:总结评论:

  1. Range takes three parameters范围需要三个参数

    • start Optional.开始可选。 An integer number specifying at which position to start.一个 integer 数字,指定从哪个 position 开始。 Default is 0默认为 0
    • stop Required.停止必填。 An integer number specifying at which position to stop (not included).一个 integer 号码指定 position 停止(不包括在内)。
    • step Optional.步骤可选。 An integer number specifying the incrementation.指定增量的 integer 数字。 Default is 1默认为 1

    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 .所以如果你想循环数字15 ,你只需要Range(6)因为可选的start参数默认为0 ,可选的step参数默认为1

  2. 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相反,你想要一个ifelif


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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM