由于之前的问题,我设法解决了一个问题,但现在我偶然发现了一个新问题。事实是,我无法确定是错误,错误还是Python或Pyglet限制。 我作了一个前提,以这种方式构建类是因为我想使我的游戏引擎尽可能独立,以便主文件应该简单地初始化类。
from dict.entity_dict import player, player_class
from collections import OrderedDict
import pyglet
key = pyglet.window.key
class Player(pyglet.sprite.Sprite):
image = None
def __init__(self, game):
self.game = game
self.keyboard = key.KeyStateHandler()
self.statistics_base = OrderedDict()
self.previous_direction = None
self.dir_id = 3
self.load_resource()
self.vx = self.game.wd / 2
self.vy = self.game.wh / 2
self.load_sprite()
def load_resource(self):
# image_stand
image_stand = pyglet.resource.image(player.get("stand", {'x': None}).get("resource"))
image_stand_width = player.get("stand", {'x': None}).get("width")
image_stand_height = player.get("stand", {'x': None}).get("height")
image_stand_columns = player.get("stand", {'x': None}).get("columns")
image_stand_rows = player.get("stand", {'x': None}).get("rows")
image_stand_loop = player.get("stand", {'x': None}).get("loop")
# image_run
image_run = pyglet.resource.image(player.get("run", {'x': None}).get("resource"))
image_run_width = player.get("run", {'x': None}).get("width")
image_run_height = player.get("run", {'x': None}).get("height")
image_run_columns = player.get("run", {'x': None}).get("columns")
image_run_rows = player.get("run", {'x': None}).get("rows")
image_run_loop = player.get("run", {'x': None}).get("loop")
# set_animation
self.standing_animations = OrderedDict()
for id in range(image_stand_rows):
self.standing_animations[id] = self.create_animation(image_stand, id, image_stand_columns, image_stand_width, image_stand_height, image_stand_loop)
self.running_animations = OrderedDict()
for id in range(image_run_rows):
self.running_animations[id] = self.create_animation(image_run, id, image_run_columns, image_run_width, image_run_height, image_run_loop)
def create_animation(self, image, id, da, width, height, loop):
frame_list = [image.get_region(x=width * i, y=height * id, width=width, height=height) for i in range(da)]
image_animation = pyglet.image.Animation.from_image_sequence(frame_list, loop, True)
return image_animation
def load_sprite(self):
self.current_animation = self.standing_animations[self.dir_id]
self.sprite = pyglet.sprite.Sprite(self.current_animation, batch=self.game.Batch, group=self.game.GroupEntitySprite)
def update_sprite(self):
if self.previous_direction == self.dir_id:
return
else:
self.sprite.image = self.current_animation
def key_player(self):
if self.keyboard[key.W]:
self.vy += 2
self.dir_id = 7
if self.keyboard[key.S]:
self.vy -= 2
self.dir_id = 3
if self.keyboard[key.D]:
self.vx += 2
self.dir_id = 5
if self.keyboard[key.A]:
self.vx -= 2
self.dir_id = 1
if self.keyboard[key.W] and self.keyboard[key.D]:
self.dir_id = 6
if self.keyboard[key.S] and self.keyboard[key.D]:
self.dir_id = 4
if self.keyboard[key.W] and self.keyboard[key.A]:
self.dir_id = 0
if self.keyboard[key.S] and self.keyboard[key.A]:
self.dir_id = 2
if not self.keyboard[key.W] and not self.keyboard[key.S] and not self.keyboard[key.A] and not self.keyboard[key.D]:
self.keyboard.clear()
self.current_animation = self.standing_animations[self.dir_id]
else:
self.current_animation = self.running_animations[self.dir_id]
def class_player(self, type):
self.statistics_base["hp"] = player_class.get(type, {'x': None}).get("hp")
self.statistics_base["atk"] = player_class.get(type, {'x': None}).get("atk")
self.statistics_base["dif"] = player_class.get(type, {'x': None}).get("dif")
self.statistics_base["atk_sp"] = player_class.get(type, {'x': None}).get("atk_sp")
self.statistics_base["dif_sp"] = player_class.get(type, {'x': None}).get("dif_sp")
self.statistics_base["vel"] = player_class.get(type, {'x': None}).get("vel")
for stat in self.statistics_base:
if self.statistics_base[stat] is None:
self.statistics_base[stat] = 10
def update(self):
self.previous_direction = self.dir_id
self.key_player()
self.update_sprite()
self.sprite.x = self.vx
self.sprite.y = self.vy
启动脚本时,播放器正确显示self.standing_animations
播放器移动时,它会正确显示self.running_animations
这是问题所在,当播放器静止不动时,动画self.standing_animations
不会显示,而是继续显示self.running_animations
经过一些测试以查看问题出在哪里,我设法找到了它。在声明中:
if not self.keyboard[key.W] and not self.keyboard[key.S] and not self.keyboard[key.A] and not self.keyboard[key.D]:
self.keyboard.clear()
self.current_animation = self.standing_animations[self.dir_id]
else:
self.current_animation = self.running_animations[self.dir_id]
self.current_animation = self.standing_animations[self.dir_id]
语句未执行。
我发现改变动画的唯一方法是用self.sprite.image = self.standing_animations[self.dir_id]
替换该部分
这样做显然可以使动画发生变化,但是会固定在第一帧(这是因为不断调用sprite.image)
现在我正在尝试以不同方式进行尝试,但是我仍然遇到相同的问题。因此,在这一点上,我无法弄清楚这到底是我的错误,还是Python或Pyglet的错误/限制。
你能帮我吗?我不再知道该怎么解决这个问题。
编辑:这不是最好的解决方案,但是我已经修改了脚本以创建两个精灵。一种用于跑步,一种用于站立。然后,入口会根据玩家是否移动而从可见状态更改为否。我并不真的喜欢它作为解决方案,但是现在我保留了它,希望有人可以帮助我解决这个问题。