school project: simple C++ game (need help with keyboard input)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

school project: simple C++ game (need help with keyboard input)

 
0
  #1
Mar 24th, 2007
Hello,

I have to make a small game in C++ as a school project. Now, although I've made many 2D games before I'm very new to C++, and I ran into a few problems.

One: I need dynamic key input. I now use getch() and it makes the program wait for key input, but I want other code to keep running while no key is pressed yet.

Two: I think I have a memory leak or something, because if I keep running my game a few times, my pc becomes slower. (I have a dutch pc so can't describe it very clearly, but if you press CTRL+ALT+DEL, and look at the performance tab, and then the thing beneath the CPU gets higher everytime I play (from 178 MB to 200, to 300 and then my pc becomes kind of slow).

My code (the game's a very silly not-yet-working correctly car-racing game):

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <conio.h>
  5. #include <process.h>
  6.  
  7. using namespace std;
  8.  
  9. //car variables
  10. string car("|=|");
  11. string car_frontback("0-0");
  12. string car_space_left("");
  13. string car_space_right("");
  14. int car_space_int = 10;
  15. //int car_health = 100;
  16.  
  17. //enemy variables
  18. string enemy("|=|");
  19. string enemy_frontback("X=X");
  20. string enemy_space_left("");
  21. string enemy_space_right("");
  22. int enemy_space_int = 0;
  23. int enemy_pos = 0;
  24.  
  25. //game variables
  26. int row_total = 15;
  27. int space_total = 20;
  28. string game_space(" ");
  29. string line("|");
  30. string show_nothing(" ");
  31. int nothing[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  32. int i = 0;
  33. int num = 0;
  34.  
  35. //keyboard variables
  36. const char KEYRIGHT = 75;
  37. const char KEYLEFT = 77;
  38. char arrow = 0;
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42. //title screen
  43. cout << " -----------------" << endl
  44. << " - Welcome -" << endl
  45. << " - to -" << endl
  46. << " - Ultra Car -" << endl
  47. << " - Racing -" << endl
  48. << " - 3000 -" << endl
  49. << " -----------------" << endl
  50. << "" << endl
  51. << "Use the left and right keys to dodge cars." << endl
  52. << "Press any key to start.";
  53.  
  54. srand(time(NULL));
  55.  
  56. //check keyboard
  57. arrow = kbhit();
  58. while(arrow !=27) {
  59.  
  60. //variables
  61. i = 0;
  62. car_space_left = "";
  63. car_space_right = "";
  64. enemy_space_left = "";
  65. enemy_space_right = "";
  66.  
  67. //wait for kb input... why wait?! :( ..stupid c++
  68. arrow = getch();
  69. switch(arrow)
  70. {
  71. case KEYLEFT:
  72. //left arrow key
  73. if (car_space_int <= space_total) car_space_int+=1;
  74. break;
  75. case KEYRIGHT:
  76. //right arrow key
  77. if (car_space_int > 0) car_space_int-=1;
  78. break;
  79. }
  80.  
  81. //space left to the car
  82. for (i=0; i < car_space_int; i++) {
  83. car_space_left = car_space_left + " ";
  84. }
  85.  
  86. //space right to the car
  87. for (i=0; i <= space_total-car_space_int; i++) {
  88. car_space_right = car_space_right + " ";
  89. }
  90.  
  91. //set enemy position
  92. if (enemy_pos == 14) {
  93. num = 1 + rand() % (20 - 1 + 1);
  94. enemy_space_int = num;
  95. }
  96.  
  97. //space left to the enemy
  98. for (i=0; i < enemy_space_int; i++) {
  99. enemy_space_left = enemy_space_left + " ";
  100. }
  101.  
  102. //space right to the enemy
  103. for (i=0; i <= space_total-enemy_space_int; i++) {
  104. enemy_space_right = enemy_space_right + " ";
  105. }
  106.  
  107.  
  108.  
  109. enemy_pos += 1;
  110. if (enemy_pos > row_total) {
  111. enemy_pos = 0;
  112. }
  113.  
  114.  
  115.  
  116. //clear screen
  117. system("cls");
  118.  
  119. //place game screen lower
  120.  
  121. cout << endl;
  122. cout << endl;
  123. cout << endl;
  124.  
  125. //position of enemy
  126. for (i=0; i<row_total; i++)
  127. {
  128.  
  129. nothing[i] = 0;
  130.  
  131. }
  132.  
  133.  
  134.  
  135. nothing[enemy_pos] = 1;
  136. nothing[enemy_pos+1] = 2;
  137. nothing[enemy_pos+2] = 1;
  138.  
  139. //show road, enemy
  140. for (i=0; i<row_total; i++) {
  141. if (nothing[i] == 0) { show_nothing = " "; }
  142. else if (nothing[i] == 1) { show_nothing = enemy_frontback; }
  143. else if (nothing[i] == 2) { show_nothing = enemy; }
  144. cout << game_space << line << enemy_space_left << show_nothing << enemy_space_right << line << endl;
  145. }
  146.  
  147. //show car
  148. cout << game_space << line << car_space_left << car_frontback << car_space_right << line << endl;
  149. cout << game_space << line << car_space_left << car << car_space_right << line << endl;
  150. cout << game_space << line << car_space_left << car_frontback << car_space_right << line << endl;
  151.  
  152. cout << endl << "check " << enemy_pos << " + " << i;
  153.  
  154.  
  155. }
  156.  
  157. //end game
  158. system("PAUSE");
  159. return EXIT_SUCCESS;
  160. }
  161.  
  162. //I HAVE A MEMORY LEAK SOMEWHERE!!! :(
Yes, I know my code is very very bad... this is the first time I use C++. And yes it's a text-based racing game... :eek:

Basically what it does now is that you can steer a car from left to right, and that other cars run down the road from top to bottom, but they only can down if you keep driving (no keyboard input and they stop moving).

Any help would be much appreciated!
Last edited by 2Dcube; Mar 24th, 2007 at 8:34 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #2
Mar 24th, 2007
you need kbhit()
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #3
Mar 24th, 2007
It's in there already:


//check keyboard
arrow = kbhit();
while(arrow !=27) {
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #4
Mar 24th, 2007
Oopsie. I didn't see it. In that case you need to use it correctly.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #5
Mar 24th, 2007
So you're saying I don't use it correctly? I have no idea how to do so, sorry.

Please be more descriptive.

Also, what could be the reason that my pc becomes slower? I have no previous experiences with memory leaks etc. In Flash you don't have to worry about that...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #6
Mar 24th, 2007
Well where is your time delay as well?
Last edited by iamthwee; Mar 24th, 2007 at 9:16 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #7
Mar 24th, 2007
I have none... i'm not sure where i would need that for...

also, if you just load the code in Dev-C++ for example, you should be able to run it (i only use standard libraries).
Last edited by 2Dcube; Mar 24th, 2007 at 9:18 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #8
Mar 24th, 2007
i only use standard libraries
That's nice, do you think system("cls") is standard and cross platform compatible?
Last edited by iamthwee; Mar 24th, 2007 at 9:22 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #9
Mar 24th, 2007
No, i think it only works on Windows...

I know I'm a noob when it comes to C++, but I do know the basics of programming.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: school project: simple C++ game (need help with keyboard input)

 
0
  #10
Mar 24th, 2007
It's also a compiler issue as well, <conio.h> isn't supported by all compilers.


Anyway back on topic, so where is you time delay?
Last edited by iamthwee; Mar 24th, 2007 at 9:27 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC