| | |
My simple game: what would i use
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
EDIT: I've got a while loop in my code, just now when player 1 goes to have his/her second attack the hp is reset to 99,
c++ Syntax (Toggle Plain Text)
#include <conio.h> #include <iostream> using namespace std; int main() { int p1attack, p2attack, p1hp, p2hp; char attack; p1hp = 99; p2hp = 99; system("TITLE Game."); while(p1hp > 0 && p2hp > 0) { cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : "; cin >> attack; switch(attack) { case 'a': case 'A': srand ( time(NULL) ); p1attack = rand() % 99 + 1; cout << "You hit " << p1attack << "!"; break; default: cout << endl << "INVALID SELECTION!"; break; } p2hp -= p1attack; cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : "; cin >> attack; switch(attack) { case 'a': case 'A': srand ( time(NULL) ); p2attack = rand() % 99 + 1; cout << "You hit " << p2attack << "!" << endl; break; default: cout << endl << "INVALID SELECTION!"; break; p1hp -= attack; } } getch(); }
Last edited by Black Magic; Apr 27th, 2008 at 1:28 pm.
C Plus Plus Coder.
Fourteen Years Of Age
Fourteen Years Of Age
Hey Black Magic i have solved your problem.
Well just examine That you have typed this code in
Line 54. Which probarbly doesnt get executed because there are break; statements above it.
So You actually need to paste the code in between Line 46 and line 47 so that the value gets changed.
So here is the total program with the adjustment.
Well just examine That you have typed this code in
C++ Syntax (Toggle Plain Text)
p1hp -= p2attack;
Line 54. Which probarbly doesnt get executed because there are break; statements above it.
So You actually need to paste the code in between Line 46 and line 47 so that the value gets changed.
So here is the total program with the adjustment.
C++ Syntax (Toggle Plain Text)
#include <conio.h> #include <iostream> using namespace std; int main() { int p1attack, p2attack, p1hp, p2hp; char attack; p1hp = 99; p2hp = 99; system("TITLE Game."); while(p1hp > 0 && p2hp > 0) { cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : "; cin >> attack; switch(attack) { case 'a': case 'A': srand ( time(NULL) ); p1attack = rand() % 99 + 1; cout << "You hit " << p1attack << "!"; break; default: cout << endl << "INVALID SELECTION!"; break; } p2hp -= p1attack; cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : "; cin >> attack; switch(attack) { case 'a': case 'A': srand ( time(NULL) ); p2attack = rand() % 99 + 1; p1hp -= p2attack; cout << "You hit " << p2attack << "!" << endl; break; default: cout << endl << "INVALID SELECTION!"; break; } } getch(); }
Or, if you'd like to keep in consistent with the code for player 1's attack, you could simply put it outside of the switch statement but inside the while loop (like line 57 or 58 in Sky Diploma's program). If you do do that though, remember to take away that same code from line 47.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right
For example if the user enters something other than "A".
For example if the user enters something other than "A".
•
•
•
•
I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right
For example if the user enters something other than "A".
p2hp -= p1attack; is called and p1attack hasn't been initialized, you'll get not so optimal results.So either change it, or in the beginning of the loop set p1attack and p2attack to 0.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Thanks guys, now i was wondering now if i should make it so the other player cannot make your hp a negative number, how could this be done? Please reply.
c++ Syntax (Toggle Plain Text)
#include <conio.h> #include <iostream> using namespace std; int main() { int p1attack, p2attack, p1hp, p2hp; char attack; p1hp = 99; p2hp = 99; system("TITLE Game."); while(p1hp > 0 && p2hp > 0) { cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : "; cin >> attack; switch(attack) { case 'a': case 'A': srand ( time(NULL) ); p1attack = rand() % 50 + 1; cout << "You hit " << p1attack << "!"; break; default: cout << endl << "INVALID SELECTION!"; break; } p2hp -= p1attack; cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : "; cin >> attack; switch(attack) { case 'a': case 'A': srand ( time(NULL) ); p2attack = rand() % 50 + 1; p1hp -= p2attack; cout << "You hit " << p2attack << "!" << endl << endl; break; default: cout << endl << "INVALID SELECTION!"; break; } } getch(); }
C Plus Plus Coder.
Fourteen Years Of Age
Fourteen Years Of Age
Right after
You may also try making the health values unsigned, though the above method should work well enough
p2hp -= p1attack; , add: C++ Syntax (Toggle Plain Text)
if(p2hp < 0) p2hp = 0;
You may also try making the health values unsigned, though the above method should work well enough
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
![]() |
Similar Threads
- How do i create a simple game using c++?? (C++)
- C++ game? (C++)
- build java game (Java)
Other Threads in the C++ Forum
- Previous Thread: game code help
- Next Thread: Using functions without calling them from any class
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






