User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,910 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,647 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 6690 | Replies: 27 | Solved
Reply
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

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

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

#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!
Last edited by 2Dcube : Mar 24th, 2007 at 7:34 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,666
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #2  
Mar 24th, 2007
you need kbhit()
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

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

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


//check keyboard
arrow = kbhit();
while(arrow !=27) {
Reply With Quote  
Join Date: Aug 2005
Posts: 4,666
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #4  
Mar 24th, 2007
Oopsie. I didn't see it. In that case you need to use it correctly.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

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

  #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  
Join Date: Aug 2005
Posts: 4,666
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #6  
Mar 24th, 2007
Well where is your time delay as well?
Last edited by iamthwee : Mar 24th, 2007 at 8:16 am.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

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

  #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 8:18 am.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,666
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #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 8:22 am.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Mar 2007
Posts: 12
Reputation: 2Dcube is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
2Dcube 2Dcube is offline Offline
Newbie Poster

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

  #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  
Join Date: Aug 2005
Posts: 4,666
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #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 8:27 am.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC