Thread: My Maze Game --- A Few Questions

  1. #1
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050

    My Maze Game --- A Few Questions

    I'm trying to make a maze...wait let me rephrase that, I am making a maze game. You start off not knowing not any of the map, but as you move farther into the maze you see more of the map (all discovered squares never go away). Right now my game could be finished after a few hours of work (just a matter of typing in some code), but it would contain a rather poor interface for this type of game. Instead of being able to use the arrow keys when moving through the maze you have to enter U/u (up), D/d (down), L/l (left), and R/r (right). I would rather make it so the player could just use the arrow keys to move through the maze...this would make the game a lot more fun. Could anyone tell me how I could do this? I'm not sure how the program can detect the key movement, then load the graphics (a console app so they are only * o X ) for the squares that should be made visible. Here is an example of what I currently have:

    if(d_maze[2][0] == '*' && var_1 == 1)
    {
    do
    {
    cout << "Would you like to move up, down, or right?\n";
    cin >> move;
    if(move == 'U' || move == 'u')
    {
    d_maze[2][0] = 'o';
    d_maze[1][0] = '*';
    d_maze[0][0] = 'o';
    d_maze[2][1] = 'o';
    var_1 = 0;
    break;
    }
    if(move == 'D' || move == 'd')
    {
    d_maze[2][0] = 'o';
    d_maze[3][0] = '*';
    d_maze[4][0] = 'o';
    d_maze[4][1] = 'X';
    d_maze[2][1] = 'o';
    d_maze[3][1] = 'X';
    var_1 = 0;
    break;
    }
    if(move == 'R' || move == 'r')
    {
    d_maze[2][0] = 'o';
    d_maze[2][1] = '*';
    d_maze[1][0] = 'o';
    d_maze[3][0] = 'o';
    d_maze[3][1] = 'X';
    d_maze[3][2] = 'X';
    d_maze[1][1] = 'X';
    d_maze[1][2] = 'X';
    d_maze[2][2] = 'o';
    var_1 = 0;
    break;
    }
    }while(var_1 == 1);
    }

    Here is a basic breakdown of how the program works:

    show maze
    ask for user move
    input user move (graphics)
    ...repeat process until game end

    Also, another thing I would like to do with the program is have a timer. This would a certain challenge to the game by discouraging slow decisions by the user. I want the timer to continue to run in until either time = 0 or until game end. I'm sure there is a way to do this, but I just don't know the basic syntax of it all.

    Hopefully somebody can help me on this, because I would like to be able to improve the gameplay/interface of my maze game. If not I guess I'll have to do without for the time being.

  2. #2
    What is the * o and X in your program for?

    What compiler are you using?

    I will get back to you on the key thing, but i believe kbHit(); may work. (i don't think that is the correct caps though)

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    The * is for the player's piece so to speak.
    The o is to show that this is a square you can move to.
    The X is to show that this is a wall and you can't move there.

    I'm using DevC++ as my compiler.

  4. #4

    Post

    Oh, and if you would kindly use [CODE] tags? it makes the posts much nicer looking. See the note at the top of the forum for more info...

    And, i havent gotten kbhit to work yet either.

  5. #5
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Well, thanks for trying to help, but I found out to use arrow key movement appropriately (somewhat at least). The only problem is that you can't use arrow key movement in a console application, or can you and I'm just being really stupid right now?

    Does anyone know how you can run a timer? I want the program to last say 30 seconds, what would be some basic code for it? I've been thinking that maybe having a continous loop while I subtract 1 from it each time, and redisplay the new number, but this might make the intervals too fast. Plus I'm not sure how I could make it to loop and run while other processes are taking place. Any help would be greatly appreicated...thanks.

  6. #6
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    If you're programming for the win32 console you should look into the ReadConsoleInput() function.
    "The mind, like a parachute, only functions when open."

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    19
    When you press an arrow key two hits are storred in the buffer therefor you need to use getch() twice to find out which arrow key is pressed. Something like this.


    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
    	int iKey;
    	char key;
    	
    	do
    	{
    		if(kbhit())			//if a key is pressed
    		{
    			key = getch();	//get the key
    			iKey = (char)key;//change it to an int
    			cout<<"Key: "<<key<<"\tiKey: "<<iKey<<endl;//display it
    		}
    	}while(key != 'Q' && key != 'q');
    	
    	return 0;
    }
    This took me foooorever to fiqure out a while ago.

  8. #8
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Thank you very much for that code, WR. The only problem with it is that Dev can't use getch() or kbhit(), so the code won't work with Dev. Luckily, though, I have an introductory version of MSVC++ that I am able to use. Now it's just time to implement that code into my program.

    It seems like everybody is avoiding this question, which is fine but I would appreciate it if it were answered. How a run a 30 second timer while the game is going on? I want the timer to count down from 30-0 and end when either the timer reaches 0 or the player has reached the end of the maze. The timer also has to loop continously while the rest of the game is going on. Could anybody help me on this, please?

  9. #9
    I am somewhat new to C++, but i know from my java experiance that if you want to do what you are saying, you will have to have another thread that does the timer. Unfortuantely, i have no idea how to do that.

  10. #10
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Just use the sleep() function.....I use dev-C++ to.....

  11. #11
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    time_t seconds;

    seconds = time(NULL);
    long secondsToCountTo = seconds + 30;

    while (seconds <= secondsToCountTo)
    {
    doStuff();
    }


    I *think* this will work.. if not you can look up functions in time.h yourself.

  12. #12
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Thanks, Dual-Catfish, for the help. I was going to go look in time.h myself, because it didn't seem as if anybody was going to answer that question for me, but since you did you have saved me some time.

  13. #13
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    this may work....

    for the 30 second thing this could work

    Code:
    ]
    x = 0;
    while(x != 30)
    {
    
    Sleep(1000);  //sleeps one second
    x++;
    do stuff here();  
    //then you can check what x equals 
    //for the time spent inside the loop and \
    //check what key were pressed etc.
    
    }
    it only 'sleeps' for 1 second at a time, because otherwise you would pause the computer for too long before doing anything because sleep pauses the computer from doing almost everything but it will still remember the key you press during the sleep

    Wow! what I wrote above is confusing
    ummmmm basically the code should do what you want (i think)

  14. #14
    Unregistered
    Guest
    i've read something somewhere in a book about CLOCKS_PER_SECOND or maybe it was TICKS_PER_SECOND, and using how long the program has been running to create a time counter

  15. #15

    getche()

    In BloodShed Dev-C++, which i have it is possible to use getche().

    Just:
    Code:
    #import <conio.h>
    to get the character hit, cast it to a char, otherwise do a compare of the integer that it returns. the prototype of getche:
    Code:
    int getche();
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. A few questions on game programming
    By mramazing in forum Game Programming
    Replies: 7
    Last Post: 01-11-2009, 05:48 AM
  3. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  4. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  5. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM