Man kunne starte med noget i stil med:
#include <windows.h>
#include <stdio.h>
#include <limits.h>
#include <list>
#include <time.h>
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#ifdef __BORLANDC__
#pragma warn -8030
#pragma warn -8027
#endif
#define MAX_DEPTH 256
#define X_SIZE 33
#define Y_SIZE 24
#define FREQ 50 // 50 tick/sec
HANDLE StdOut, StdIn;
int YOffset;
void GotoXY(int x, int y);
void GetXY(int &x, int &y);
BOOL IsKeyPressed(int Key);
void OutCharXY(int x, int y, char ch, WORD attr);
BOOL KeyHit();
WORD GetChar();
void ShowCaret(BOOL on);
int GetYOffset();
int GetYSize();
int GetXSize();
void ClearScreen();
int Random(int Range)
{
return (rand() >> 3)%Range;
}
class Point
{
public:
Point(int x_, int y_) : x(x_), y(y_)
{}
Point() : x(0), y(0) {}
int x;
int y;
};
std::vector <Point> BoxList;
int main(void)
{
StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
StdIn = GetStdHandle(STD_INPUT_HANDLE);
YOffset = GetYOffset() + 1;
ClearScreen();
ShowCaret(false);
srand(time(0));
unsigned int n;
int CarX = GetXSize()/2;
OutCharXY(CarX, YOffset + GetYSize() - 10, '@', FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Draw
while(!IsKeyPressed(27)) // Hit Esc to exit
{
if(BoxList.size() < 10 && Random(3) == 0)
{
BoxList.push_back(Point(Random(GetXSize()), 0));
}
for(n = 0; n < BoxList.size(); )
{
OutCharXY(BoxList[n].x, BoxList[n].y + YOffset, ' ', FOREGROUND_RED | FOREGROUND_INTENSITY); // Erase
if(++BoxList[n].y < GetYSize())
{
OutCharXY(BoxList[n].x, BoxList[n].y + YOffset, '#', FOREGROUND_RED | FOREGROUND_INTENSITY); // Draw
n++;
}
else
{
BoxList.erase(BoxList.begin() + n);
}
}
if(IsKeyPressed(VK_LEFT) && CarX > 0)
{
OutCharXY(CarX, YOffset + GetYSize() - 10, ' ', FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Erase
CarX--;
OutCharXY(CarX, YOffset + GetYSize() - 10, '@', FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Draw
}
else if(IsKeyPressed(VK_RIGHT) && CarX < GetXSize())
{
OutCharXY(CarX, YOffset + GetYSize() - 10, ' ', FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Erase
CarX++;
OutCharXY(CarX, YOffset + GetYSize() - 10, '@', FOREGROUND_GREEN | FOREGROUND_INTENSITY); // Draw
}
Sleep(100);
}
ShowCaret(true);
return 0;
}
void GotoXY(int x, int y)
{
COORD c;
c.X = (short )x;
c.Y = (short )y;
SetConsoleCursorPosition(StdOut, c);
}
void GetXY(int &x, int &y)
{
CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(StdOut, &ConsoleScreenBufferInfo);
x = ConsoleScreenBufferInfo.dwCursorPosition.X;
y = ConsoleScreenBufferInfo.dwCursorPosition.Y;
}
int GetYOffset()
{
CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(StdOut, &ConsoleScreenBufferInfo);
return ConsoleScreenBufferInfo.srWindow.Top;
}
int GetYSize()
{
CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(StdOut, &ConsoleScreenBufferInfo);
return ConsoleScreenBufferInfo.srWindow.Bottom - ConsoleScreenBufferInfo.srWindow.Top + 1;
}
int GetXSize()
{
CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(StdOut, &ConsoleScreenBufferInfo);
return ConsoleScreenBufferInfo.srWindow.Right - ConsoleScreenBufferInfo.srWindow.Left + 1;
}
WORD GetChar()
{
DWORD NumEventsRead;
INPUT_RECORD InputRecord;
while(1)
{
if(!ReadConsoleInput(StdIn, &InputRecord, 1, &NumEventsRead))
return 0;
if(InputRecord.EventType & KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL &&
InputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU &&
InputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT)
{
return InputRecord.Event.KeyEvent.wVirtualKeyCode;
}
}
}
}
BOOL KeyHit()
{
DWORD NumEvents, NumEventsRead;
INPUT_RECORD *InputRecord;
DWORD n;
GetNumberOfConsoleInputEvents(StdIn, &NumEvents);
InputRecord = (INPUT_RECORD *)malloc(sizeof(INPUT_RECORD)*NumEvents);
PeekConsoleInput(StdIn, InputRecord, NumEvents, &NumEventsRead);
for(n = 0; n < NumEventsRead; n++)
{
if(InputRecord[n].EventType & KEY_EVENT && InputRecord[n].Event.KeyEvent.bKeyDown)
{
if(InputRecord[n].Event.KeyEvent.wVirtualKeyCode != VK_CONTROL &&
InputRecord[n].Event.KeyEvent.wVirtualKeyCode != VK_MENU &&
InputRecord[n].Event.KeyEvent.wVirtualKeyCode != VK_SHIFT)
{
free(InputRecord);
return TRUE;
}
}
}
free(InputRecord);
return FALSE;
}
void OutCharXY(int x, int y, char ch, WORD attr)
{
COORD Coord;
Coord.X = short(x);
Coord.Y = short(y + YOffset);
DWORD NumWritten;
WriteConsoleOutputCharacter(StdOut, &ch, 1, Coord, &NumWritten);
WriteConsoleOutputAttribute(StdOut, &attr, 1, Coord, &NumWritten);
}
BOOL IsKeyPressed(int Key)
{
return GetAsyncKeyState(Key) & 0x8000 ? TRUE : FALSE;
}
void ShowCaret(BOOL on)
{
CONSOLE_CURSOR_INFO ConsoleCursorInfo;
GetConsoleCursorInfo(StdOut, &ConsoleCursorInfo);
ConsoleCursorInfo.bVisible = on;
SetConsoleCursorInfo(StdOut, &ConsoleCursorInfo);
}
void ClearScreen()
{
short i;
CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
GetConsoleScreenBufferInfo(StdOut, &ConsoleScreenBufferInfo);
SMALL_RECT Pos = ConsoleScreenBufferInfo.srWindow;
DWORD D;
for(i = Pos.Top; i <= Pos.Bottom; i++)
{
COORD C;
C.X = Pos.Left;
C.Y = i;
FillConsoleOutputCharacter(StdOut, ' ', Pos.Right - Pos.Left, C, &D);
FillConsoleOutputAttribute(StdOut, 0, Pos.Right - Pos.Left, C, &D);
}
GotoXY(0, 0);
}
Kasserne er de røde #, "bilen" er en grøn @