user14948179
user14948179

Reputation:

would appreciate someone showing me how to work this out

I am making a little game where you have a pistol and you press 0 to shoot, and each time you do the health decreases by 12 until death then new target, but I can't figure out how to make the loop.

I created stop at 88 (it's at 89 for some reason but I can figure it out sooner or later) and then count down more when I press the 0 key again. I have tried if statements and everything I've really learned so far, but I cannot figure it out.

void gun_shots(int pistol)
{
     std::cout << "100 health ";
     int i = 100;
     while ( i > 87)
     {
         std::cout << i << " ";
         i--;
         if (i == 87)
         {
             std::cout << "AGH i lost health ";
         }
     }
}

 int main()
 {
     std::cout << "0: play game " << std::endl << "1: end game ";
     int shoot;
     std::cin >> shoot;
     switch (shoot)
     {
     case 0:
         gun_shots(shoot);

         break;
     }
 }

Upvotes: 0

Views: 74

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595827

You want to shoot the pistol on each press of the 0 key, and you want each shot to decrement the health. So get rid of the loop in gun_shots(), it really doesn't belong there. Move the loop into main() instead, eg:

#include <iostream>

int health;

void shoot_gun()
{
    health -= 12;
}

bool is_dead()
{
    return health <= 0;
}

void spawn()
{
    health = 100;
    std::cout << "New target spawned" << std::endl;
}

int main()
{
    int choice;

    spawn();

    do
    {
        std::cout << "health: " << health << std::endl
                  << "0: shoot " << std::endl
                  << "1: end game" << std::endl;
        std::cin >> choice;
        switch (choice)
        {
            case 0:
                shoot_gun();
                if (is_dead())
                {
                    std::cout << "Target died" << std::endl;
                    spawn();
                }
                break;
        }
    }
    while (choice != 1);
}

Upvotes: 1

Related Questions