Python 异常:错误:尝试绘制没有纹理集的精灵
所以我基本上是在使用python和arcade库开发一个自顶向下的2d游戏,我对它还不熟悉。 下面是我的角色的简单空闲动画代码。 我不太确定,但此时我得到了错误Python 异常:错误:尝试绘制没有纹理集的精灵,python,compiler-errors,pycharm,python-3.9,arcade,Python,Compiler Errors,Pycharm,Python 3.9,Arcade,所以我基本上是在使用python和arcade库开发一个自顶向下的2d游戏,我对它还不熟悉。 下面是我的角色的简单空闲动画代码。 我不太确定,但此时我得到了错误 # load texture for ideal standing self.idle_texture = [] for i in range(4): texture = load_texture(f"{main_path}/idle_anim_{i}.png")
# 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, key, 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()
def更新动画(自身,增量时间:浮点=1/60):
#弄清楚我们是需要面对左边还是右边
如果self.change\u x<0且self.character\u face\u direction==右向:
self.character\u face\u direction=左向
elif self.change\u x>0且self.character\u face\u direction==左向:
self.character\u face\u direction=右侧
#空闲动画
如果self.change_x==1和self.change_y==0:
self.texture=self.idle\u纹理[self.character\u face\u方向]
返回
类MyGame(街机窗口):
“”“主应用程序类。”“”
定义初始化(自):
#调用父类并设置窗口
超级()
#设置此程序开始的路径
file_path=os.path.dirname(os.path.abspath(uu file_uu))
chdir(文件路径)
#玩家精灵
self.player\u sprite=无
#我们需要的雪碧清单
self.player\u list=无
self.wall\u list=无
self.bullet\u list=无
self.item_list=无
self.background\u list=无
#我们的物理引擎
self.physics\u引擎=无
街机。设置背景颜色(arcade.csscolor.BLACK)
def设置(自):
“”“在此处设置游戏。调用此函数重新启动游戏。”“”
#创建精灵列表
self.player_list=arcade.SpriteList()
self.wall_list=arcade.SpriteList()
self.background_list=arcade.SpriteList()
#创建玩家精灵
self.player\u sprite=PlayerCharacter()
self.player\u sprite.center\u x=100
self.player\u sprite.center\u y=100
#添加到玩家到精灵列表
self.player\u list.append(self.player\u精灵)
self.physics\u engine=arcade.PhysicsEngineSimple(self.player\u精灵、self.wall\u列表)
def on_键按(自身、键、修改器):
“”“每当按键时调用。”“”
如果key==arcade.key.UP或key==arcade.key.W:
self.player\u sprite.change\u y=玩家\u移动\u速度
elif key==arcade.key.DOWN或key==arcade.key.S:
self.player\u sprite.change\u y=-player\u MOVEMENT\u SPEED
elif key==arcade.key.LEFT或key==arcade.key.A:
self.player\u sprite.change\u x=-player\u MOVEMENT\u SPEED
elif key==arcade.key.RIGHT或key==arcade.key.D:
self.player\u sprite.change\u x=玩家\u移动\u速度
钥匙释放时的def(自身、钥匙、修改器):
“”“释放密钥时调用”“”
如果key==arcade.key.UP或key==arcade.key.W:
self.player\u sprite.change\u y=0
elif key==arcade.key.DOWN或key==arcade.key.S:
self.player\u sprite.change\u y=0
elif key==arcade.key.LEFT或key==arcade.key.A:
self.player\u sprite.change\u x=0
elif key==arcade.key.RIGHT或key==arcade.key.D:
self.player\u sprite.change\u x=0
def on_更新(自身、增量时间):
“”“运动和游戏逻辑。”“”
#用物理引擎移动玩家
self.physics_engine.update()
#移动玩家
self.player_list.update()
#更新播放器动画
self.player\u list.update\u animation()
def on_牵引(自):
“”“渲染屏幕。”“”
arcade.start_render()
self.player_list.draw()
self.wall_list.draw()
self.background_list.draw()
#绘制屏幕的代码如下所示
def main():
“”“主方法。”“”
window=MyGame()
window.setup()
arcade.run()
如果名称=“\uuuuu main\uuuuuuuu”:
main()
我的问题是,每当我执行此代码时,它都会为我的角色空闲动画显示“异常:错误:尝试绘制没有纹理集的精灵”。
或者它将显示文件未找到错误的错误。
如果有人能帮助我,我将处理这个问题两天。这通常发生在纹理加载失败时。当找不到纹理时,较新的arcade版本现在将崩溃并出现相应的错误。少点混乱!
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, key, 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()