Thread: Problems with a simple console game

  1. #1
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23

    Problems with a simple console game

    I am a simple newbie at C++ but im eager to learn. I tried to use the sample code from a C++ book (windows game programming for dummies) and I compiled it on my Dev C++ compiler. After fixing some simple errors (usually spelling mistakes) I found 2 errors I can't figure out. Here are the errors:

    [Warning] In function 'void Init_Graphics()";
    'OL' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each)

    And another error:

    At global scope:
    'main' must return 'int'
    [Build Error] [main.o] Error 1

    And here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    #include <windows.h>
    #include <time.h>
    // DEFINES /////////////////
    #define MAX_X   77     // Max X Pos for player
    #define SCROLL_POS  24 // The point that scrolling occurs
    // PROTOTYPES //////////////
    void Init_Graphics(void);
    inline void Set_Color(int fcolor, int bcolor);
    inline void Draw_String(int x, int y, char *string);
    // GLOBALS /////////////////
    CONSOLE_SCREEN_BUFFER_INFO con_info; // Holds screen info
    HANDLE hconsole;                     // Handle to Console
    int game_running = 1;                // State of game 1=run 0=done
    // FUNCTIONS ///////////////
    void Init_Graphics(void)
    {
        // This initializes the console graphics engine
        COORD console_size = {80,25}; // Size of Console
        // Seed the random number generator with time
        srand((unsigned)time(NULL));
        // Open i/o channel to console screen
        hconsole=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, OL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, OL);
        // Make sure we are in 80x25
        SetConsoleScreenBufferSize(hconsole,console_size);
        // Get details for console screen
        GetConsoleScreenBufferInfo(hconsole,&con_info);
    } // end Init_Graphics
    ///////////////////////////
    inline void Set_Color(int fcolor, int bcolor=0)
    {
        // This function sets the color of the console output
        SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) | fcolor));
    } // End Set_Color
    ///////////////////////////
    inline void Draw_String(int x, int y, char *string)
    {
        // This function draws a string at the give x,y
        COORD cursor_pos; // used to pass coords
        // set printing position
        cursor_pos.X = x;
        cursor_pos.Y = y;
        SetConsoleCursorPosition(hconsole,cursor_pos);
        // Print the string in current color
        printf("%s",string);
    } // End Draw_String
    ///////////////////////////
    inline void Clear_Screen(void)
    {
        // This function clears the screen
        // Set color to white on black
        Set_Color(15,0);
        // clear the screen
        for (int index=0; index<=25; index++)
            Draw_String(0, SCROLL_POS,"\n");
    } // End Clear_Screen
    // MAIN GAME LOOP /////////
    void main(void)
    {
        char key;              // Player input Data
        int player_x = 40;     // Players X position
        // SECTION: initialization
        // Set up the console text graphics system
        Init_Graphics();
        // Clear the screen
        Clear_Screen();
        // SECTION: main event loop, this is where all the action
        // takes place, (the general loop is erase-move-draw)
        while(game_running)
        {
            // SECTION: erase all objects or clear screen
            // nothing to erase in our case
            // SECTION: get player input
            if (kbhit())
            {
                    // Get keyboard data, and filter it
                    key = toupper(getch());
                    // Check if player is trying to exit, if so, exit
                    if (key=='Q' || key==27)
                                    game_running=0;
                    // Check if player is moving left
                    if (key=='A')
                                    player_x--;
                    // Check if player is moving right
                    if (key=='S')
                                    player_x++;
            } // end if
            // SECTION: game logic and futhur processing
            // make sure player stays on screen
            if (++player_x > MAX_X)
                    player_x=MAX_X;
            if (--player_x < 0)
                    player_x=0;
            // SECTION: draw everything
            // draw next star at random position
            Set_Color(15,0);
            Draw_String(rand()%80, SCROLL_POS,".\n");
            // Draw Player
            Set_Color(rand()%15,0);
            Draw_String(player_x,0,"<-*->");
            Draw_String(0,0,"");
            // SECTION: synchronize to a constant frame rate
            Sleep(40);
        } // End while
    // SECTION: shutdown and bail
    Clear_Screen();
    printf("\nG A M E O V E R \n\n");
    } // End main
    Yes, I know its a simple game, but you gotta start somewhere.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Welcome to the boards!

    Hmm...I think you are meaning to use NULL instead of OL...but I'm not 100% sure. The two values you're passing OL for are:

    LPSECURITY_ATTRIBUTES lpSecurityAttributes,

    and

    HANDLE hTemplateFile

    so, unless you need to pass security attributes, that can be NULL, and msdn says you only need to pass hTemplateFile if you're opening an existing file.



    Also, do not EVER use void main, EVER!

    int main

    int int int int int

    int main

    *dances around* int int int *dances*

  3. #3
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Sweet!
    Thanks for the help.

  4. #4
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    More questions:

    I am trying to add the option of moving up and down (change in y), but I can't seem to figure out how to do it. Try to give me hints, so I can figure it out.

    Also, It only seems to work at the size of 80x25. I tried 100x100, but then the stars don't seem to work right. Whats up with that?

  5. #5
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Wink

    about the stars , first try 100, 25 - what do you see - they work fine but only in an area of 80 pixels - why ?
    because of this :

    Code:
            Set_Color(15,0);
            Draw_String(rand()%80, SCROLL_POS,".\n");
            // Draw Player
            Set_Color(rand()%15,0);
            Draw_String(player_x,0,"<-*->");
            Draw_String(0,0,"");
    try this :

    Code:
            Set_Color(15,0);
            Draw_String(rand()%100, SCROLL_POS,".\n");
            // Draw Player
            Set_Color(rand()%15,0);
            Draw_String(player_x,0,"<-*->");
            Draw_String(0,0,"");
    I'm still figuring the problem about not moving 'Y' out... as well on the starts as on the player...hmm...I'll post that later.

    p.p.s I'm this close to the awnser of how can you let it move up and down.
    Last edited by MystWind; 03-06-2005 at 09:00 AM.
    PLay MystWind beta , within two years

  6. #6
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Lightbulb

    p.s if you insert this code

    Code:
                    // Draw Player
            Set_Color(rand()%15,0);
            Draw_String(player_y,0,"<-*->");
            Draw_String(0,50,"");
    instead of

    Code:
                    // Draw Player
            Set_Color(rand()%15,0);
            Draw_String(player_y,0,"<-*->");
            Draw_String(0,0,"");
    you won't see the flashing thing in the upper left corner because its out vieuw now.
    Last edited by MystWind; 03-06-2005 at 05:40 AM.
    PLay MystWind beta , within two years

  7. #7
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    AHA , this will be my last post , here look what you get when you would change its 'y' :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    #include <windows.h>
    #include <time.h>
    
    // DEFINES /////////////////
    
    #define MAX_X   77     // Max X Pos for player
    #define SCROLL_POS  24 // The point that scrolling occurs
    
    // PROTOTYPES //////////////
    
    void Init_Graphics(void);
    inline void Set_Color(int fcolor, int bcolor);
    inline void Draw_String(int x, int y, char *string);
    
    // GLOBALS /////////////////
    
    CONSOLE_SCREEN_BUFFER_INFO con_info; // Holds screen info
    HANDLE hconsole;                     // Handle to Console
    int game_running = 1;                // State of game 1=run 0=done
    
    // FUNCTIONS ///////////////
    
    void Init_Graphics(void)
    {
        // This initializes the console graphics engine
        COORD console_size = {80,25}; // Size of Console
        // Seed the random number generator with time
        srand((unsigned)time(NULL));
        // Open i/o channel to console screen
        hconsole=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        // Make sure we are in 80x25
        SetConsoleScreenBufferSize(hconsole,console_size);  
        // Get details for console screen
        GetConsoleScreenBufferInfo(hconsole,&con_info);
    } // end Init_Graphics
    
    ///////////////////////////
    
    inline void Set_Color(int fcolor, int bcolor=0)
    {
        // This function sets the color of the console output
        SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) | fcolor));
    } // End Set_Color
    
    ///////////////////////////
    
    inline void Draw_String(int x, int y , char *string)
    {
        // This function draws a string at the give x,y
        COORD cursor_pos; // used to pass coords
        // set printing position
        cursor_pos.X = x;
        cursor_pos.Y = y;
        SetConsoleCursorPosition(hconsole,cursor_pos);
        // Print the string in current color
        printf("%s",string);
    } // End Draw_String
    
    ///////////////////////////
    
    inline void Clear_Screen(void)
    {
        // This function clears the screen
        // Set color to white on black
        Set_Color(15,0);
        // clear the screen
        for (int index=0; index<=25; index++)
            Draw_String(0, SCROLL_POS,"\n");
    } // End Clear_Screen
    
    // MAIN GAME LOOP /////////
    
    int main(int)
    {
        char key;              // Player input Data
        int player_x = 40;  
        int player_y = 50;   // Players X/Y position
        // SECTION: initialization
        // Set up the console text graphics system
        Init_Graphics();
        // Clear the screen
        Clear_Screen();
        // SECTION: main event loop, this is where all the action
        // takes place, (the general loop is erase-move-draw)
        while(game_running)
        {
            // SECTION: erase all objects or clear screen
            // nothing to erase in our case
            // SECTION: get player input
            if (kbhit())
            {
                    // Get keyboard data, and filter it
                    key = toupper(getch());
                    // Check if player is trying to exit, if so, exit
                    if (key=='Q' || key==27)
                                    game_running=0;
                    // Check if player is moving left
                    if (key=='A')
                                    player_x--;
                    // Check if player is moving right
                    if (key=='S')
                                    player_x++;
                    // Check if player is moving down                
                    if (key=='D')   player_y--;
                    // Check if player is moving up
                    if (key=='U')   player_y++;                
            } // end if
            // SECTION: game logic and futhur processing
            // make sure player stays on screen
            if (++player_x > MAX_X)
                    player_x=MAX_X;
            if (--player_x < 0)
                    player_x=0;
                    
            if (++player_y > 25)
                    player_y=25;
            if (--player_y < 0)
                    player_y=0;                      
    
            // SECTION: draw everything
    
            // draw next star at random position
            Set_Color(3,0);
            Draw_String(rand()%80, SCROLL_POS,".\n");
            // Draw Player
            Set_Color(rand()%15,0);
            Draw_String(player_x,20,"<-*->");
            Draw_String(0,50,"");
            
    
            // SECTION: synchronize to a constant frame rate
    
            Sleep(40);
        } // End while
    
    // SECTION: shutdown and bail
    
    Clear_Screen();
    printf("\nG A M E O V E R \n\n");
    } // End main
    so my final noobish but true ( I guess ) conclusion , : its not possible in this type of game to let it move up and down because evry thing is moving up all the time and because of the random colours of your space ship it renews itself all the time... too bad I though it would be a cool game ..
    I guess they will teach you an other way some further in your book.

    good luck , MystWind
    Last edited by MystWind; 03-06-2005 at 08:35 AM.
    PLay MystWind beta , within two years

  8. #8
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Thanks for all your help everybody.
    I'll just keep messing with it.

    EDIT: why isn't it possible to have it move on the y axis?
    Last edited by DZeek; 03-06-2005 at 11:45 AM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    too add, I think what your book ment by OL, wasn't OL but 0L, standing for 0 (held in a long integer). This is what NULL is normally defined as. Some people actually don't like to use NULL, as they like to use as few macros as possible.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  10. #10
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Does anyone know how I could make it so that the code I used (check the first post) includes the option of going up and down?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Told ya so...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 09-12-2007, 08:12 PM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM
  4. Game Console in OpenGL
    By HellRaiZer in forum Game Programming
    Replies: 2
    Last Post: 02-27-2002, 12:52 AM
  5. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 06:41 PM