944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 27013
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 24th, 2007
0

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

Expand Post »
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):

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2Dcube is offline Offline
12 posts
since Mar 2007
Mar 24th, 2007
0

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

you need kbhit()
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2007
0

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

It's in there already:


//check keyboard
arrow = kbhit();
while(arrow !=27) {
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2Dcube is offline Offline
12 posts
since Mar 2007
Mar 24th, 2007
0

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

Oopsie. I didn't see it. In that case you need to use it correctly.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2007
0

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

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2Dcube is offline Offline
12 posts
since Mar 2007
Mar 24th, 2007
0

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

Well where is your time delay as well?
Last edited by iamthwee; Mar 24th, 2007 at 9:16 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2Dcube is offline Offline
12 posts
since Mar 2007
Mar 24th, 2007
0

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

Quote ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 24th, 2007
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
2Dcube is offline Offline
12 posts
since Mar 2007
Mar 24th, 2007
0

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: C++ learning, moving on the next step
Next Thread in C++ Forum Timeline: class and vector problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC