我正在尝试创建一个游戏,其中一组矩形指示精灵将移动到哪里,具体取决于矩形的颜色。
目前,在尝试实现较小规模的代码时,我收到错误" TypeError:不支持的操作数类型* =:' float'和'实例'"
此代码位于sprite类中,应该使sprite移向rect对象。
def move(self, delta):
if self.target is not None:
# get the full displacement to target
displacement = self.target.sub(self.pos)
# normalize it to get direction
direction = displacement.normalize()
# and multiply by speed to get velocity (pixels/ms)
self.vel = direction.scale(self.vel)
# to get the actual step for this frame, multiply
# velocity by time (numerical integration)
step = self.vel.scale(delta)
# prevent overshooting (causes jitter back and forth)
if step.magnitude() > displacement.magnitude():
self.pos = self.target
self.target = None
else:
# otherwise update position by adding step
self.pos = self.pos.add(step)
return
我调试了以检查delta是否为浮点数而不是,因此'实例'可能是问题所在。我的理解是实例是指一个基于某个类的对象,是我自己的vector2类。或者可能正在调用此方法的类。
def scale(self, s):
ans = vector2(self.x, self.y)
ans.x *= s
ans.y *= s
return ans
这就是'移动'被称为。它在主游戏循环中
# UPDATE simulation
end = pygame.time.get_ticks()
delta_time = end - cycle_time
cycle_time = end
# Checks if player is touching any objects
for obj in object_list:
player_sprite.update(obj)
for obj in object_list:
if obj.status == 'active':
p_sprite.target = obj.pos
任何关于这个错误根源的帮助都会非常感激,我对python来说比较新。