我正在尝试通过手动换档创建一个简单的汽车游戏。但是,我在实施换档时遇到了一些麻烦。
这是我当前的“汽车”代码:
int gear = 1; // Current gear, initially the 1st
int gearCount = 5; // Total no. of gears
int speed = 0; // Speed (km/h), initially 0
int[] maxSpeedsPerGear = new int[]
{
40, // First gear max. speed at max. RPM
70, // Second gear max. speed at max. RPM
100, // and so on
130,
170
}
int rpm = 0; // Current engine RPM
int maxRPM = 8500; // Max. RPM
public void update(float dt)
{
if(rpm < maxRPM)
{
rpm += 65 / gear; // The higher the gear, the slower the RPM increases
}
speed = (int) ((float)rpm / (float)maxRPM) * (float)maxSpeedsPerGear[gear - 1]);
if(isKeyPressed(Keys.SPACE))
{
if(gear < gearCount)
{
gear++; // Change the gear
rpm -= 3600; // Drop the RPM by a fixed amount
if(rpm < 1500) rpm = 1500; // Just a silly "lower limit" for RPM
}
}
}
但是,此实现并没有真正起作用。第一个档位工作正常,但随后的档位变化会导致速度下降。通过添加一些调试消息,当以RPM限制更改时,我得到了这些速度值:
Speed at gear 1 before change: 40
Speed after changing from gear 1 to gear 2: 41
Speed at gear 2 before change: 70
Speed after changing from gear 2 to gear 3: 59
Speed at gear 3 before change: 100
Speed after changing from gear 3 to gear 4: 76
Speed at gear 4 before change: 130
Speed after changing from gear 4 to gear 5: 100
如您所见,每次更改之后的速度在更改之前要慢一些。您如何考虑换档之前的速度,以便在换档时速度不会降低?