Thread: Help with a simple game

  1. #1
    Registered User
    Join Date
    Mar 2004

    Help with a simple game

    I'm trying to learn as I go. I found a simple game in a graphics book that I've been messing with to learn about graphics. The game originally put a ship "<--*-->" (that's the ship) on the very top of a window and you could move it from left to right while stars would pass by making it look like it was moving through space.

    I want to move the ship down about 10 spaces to the middle of the console window just to know that I can do it.

    It's a console application. Here is the code.

    The line that makes the ship is:


    Instead of 5, it used to be 0 which placed it at the top. The way I have it now does the job somewhat but there is still "residue" leftover and you can still see the ship behind it as it moves. I want to get rid of the leftover graphic from the ship.

    // PROG2_1.CPP - A simple console based game to illustrate
    // a generic game loop
    // INCLUDES ///////////////////////////////////////////////
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h> 
    #include <conio.h>
    #include <windows.h>
    #include <time.h>
    // DEFINES ////////////////////////////////////////////////
    #define MAX_X        77  // maximum x position 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, 0=done, 1=run
    // FUNCTIONS //////////////////////////////////////////////
    void Init_Graphics(void)
    // this function initializes the console graphics engine
    COORD console_size = {80,25}; // size of console
    // seed the random number generator with time
    // open i/o channel to console screen
    hconsole=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ,
    // make sure we are in 80x25
    // get details for console screen                       
    } // 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) | 
    } // Set_Color
    inline void Draw_String(int x,int y, char *string)
    // this function draws a string at the given x,y
    COORD cursor_pos; // used to pass coords
    // set printing position
    cursor_pos.X = x;
    cursor_pos.Y = y;
    // print the string in current color
    } // end Draw_String
    inline void Clear_Screen(void)
    // this function clears the screen
    // set color to white on black
    // 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;  // player's x position
    // SECTION: initialization
    // set up the console text graphics system
    // clear the screen
    // SECTION: main event loop, this is where all the action  
    // takes place, the general loop is erase-move-draw
         // SECTION: erase all the 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());
            // is player trying to exit, if so exit
            if (key=='Q' || key==27)
               game_running = 0; 
            // is player moving left        
            if (key=='A')
            // is player moving right
            if (key=='S')
            } // end if   
         // SECTION: game logic and further processing
         // make sure player stays on screen 
         if (++player_x > MAX_X)
         if (--player_x < 0)
         // SECTION: draw everything
         // draw next star at random position
         Draw_String(rand()%80, SCROLL_POS, ".\n");
         // draw player 
         // SECTION: synchronize to a constant frame rate
         } // end while
    // SECTION: shutdown and bail
    printf("\nG A M E  O V E R \n\n");
    } // end main

  2. #2
    Registered User
    Join Date
    Feb 2002
    everytime the ship moves, clear the space behind it (IE, draw a bunch of spaces or a tab at the position behind the ship).
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a simple Windows game
    By ejohns85 in forum C Programming
    Replies: 1
    Last Post: 05-22-2009, 12:46 PM
  2. A "guess my number" game, with simple AI
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 10:45 PM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 09:24 PM
  5. Replies: 1
    Last Post: 11-06-2001, 02:15 PM