考虑出界位置是6和-6。
我想让船掉头并朝相反的方向移动。
这是我拥有的代码.. 它仍然不能 100% 地按我想要的方式工作。我很想知道是否有人对如何改进有任何想法。这是我的代码的逻辑。
//If the ship hits a boundary it turns around and moves in the opp.
//direction. To do this, the ship's velocty should be flipped from a
//negative into a positive number, or from pos to neg if boundary
//is hit.
//if ship position is -5 at velocity -1 new ship pos is -6
//if ship position is -6 at velocity -1 new ship velocity is +1
// new ship position is +5
这是我的代码:
public void move()
{
position = velocity + position;
if (position > 5)
{
velocity = -velocity;
}
else if (position < -5)
{
velocity = +velocity;
}
}