Restart player

Hi all,

I’m new to programming and new to game development, any help would be greatly appreciated. I did the shooter game tutorial and I learned a lot. I’m trying to add functionality so when the player’s health goes to 0 then the game kind of restarts. The player class has an active variable and it gets set to false when player’s health is 0 but the game continues on. Any ideas? this is the code where the isActive gets set to 0.

        for (int i = 0; i < enemies.Count; i++)
        {
            enemyRectangle = new Rectangle((int)enemies[i].Position.X, (int)enemies[i].Position.Y, enemies[i].Width, enemies[i].Health);


            if (playerRectangle.Intersects(enemyRectangle))
            {

                //since the enemy collided with the player destroy it
                enemies[i].Health = 0;

                //show the explosion where the enemy was...
                AddExplosion(enemies[i].Position);

                //subtract the health from the player based on the enemy damage
                player.Health = player.Health - enemies[i].Damage;

                //if the player health is less than zero we died
                if (player.Health <= 0)
                {
                    player.Active = false;
                    AddExplosion(player.Position);
                    
                   
                }
            }

            for (var l = 0; l < LaserBeams.Count; l++)
            {
                //create a rectangle for this laserbeam
                laserRectangle = new Rectangle((int)LaserBeams[l].Position.X, (int)LaserBeams[l].Position.Y, LaserBeams[l].Width, LaserBeams[l].Height);

                //test the bounds of the laser and enemy
                if (laserRectangle.Intersects(enemyRectangle))
                {
                    //show the explosion where the enemy was...
                    AddExplosion(enemies[i].Position);
                    //kill of the enemy
                    enemies[i].Health = 0;

                    //kill of the laserbeam
                    LaserBeams[l].Active = false;
                }


            }

What would you expect to happen? You’re not doing anything with player.Active. You need some logic that checks if player.Active is false and resets the game or whatever else you want to happen if that’s the case.

when active is set to false you have to stop updating the game logic and reset everything and then set active back to true and start updating it again

Hi Kay,

Is there an example you recommend? I have no idea how to reset the game logic.

Thank you!
Alex

that all depends on your code. but i guess you have a update loop where you call other update functions? you can surround those with an if statement and check if the active boolean is still true.