-
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?
-
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.
-
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?
-
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.
-
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.