Puzzled by my sprite's unequal +/ - velocity

前端 未结 2 1751
暗喜
暗喜 2021-01-22 11:53

I have two sprites in my game. The zombie sprite works perfectly, moving in all directions at a velocity of 1.0. My player sprite however, despite moves more slowly in the posit

2条回答
  •  花落未央
    2021-01-22 12:22

    Ok. I think the problem is that the number of pixels that the sprite is moved each update is rounded down, so that 2.25 becomes 2 pixels and -2.25 becomes -3 pixels. Moving by a fractional number of pixels doesn't make sense I think. If you change lines 229 - 233 to

    else:
        # move player in that direction
        player.velocity = calc_velocity(player.direction, 2.0)
        player.velocity.x *= 2.0
        player.velocity.y *= 2.0
    

    The velocity is now an integer and there would be no rounding problems. It is faster though. Is there some reason why you don't just have the velocity as an integer instead of a float squared?

提交回复
热议问题