pygame.transform.rotate()问题,索引显然超出了范围

时间:2018-05-22 21:56:55

标签: python-3.x pygame sprite pygame-surface

所以我正在开发一款基本的PacMan游戏,并试图让我的播放器运行其sprite_sheet来显示动画。当我向右移动时(这就是图像的原始方向),它可以完美地工作,但是对于我在pygame.transform.rotate()图像中的任何其他方向,它返回以下内容:  self.image =(PAC_UP [self.spritesheet_index%4]) IndexError:列表索引超出范围

不确定为什么会发生这种情况(显然它与旋转有关)。关于轮换的任何解释以及为什么会发生这种情况将不胜感激!

谢谢! 继承我的代码:

class sprite_sheet():
    def __init__ (self, filename):
        self.spritesheet = pygame.image.load(filename)

    def get_image(self, x, y, width, height):
        image = pygame.Surface((width, height))
        image.set_colorkey(BLACK)
        image.blit(self.spritesheet, (0,0), (x, y, width, height))
        return image


PAC_RIGHT = []
for x in range(4):
    image = (sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24))
    PAC_RIGHT.append(image)


PAC_UP = []
for x in range(4):
    image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
    PAC_RIGHT.append(pygame.transform.rotate(image, 90))

PAC_LEFT = []
for x in range(4):
    image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
    PAC_RIGHT.append(pygame.transform.rotate(image, 180))

PAC_DOWN = []
for x in range(4):
    image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
    PAC_RIGHT.append(pygame.transform.rotate(image, 270))





class Player (pygame.sprite.Sprite):

    def __init__(self, game, x, y):
        self.groups = game.all_sprites

        pygame.sprite.Sprite.__init__(self, self.groups)

        self.game = game
        self.image = sprite_sheet('pacman-spritesheet.png').get_image(0, 0, 24, 24)

        self.x = x * TILESIZE
        self.y = y * TILESIZE


        self.rect = self.image.get_rect()

        self.vx, self.vy = 0, 0

        self.spritesheet_index = 0

        self.first_time = pygame.time.get_ticks()
        self.delay = 43




    def get_keys(self):
        self.vx, self.vy = 0, 0
        keys = pygame.key.get_pressed()

        self.spritesheet_index += 1



        if keys[pygame.K_LEFT]:
            self.vx = -PLAYER_SPEED
            self.second_time = pygame.time.get_ticks()

            if self.second_time-self.first_time > self.delay:
                self.image = (PAC_LEFT[self.spritesheet_index % 4])
                self.first_time = pygame.time.get_ticks()

        if keys[pygame.K_RIGHT]:
            self.vx = PLAYER_SPEED
            self.second_time = pygame.time.get_ticks()

            if self.second_time-self.first_time > self.delay:
                self.image = (PAC_RIGHT[self.spritesheet_index % 4])
                self.first_time = pygame.time.get_ticks()

        if keys[pygame.K_UP]:
            self.vy = -PLAYER_SPEED
            self.second_time = pygame.time.get_ticks()

            if self.second_time-self.first_time > self.delay:

                self.image = (PAC_UP[self.spritesheet_index % 4])
                self.first_time = pygame.time.get_ticks()

        if keys[pygame.K_DOWN]:
            self.vy = PLAYER_SPEED
            self.second_time = pygame.time.get_ticks()

            if self.second_time - self.first_time > self.delay:
                self.image = (PAC_DOWN[self.spritesheet_index % 4])
                self.first_time = pygame.time.get_ticks()

1 个答案:

答案 0 :(得分:1)

您似乎只是将精灵图像添加到PAC_UP列表中:

PAC_RIGHT = []
for x in range(4):
    image = (sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24))
    PAC_RIGHT.append(image)


PAC_UP = []
for x in range(4):
    image = sprite_sheet('pacman-spritesheet.png').get_image(x*24, 0, 24, 24)
    PAC_RIGHT.append(pygame.transform.rotate(image, 90))

修改您的代码以附加到相应的列表PAC_DOWNPAC_LEFT& PAC_RIGHT

此外,这看起来是一个复制粘贴错误。而不是复制粘贴,最好创建一个函数来进行图像加载。

这是一个未经测试的例子:

def load_sprite_images(rotation, filename='pacman-spritesheet.png'):
    """Load four sprite images from the specified file"""
    images = []
    for x in range(4):
        image = (sprite_sheet(filename).get_image(x*24, 0, 24, 24))
        images.append(pygame.transform.rotate(image, rotation))
    return images

PAC_RIGHT = load_sprite_images(0)
PAC_UP = load_sprite_images(90)
PAC_LEFT = load_sprite_images(180)
PAC_RIGHT = load_sprite_images(270)