PDA

View Full Version : Counting down with dec()



Wizard
31-05-2007, 09:14 AM
System: Windows XP
Compiler/IDE: Delphi 6
API: DelphiX?

Hi everyone. My game is written in Delphi6 and DelphiX. It's a 2d car racer and I have a car on a road with a certain amount of gas, if the cas runs out before the car reaches the end of the road the game is finished and the user has to try again. The gas is a constant of 1200 and it is decreased with dec(fGas);

I have managed to get the movement in my game independent of frame rate with:


dxtimer1.Interval:=1000 div 200;
NewTime := TimeGetTime;
UpdateSpeed := (NewTime - OldTime) * OneMSec;
OldTime := NewTime;
and
y := y + (7 * updatespeed);

My problem relates to running the game on different pc's, the movement is now the same on slow and fast machines but on a fast machine the gas "dec(fGas)" runs too fast--from 1200 to 0.

I played around with GetTickCount, timeGetTime etc. but no success. Any ideas on how to get the "dec(fGas)" so that it runs down from 1200 to 0 at a constant rate on slow and fast machines?

Thanks for your help.

jasonf
31-05-2007, 09:24 AM
There is no difference between moving in a particular direction by X units and decreasing the amount of fuel by X units in a particular time frame.

I'd have another look at your code and try to get the time independant solution to work for your fuel too.. it will work, you just have to have the right approach.

Basically, as you have for your movement, you need to know the amount to update by for each frame.

In your example, you work out how many MSecs have passed.
So your fuel consumption can use the same value as your movement code. By using a consumptionb rate per MSecs, you can determine how much fuel has been spent.

Fuel := Fuel - (CurrentConsumptionRatePerMSec * MSecsElapsed);
If Fuel<0 then Fuel := 0;

Something like that anyway, you'll be able to specify that you've got your foot on the gas and therefore use more fuel.. this can in turn provide more speed.

Wizard
31-05-2007, 10:54 AM
Thanks!!!

I got it working :-)

fGas := fGas - 1 * UpdateGas;

NewGas := TimeGetTime;
UpdateGas := (NewGas - OldGas) * OneMSec;
OldGas := NewGas;

dxfont1.TextOut(dxdraw1.surface,470,10,FloatToStrF (Car.gas / 10, ffFixed,15,0));

I changed the type of fGas from integer to Extended to work with the FloatToStrF :-)