异常:错误:尝试绘制没有纹理集的精灵

问题描述

因此,基本上,我正在使用python和arcade库开发自上而下的2d游戏,而我对此还是陌生的。 下面给出的是我为角色制作的简单闲置动画的代码

import arcade
import os


SCREEN_TITLE = "shooting_game"

SPRITE_IMAGE_SIZE = 32

SPRITE_SCALING_PLAYER = 1
SPRITE_SCALING_TILES = 2

SPRITE_SIZE = int(SPRITE_IMAGE_SIZE * SPRITE_SCALING_PLAYER)

SCREEN_GRID_WIDTH = 15
SCREEN_GRID_HEIGHT = 7

SCREEN_WIDTH = SPRITE_SIZE * SCREEN_GRID_WIDTH
SCREEN_HEIGHT = SPRITE_SIZE * SCREEN_GRID_HEIGHT


PLAYER_MOVEMENT_SPEED = 3


RIGHT_FACING = 0
LEFT_FACING = 1


def loadtexture(filename):

return [
    arcade.load_texture(filename),arcade.load_texture(filename,flipped_horizontally=True)
]


class PlayerCharacter(arcade.Sprite):
""" Player sprite """
def __init__(self):

    # set up parent class
    super().__init__()

    # Default to face right
    self.character_face_direction = RIGHT_FACING

    # Used for filliping between image sequence
    self.cur_texture = 0
    self.scale = SPRITE_SCALING_PLAYER

    # ____Load texture_______
    main_path = "maps/knight/idle"

我不太确定,但是此时我遇到了错误

    # load texture for ideal standing
    self.idle_texture = []
    for i in range(4):
        texture = load_texture(f"{main_path}/idle_anim_{i}.png")
        self.idle_texture.append(texture)

在此上方代码

我的角色也没有翻转

def update_animation(self,delta_time: float = 1/60):
    # figure out if we need to face left or right
    if self.change_x < 0 and self.character_face_direction == RIGHT_FACING:
        self.character_face_direction = LEFT_FACING
    elif self.change_x > 0 and self.character_face_direction == LEFT_FACING:
        self.character_face_direction = RIGHT_FACING

    # idle animation
    if self.change_x == 1 and self.change_y == 0:
        self.texture = self.idle_texture[self.character_face_direction]
        return


class MyGame(arcade.Window):
""" Main application class. """
 
def __init__(self):
    # call the parent class and set up the window
    super().__init__(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_TITLE)

    # Set the path to start with this program
    file_path = os.path.dirname(os.path.abspath(__file__))
    os.chdir(file_path)
    # Player sprite
    self.player_sprite = None

    # sprites list we need
    self.player_list = None
    self.wall_list = None
    self.bullet_list = None
    self.item_list = None
    self.background_list = None

    # OUR physics engine
    self.physics_engine = None

    arcade.set_background_color(arcade.csscolor.BLACK)

def setup(self):
    """ Set up the game here. Call this function to restart the game. """

    # Create the sprite list
    self.player_list = arcade.SpriteList()
    self.wall_list = arcade.SpriteList()
    self.background_list = arcade.SpriteList()

    # creating player sprite
    self.player_sprite = PlayerCharacter()

    self.player_sprite.center_x = 100
    self.player_sprite.center_y = 100
    # add to player to sprite list
    self.player_list.append(self.player_sprite)

    self.physics_engine = arcade.PhysicsEnginesimple(self.player_sprite,self.wall_list)

def on_key_press(self,key,modifiers):
    """ Called whenever a key is pressed. """

    if key == arcade.key.UP or key == arcade.key.W:
        self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
    elif key == arcade.key.DOWN or key == arcade.key.S:
        self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
    elif key == arcade.key.LEFT or key == arcade.key.A:
        self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
    elif key == arcade.key.RIGHT or key == arcade.key.D:
        self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED

def on_key_release(self,modifiers):
    """ Called when the key is released """
    if key == arcade.key.UP or key == arcade.key.W:
        self.player_sprite.change_y = 0
    elif key == arcade.key.DOWN or key == arcade.key.S:
        self.player_sprite.change_y = 0
    elif key == arcade.key.LEFT or key == arcade.key.A:
        self.player_sprite.change_x = 0
    elif key == arcade.key.RIGHT or key == arcade.key.D:
        self.player_sprite.change_x = 0

def on_update(self,delta_time):
    """ Movement and game logic. """

    # Move the player with the physics engine
    self.physics_engine.update()

    # Move the player
    self.player_list.update()

    # update the player animation
    self.player_list.update_animation()

def on_draw(self):
    """ Render the screen. """

    arcade.start_render()
    self.player_list.draw()
    self.wall_list.draw()
    self.background_list.draw()
    # Code to draw the screen goes here


def main():
    """ Main method. """
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
main()

我的问题是,当我执行此代码时,它将为我的角色空闲动画显示“异常:错误:尝试绘制没有纹理集的精灵”。 或将显示文件未找到错误错误。 请有人帮我解决这个问题,持续2天。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)