How can I count down healthbar hitpoint per second?

I have a car game when the car runs out the road, its healthbar will be decreased. But i want to slow down the decrease. How can I do it?
This is the current code.

for (int i = 20; i >= 0; i--)
            {
                    if (car.getPosY() < 450 || car.getPosY() > 500)
                    {
                        car.hitPoints = car.hitPoints - 1;
                        // if (car.hitPoints <= 0) car.active = false;
                    }
            }

The Update method has a parameter, gameTime, which is of type GameTime. This contains information about how much time has elapsed in your game, both total time and the time since the last update.

Since you want to decrease your car’s hitpoints by an amount per second, you’re likely most interested in how much time has elapsed since the last update. This is stored in the GameTime.Elapsed property, and the total number of seconds can be obtained via GameTime.Elapsed.TotalSeconds.

Once you have this, you can multiply by how many hitpoints your car should lose per second to calculate the total amount of hitpoints that should be lost in that update cycle :slight_smile:

2 Likes