Pygame动画列表索引不断超出范围

问题描述 投票:0回答:2

我按照清晰的代码教程制作了一个平台游戏并最终完成了它,但是有一件事总是不断搞砸。这就是动画,有时游戏不会运行,只能在调试模式下运行,因为动画列表超出索引,这对我来说没有意义,因为列表中的每个项目都被考虑在内。下面是我的player.py,它具有视频提到的所有功能。如果需要更多信息,我将很乐意提供。 (动画部分在 self.image = self.animations['idle'][self.frame_index] 处特别出错)

import pygame 
from support import import_folder

class Player(pygame.sprite.Sprite):
    def __init__(self,pos,surface,create_jump_particles):
        super().__init__()
        self.import_character_assets()
        self.frame_index = 0
        self.animation_speed = 0.15
        self.image = self.animations['idle'][self.frame_index]
        self.rect = self.image.get_rect(topleft = pos)
        
        # dust particles 
        self.import_dust_run_particles()
        self.dust_frame_index = 0
        self.dust_animation_speed = 0.15
        self.display_surface = surface
        self.create_jump_particles = create_jump_particles

        # player movement
        self.direction = pygame.math.Vector2(0,0)
        self.speed = 8
        self.gravity = 0.8
        self.jump_speed = -16

        # player status
        self.status = 'idle'
        self.facing_right = True
        self.on_ground = False
        self.on_ceiling = False
        self.on_left = False
        self.on_right = False

    def import_character_assets(self):
        character_path = '../graphics/character/'
        self.animations = {'idle':[],'run':[],'jump':[],'fall':[]}

        for animation in self.animations.keys():
            full_path = character_path + animation
            self.animations[animation] = import_folder(full_path)

    def import_dust_run_particles(self):
        self.dust_run_particles = import_folder('../graphics/character/dust_particles/run')

    def animate(self):
        animation = self.animations[self.status]

        # loop over frame index 
        self.frame_index += self.animation_speed
        if self.frame_index >= len(animation):
            self.frame_index = 0

        image = animation[int(self.frame_index)]
        if self.facing_right:
            self.image = image
        else:
            flipped_image = pygame.transform.flip(image,True,False)
            self.image = flipped_image

        # set the rect
        if self.on_ground and self.on_right:
            self.rect = self.image.get_rect(bottomright = self.rect.bottomright)
        elif self.on_ground and self.on_left:
            self.rect = self.image.get_rect(bottomleft = self.rect.bottomleft)
        elif self.on_ground:
            self.rect = self.image.get_rect(midbottom = self.rect.midbottom)
        elif self.on_ceiling and self.on_right:
            self.rect = self.image.get_rect(topright = self.rect.topright)
        elif self.on_ceiling and self.on_left:
            self.rect = self.image.get_rect(topleft = self.rect.topleft)
        elif self.on_ceiling:
            self.rect = self.image.get_rect(midtop = self.rect.midtop)

    def run_dust_animation(self):
        if self.status == 'run' and self.on_ground:
            self.dust_frame_index += self.dust_animation_speed
            if self.dust_frame_index >= len(self.dust_run_particles):
                self.dust_frame_index = 0

            dust_particle = self.dust_run_particles[int(self.dust_frame_index)]

            if self.facing_right:
                pos = self.rect.bottomleft - pygame.math.Vector2(6,10)
                self.display_surface.blit(dust_particle,pos)
            else:
                pos = self.rect.bottomright - pygame.math.Vector2(6,10)
                flipped_dust_particle = pygame.transform.flip(dust_particle,True,False)
                self.display_surface.blit(flipped_dust_particle,pos)

    def get_input(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_RIGHT]:
            self.direction.x = 1
            self.facing_right = True
        elif keys[pygame.K_LEFT]:
            self.direction.x = -1
            self.facing_right = False
        else:
            self.direction.x = 0

        if keys[pygame.K_SPACE] and self.on_ground:
            self.jump()
            self.create_jump_particles(self.rect.midbottom)

    def get_status(self):
        if self.direction.y < 0:
            self.status = 'jump'
        elif self.direction.y > 1:
            self.status = 'fall'
        else:
            if self.direction.x != 0:
                self.status = 'run'
            else:
                self.status = 'idle'

    def apply_gravity(self):
        self.direction.y += self.gravity
        self.rect.y += self.direction.y

    def jump(self):
        self.direction.y = self.jump_speed

    def update(self):
        self.get_input()
        self.get_status()
        self.animate()
        self.run_dust_animation()
        

我已经尝试了一切,我什至去了他的网站并下载了他的原型并尝试向其中添加我自己的文件,但它仍然返回相同的错误。应该发生的事情是我的角色根据他们所处的状态执行动画。

python python-3.x animation pygame
2个回答
0
投票

因此,代码很可能没有加载“空闲”角色动画的任何动画帧。

初始化代码首先加载所有资源:

class Player(pygame.sprite.Sprite):
    def __init__(self,pos,surface,create_jump_particles):
        super().__init__()
        self.import_character_assets()

哪里

self.import_character_assets()
是:

def import_character_assets(self):
    character_path = '../graphics/character/'
    self.animations = {'idle':[],'run':[],'jump':[],'fall':[]}

    for animation in self.animations.keys():
        full_path = character_path + animation
        self.animations[animation] = import_folder(full_path)

迭代“idle”、“run”、“jump”、“fall”,将“../graphics/character/idle”、“../graphics/character/run”等中的图像加载到列表词典。

我们没有

import_folder()
的代码,但它显然是为了加载一些图像集。

导入后,

self.animations
字典预计
self.animations['idle']
中至少有一个图像。这是因为
__init__()
默认为“空闲”,并且索引
0
:

    self.frame_index = 0
    self.animation_speed = 0.15
    self.image = self.animations['idle'][self.frame_index]  # <<-- HERE, 0

因此我们可以得出结论,因为我们知道,

self.animations['idle']
超出了范围,即使在
[0]
上,也没有加载任何图像。

检查是否有可用的图像。或者文件名可能不是预期的(例如:.jpg,不是 .png),或者其他一些文件系统错误(例如:只读、零字节文件……)。也许加载路径不正确?

检查负载并写入错误可能是个好主意:

def import_character_assets(self):
    character_path = '../graphics/character/'
    self.animations = {'idle':[],'run':[],'jump':[],'fall':[]}

    for animation in self.animations.keys():
        full_path = character_path + animation
        self.animations[animation] = import_folder(full_path)
        # check we loaded something
        if ( len( self.animations[animation] ) == 0 ):
            print( "Failed to load any animations for \"" + animation + "\" type" )

0
投票

您应该在此处输入完整路径而不是“..”:

字符路径 = '../图形/字符/'

© www.soinside.com 2019 - 2024. All rights reserved.