Thread: console games

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    console games

    Hello people,

    I set out a couple of years ago learning to program games , and people told me i had to learn console mode c++ first before getting into the pretty graphics and what not. I now near enough know the syntax of console c++ and i would like to feel as if i could access the full capabilities of the language.
    The one ability of the language that is letting me down is to register the keyboard not to enter a string but to control a character around the screen like an arcade game.
    Can anyone point me in the direction of a function that achieves this. For example moving a pacman character or is this only achievable in Windows API?
    Last edited by The Gweech; 10-10-2003 at 06:19 PM.

  2. #2
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    like do u mean getting key presses and the using them to do something, like pressing up to go up or spacebar to shoot? i cant remember off the top of my head but ill look it up then post some code to get u started.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Geo Fry that is exactly what i mean
    So far i have only seen tic-tac-toe and connect four in c++ console mode as far as games are concerned can you do space invaders and tetris or pacman?
    Last edited by The Gweech; 10-10-2003 at 06:47 PM.

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I've got a simple Windows API example here that shows you how to draw a simple ball and move it around using the arrow keys.

    http://www.abdn.ac.uk/~u02cll2/windo...movingball.jpg

    The program can be downloaded here.

    The source code can be viewed in HTML format here.

  5. #5
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    heres some code, availible in the FAQ
    Code:
    #include <windows.h>
    #include <iostream>
    
    bool Keypress(char &Key)
    {
    	INPUT_RECORD Event;
    	DWORD NumberOfEvents, EventsRead, EventCounter;
    
     	GetNumberOfConsoleInputEvents(GetStdHandle(STD_IN
    PUT_HANDLE), &NumberOfEvents);
    
    	if (NumberOfEvents == 0)
    		return false;
    	
    	for (EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++)
    	{
     		PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE),
     &Event, 1, &EventsRead);
    		if ((Event.EventType == KEY_EVENT) && ((Event.Event.KeyEvent.bKeyDown)))
    		{
     			ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE)
    , &Event, 1, &EventsRead);
    			Key = Event.Event.KeyEvent.wVirtualKeyCode;
    			if  (!(FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_
    HANDLE))))
    				exit(0);
    			return true;
    		}
    		else
     			ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE)
    , &Event, 1, &EventsRead);
    	}
    
    	return false;
    }
    
    int main()
    {
    	char key = 0;
    	for (;;)
    	{		
    		if (Keypress(key))
    		{
    			if (key == VK_LEFT)
    				std::cout << "Left\n";
    			if (key == VK_RIGHT)
    				std::cout << "Right\n";
    			if (key == VK_DOWN)
    				std::cout << "Down\n";
    			if (key == VK_UP)
    				std::cout << "Up\n";
    			if (key == VK_ESCAPE)
    				return 0;
    		}
    	}
    	return 0;
    }

    i dont think that pacman or space invaders would be possible in console mode, although an extremely ghetto tetris might be. but i would recommand graphics for those kinds of things.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are console games still fun?
    By DarkMortar in forum Game Programming
    Replies: 12
    Last Post: 05-19-2006, 09:00 AM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM
  4. Console games and movement
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 03-17-2002, 08:07 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM