954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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):

#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include <process.h>

using namespace std;

//car variables
string car("|=|");
string car_frontback("0-0");
string car_space_left("");
string car_space_right("");
int car_space_int = 10;
//int car_health = 100;

//enemy variables
string enemy("|=|");
string enemy_frontback("X=X");
string enemy_space_left("");
string enemy_space_right("");
int enemy_space_int = 0;
int enemy_pos = 0;

//game variables
int row_total = 15;
int space_total = 20;
string game_space("       ");
string line("|");
string show_nothing("   ");
int nothing[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int i = 0;
int num = 0;

//keyboard variables
const char KEYRIGHT = 75;
const char KEYLEFT = 77;
char arrow = 0;

int main(int argc, char *argv[])
{
    //title screen
    cout << "   -----------------" << endl
         << "   -    Welcome    -" << endl
         << "   -      to       -" << endl
         << "   -   Ultra Car   -" << endl
         << "   -    Racing     -" << endl
         << "   -     3000      -" << endl
         << "   -----------------" << endl
    << "" << endl
    << "Use the left and right keys to dodge cars." << endl
    << "Press any key to start.";
    
    srand(time(NULL));

    //check keyboard
    arrow = kbhit();
    while(arrow !=27) {
                
                //variables
                i = 0; 
                car_space_left = "";
                car_space_right = "";
                enemy_space_left = "";
                enemy_space_right = "";
        
        //wait for kb input... why wait?! :( ..stupid c++
        arrow = getch();     
        switch(arrow)
        {                          
            case KEYLEFT:
                 //left arrow key
                 if (car_space_int <= space_total) car_space_int+=1;
            break;
            case KEYRIGHT:
                 //right arrow key
                 if (car_space_int > 0) car_space_int-=1;
            break;
        }
                
        //space left to the car
        for (i=0; i < car_space_int; i++) {
            car_space_left = car_space_left + " ";
        }
        
        //space right to the car
        for (i=0; i <= space_total-car_space_int; i++) {
            car_space_right = car_space_right + " ";
        }
        
        //set enemy position
        if (enemy_pos == 14) {
            num = 1 + rand() % (20 - 1 + 1);
            enemy_space_int = num;
        }
        
        //space left to the enemy
        for (i=0; i < enemy_space_int; i++) {
            enemy_space_left = enemy_space_left + " ";
        }
        
        //space right to the enemy
        for (i=0; i <= space_total-enemy_space_int; i++) {
            enemy_space_right = enemy_space_right + " ";
        }
        
        
        
        enemy_pos += 1;
        if (enemy_pos > row_total) {
           enemy_pos = 0;
        }
        

        
        //clear screen
        system("cls"); 
        
        //place game screen lower
        
        cout << endl;
        cout << endl;
        cout << endl;
        
        //position of enemy
        for (i=0; i<row_total; i++)
        {
            
             nothing[i] = 0;
             
        }
        
        
        
        nothing[enemy_pos] = 1;
        nothing[enemy_pos+1] = 2;
        nothing[enemy_pos+2] = 1;
        
        //show road, enemy
        for (i=0; i<row_total; i++) {
                  if (nothing[i] == 0) { show_nothing = "   "; }
             else if (nothing[i] == 1) { show_nothing = enemy_frontback; }
             else if (nothing[i] == 2) { show_nothing = enemy; }
        cout << game_space << line << enemy_space_left << show_nothing << enemy_space_right << line << endl;
        }
        
        //show car
        cout << game_space << line << car_space_left << car_frontback << car_space_right << line << endl;
        cout << game_space << line << car_space_left << car           << car_space_right << line << endl;
        cout << game_space << line << car_space_left << car_frontback << car_space_right << line << endl;
        
        cout << endl << "check " << enemy_pos << " + " << i;  
        
          
        }
                
    //end game
    system("PAUSE");
    return EXIT_SUCCESS;
}

//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!

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

you need kbhit()

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

It's in there already:


//check keyboard
arrow = kbhit();
while(arrow !=27) {

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Oopsie. I didn't see it. In that case you need to use it correctly.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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...

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Well where is your time delay as well?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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).

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
i only use standard libraries

That's nice, do you think system("cls") is standard and cross platform compatible?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

It's also a compiler issue as well, isn't supported by all compilers.


Anyway back on topic, so where is you time delay?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I'm sorry,I have no idea what a time delay is...

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Let's imagine instead of clearing the screen every time you pressed one of the arrow keys, you cleared the screen every half second.

Timer? Yes.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I need to refresh the screen everytime the screen changes...

Anyway, when using getch() the program waits till there's keyboard input. Is there a way to make it so that the program keeps doing code, while still using getch() (or something else) to check for keyboard input?

If I could just get that to work everything'd be fine..

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Yes but you keep ignoring my suggestion about using a timer/delay function.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

That's because I have no idea what to do with it, what it's for, why I need it, etc.

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Well basically it is telling you how many times you need to refresh the screen.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
you need kbhit()
That's nice, do you think system("cls") is standard and cross platform compatible?


This was hilarious!!! Suggesting a non-standard function then pointing out something that's not standard! :) Decide which side of the standard you sit on and stay there, don't vacillate.

Also, system() is a standard library function. It's the parameter "cls" that's system dependent.

You don't have memory leaks. You have a runaway program that will take whatever resources it wants.

Fix the kbhit() problem, and that will correct most of it. (if you knew to use kbhit() you certainly can find out how to use it)

Fix your input then we'll get the timer thing working...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Hey, actually...

It works now...! I used GetAsyncKey and I now have a working text based car racing game.

system(cls) NEEDS to be done every time you press the key, because everytime you press it the car moves to the left or right.

Now it's actually done more often (also when you not press) because the opposing cars drive down the road.

So, the screen needs to be refreshed a lot because it's a constantly changing game. Well, it's a game.

ANyway, maybe what you mean is that it runs as quick as the processor allows it to, so that's why i need a delay? (so it runs this quick on every pc)??

You don't have memory leaks. You have a runaway program that will take whatever resources it wants.


Ah i see... that makes it a bit more clear for me, thanks.

2Dcube
Newbie Poster
12 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

>This was hilarious!!! Suggesting a non-standard function then pointing out something that's not standard!

I only pointed that out cos the OP thought he was using standard functions. I know very well, due to the nature of this program that non-standard functions are necessary - for example clearing the screen reliably. But keep laughing if you found that funny mr disney -or better still next time read the thread more carefully:)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Since we're using WinAPI already, why not use it to clear the screen instead of using system calls? The solution below I found at CProgramming :

#include <windows.h>

void clear_screen ( void )
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}
John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You