Jul 21, 2015 at 5:19pm UTC
So I just started c++ programming about 2 weeks ago and i decided to try and make a basic game. This game has one player that attacks and blocks against a computer.
The player has to quickly hit enter within 0.5 seconds to block the attack or succeed the attack. If they take any longer they fail. I am using the clock function to do this but for some reason it only works for the first turn. It will allow me to block the first attack but then every move it will just say i took 0 seconds and that i succeeded. Any ideas for why it does this?
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <ctime>
using namespace std;
//Globals
int health = 100;
int enmyhp = 100;
int plyrchoose;
int start = clock();
int reactattck;
int reactblck;
int main()
{
srand(time(NULL));
cout << "BattleCry" << endl;
cout << "Press 1 to continue" << endl;
string continuechoose = "1";
int continueloop = 1;
while (continueloop == 1)
{
getline (cin,continuechoose);
if (continuechoose == "1")
{
continueloop = 0;
cout << "Get ready!" << endl;
}
else
{
cout << "What?" << endl;
}
}
while (true)
{
cout << "health ";
cout << health << endl;
cout << "enemy health ";
cout << enmyhp << endl;
cout << "Your enemy prepares to attack!" << endl;
Sleep(5000);
start = clock();
cout << "Go!" << endl;
cin.ignore();
reactblck = clock() - start;
cout << reactblck << endl;
if (reactblck > 500)
{
cout << "Your enemy successfully hit you!" << endl;
health -= 10;
if (health <= 0)
{
cout << "You Died..." << endl;
return 0;
}
}
else if (reactblck <= 500)
{
cout << "You successfully blocked your enemy's attack!" << endl;
}
cout << "health ";
cout << health << endl;
cout << "enemy health ";
cout << enmyhp << endl;
cout << "What do you want to do?" << endl;
cout << "1) Atttack" << endl;
cout << "2) Recover" << endl;
cin >> plyrchoose;
if (plyrchoose == 1)
{
cout << "You prepare to attack!" << endl;
Sleep(5000);
start = clock();
cout << "Go!" << endl;
cin.ignore();
reactattck = clock() - start;
cout << reactattck << endl;
if (reactattck <= 500)
{
cout << "You successfully hit your enemy!" << endl;
enmyhp -= 10;
if (enmyhp <= 0)
{
cout << "You defeated your enemy!" << endl;
return 0;
}
}
else if (reactattck > 500)
{
cout << "Your enemy successfully blocked your attack!" << endl;
}
}
else if (plyrchoose = 2)
{
cout << "You recover your health" << endl;
health += 5;
}
}
}
Jul 21, 2015 at 7:10pm UTC
Look at line 96.
When compiling, make sure to set your compiler to complain about everything it can.
Hope this helps.
Jul 21, 2015 at 7:25pm UTC
Thanks duoas! It turns out that when you entered 1 or 2 to choose it considered it as the attack