Spiel erstellen in der Konsole ?
-
Hallo gibt es eine möglichkeit in der konsole nen spiel zu erstellen
also buchstabe mit links oder rechts zu steuern und wenn der mit was anden KOLIDIRT
sollte erstmal nen Beepton kommen und dann halt wie eine wandalso so:
O = spieler
| = wand
_ = wand_______________
| |
| 0 |
|_______________|ein raum mit spieler
ABER WIE GEHT DAS ??????????
-
Deine Wände und den Spieler malerst du mit den entsprechenden Ascii Zeichen in die Konsole rein. Logisch ne ?
Mit ReadConsoleInput fragst du die Tastenereignisse links, rechts, hoch, runter und die Postition des Spielers ab.Für die Kollisionsabfrage machst du dir ein Array:
bool Wall[ConsoleX*ConsoleY];
ConsoleX, ConsoleY ist die Anzahl der Zellen der Konsole.
An den X/Y Positionen, wo eine Wand ist:
Wall[Y*ConsoleX+X] = true;
Wo keine Wand ist:
Wall[Y*ConsoleX+X] = false;
Die Abfrage, ob sich an einer Koordinate eine Wand befindet:
bool IsWall( int x, int y ) { return Wall[ y * ConsoleX + x ]; }
-
kannst du ma n beispiehl phosten ?
-
Jo.
#include <stdio.h> #include <windows.h> HANDLE hOut, hIn; INPUT_RECORD ir; typedef int bool; enum{false, true}; const char* title = " - - Big Brother's Collision Demo - - "; #define ConsoleX 80 #define ConsoleY 25 const int Player = 'O'; const int Background = ' '; // Aktuelle Spielerposition int X, Y; // Puffer, booom :D CHAR_INFO boom [ ConsoleX * ConsoleY ]; // Puffer, Spielfeldkopie. Für Boom CHAR_INFO area [ ConsoleX * ConsoleY ]; bool Wall[ ConsoleX * ConsoleY ]; bool Run; int InitConsole( const char* title ) { if ( ( hIn = GetStdHandle(STD_INPUT_HANDLE) ) == INVALID_HANDLE_VALUE ) return 1; if ( ( hOut = GetStdHandle(STD_OUTPUT_HANDLE) ) == INVALID_HANDLE_VALUE ) return 1; SetConsoleTitle( title ); Run = true; return 0; } void GotoXY ( int x, int y ) { COORD pos = { x, y }; SetConsoleCursorPosition( hOut, pos ); } bool IsWall( int x, int y ) { return Wall[ y * ConsoleX + x ]; } void PrepareBoom() { int i; for ( i=0; i<ConsoleX * ConsoleY; i++ ) boom[i].Attributes = BACKGROUND_RED; } void GameOver() { char* game_over = "Game Over"; int i; int len = strlen(game_over); system("cls"); GotoXY( (ConsoleX/2) - (len/2), ConsoleY/2 ); for ( i=0; i<len; i++ ) { putchar( game_over [i] ); Sleep(200); } Sleep(1000); } void Boom() { int i; SMALL_RECT r = {0,0,ConsoleX-1, ConsoleY-1};// Coordinates of the screenbuffer rectangle. COORD bs = {ConsoleX, ConsoleY}; // Column-row size of source buffer. COORD bc = {0}; // Upper-left cell to write from. ReadConsoleOutput( hOut, area, bs, bc, &r ); // Spielfeld kopieren. for( i=0; i<=3; i++ ) { WriteConsoleOutput( hOut, boom, bs, bc, &r ); Sleep(100); WriteConsoleOutput( hOut, area, bs, bc, &r ); Sleep(100); } Run = false; } bool PositionValid( int X, int Y ) { return X >= 0 && X <= ConsoleX-1 && Y >= 0 && Y <= ConsoleY-1; } void Goto( COORD pos ) { SetConsoleCursorPosition( hOut, pos ); } void MovePlayer( int prev_x, int prev_y, int x, int y ) { GotoXY( prev_x, prev_y ); putchar( Background ); GotoXY( x, y ); putchar( Player ); X=x; Y=y; } void Up() { if ( ! PositionValid ( X, Y-1 ) ) return; if ( IsWall( X, Y-1 ) ) Boom(); else MovePlayer( X, Y, X, Y-1 ); } void Down() { if ( ! PositionValid ( X, Y+1 ) ) return; if ( IsWall( X, Y+1 ) ) Boom(); else MovePlayer( X, Y, X, Y+1 ); } void Left() { if ( ! PositionValid ( X-1, Y ) ) return; if ( IsWall( X-1, Y ) ) Boom(); else MovePlayer( X, Y, X-1, Y ); } void Right() { if ( ! PositionValid ( X+1, Y ) ) return; if ( IsWall( X+1, Y ) ) Boom(); else MovePlayer( X, Y, X+1, Y ); } int ConsoleEvents( int count ) { switch( ir.EventType ) { case KEY_EVENT: { switch ( ir.Event.KeyEvent.wVirtualKeyCode ) { case VK_UP: if( ir.Event.KeyEvent.bKeyDown ) Up(); break; case VK_DOWN: if( ir.Event.KeyEvent.bKeyDown ) Down(); break; case VK_LEFT: if( ir.Event.KeyEvent.bKeyDown ) Left(); break; case VK_RIGHT: if( ir.Event.KeyEvent.bKeyDown ) Right(); break; case VK_ESCAPE: Run = false; break; } } } return 0; } int MainLoop() { int count = 0; while( Run ) { if ( ! ReadConsoleInput( hIn, &ir, 1, &count )) return 1; while( count-- ) { if ( ConsoleEvents(count) ) return 1; } } return 0; } int ShowConsoleCursor( bool show ) { CONSOLE_CURSOR_INFO ci = {0}; if ( ! GetConsoleCursorInfo( hOut, &ci ) ) return 1; ci.bVisible = show; if ( ! SetConsoleCursorInfo( hOut, &ci ) ) return 1; return 0; } void InitWall() { int wall_len = 20; int x = ConsoleX/2 - wall_len/2, y = ConsoleY/2, i = 0; while( i <= wall_len ) { GotoXY( x+i, y ); putchar( 219 ); Wall[ y * ConsoleX + x+i++ ] = true; } } int ConsoleDimension( short x, short y ) { SMALL_RECT windowCorners = {0,0,x-1,y-1}; COORD screenBufSize = {x, y}; if ( ! SetConsoleWindowInfo( hOut, 1, &windowCorners ) ) return 1; if( ! SetConsoleScreenBufferSize( hOut, screenBufSize )) return 1; return 0; } int Init() { if( InitConsole( title ) ) return 1; if( ConsoleDimension( ConsoleX, ConsoleY )) return 1; if ( ShowConsoleCursor( false ) ) return 1; InitWall(); PrepareBoom(); MovePlayer( 0, 0, 20, 20 ); return 0; } int main() { if ( Init() ) return 1; if ( MainLoop() ) return 1; GameOver(); PostMessage( FindWindow( NULL, title ), WM_QUIT, 0, 0 ); return 0; }
-
womit ist das Compliert und gib es irgentwo tutorials zu spiele programmierung in der konsole ???
-
Dumm schrieb:
womit ist das Compliert und gib es irgentwo tutorials zu spiele programmierung in der konsole ???
häh
das sollte jeder handelsübliche c-compiler hinkriegen.
wegen tutorials, was erwartest du da ? freunde dich mit den console functions der winapi an, dann funzt das ohne tuts.
-
Dumm schrieb:
womit ist das Compliert ...
Äääh compiliert ?
Wieso ? Das sollte eigentlich jeder C-Compiler gebacken kriegen.Dumm schrieb:
und gib es irgentwo tutorials zu spiele programmierung in der konsole ???
Z.Zt. habe ich nur diesen Link hier parat, mit vielen Quellcodebeispielen^^
http://www.brackeen.com/vga/index.htmlansonsten, Google kennst du oder `?
-
HI
ich komm damit voll nicht kla ... kannst du mal einfach nur machen das
der einfach nur den Player steuert - keine wand nix einfach das ich den in der
konsole rumlemken kanwere voll net
achja diese seite da basirtz nur auf Pixel und hat fürmich kaum was mit konsolenspiele zutuhn
-
DUMM schrieb:
HI
ich komm damit voll nicht kla ...
wisonich
DUMM schrieb:
achja diese seite da basirtz nur auf Pixel und hat fürmich kaum was mit konsolenspiele zutuhn
kann man doch viel mehr mit machen, als mit den buchstabenzellen^^
hast du dir die beispiele angeguckt ?
die programme laufen alle in der konsole. wenn das nix mit konsolenspiel zu tun hat.#include <stdio.h> #include <windows.h> HANDLE hOut, hIn; INPUT_RECORD ir; typedef int bool; enum{false, true}; const char* title = " - - Big Brothers Player Movement Demo - - "; #define ConsoleX 80 #define ConsoleY 25 const int Player = 'O'; const int Background = ' '; int X, Y; bool Run; int InitConsole( const char* title ) { if ( ( hIn = GetStdHandle(STD_INPUT_HANDLE) ) == INVALID_HANDLE_VALUE ) return 1; if ( ( hOut = GetStdHandle(STD_OUTPUT_HANDLE) ) == INVALID_HANDLE_VALUE ) return 1; SetConsoleTitle( title ); Run = true; return 0; } void GotoXY ( int x, int y ) { COORD pos = { x, y }; SetConsoleCursorPosition( hOut, pos ); } bool PositionValid( int X, int Y ) { return X >= 0 && X <= ConsoleX-1 && Y >= 0 && Y <= ConsoleY-1; } void MovePlayer( int prev_x, int prev_y, int x, int y ) { GotoXY( prev_x, prev_y ); putchar( Background ); GotoXY( x, y ); putchar( Player ); X=x; Y=y; } void Up() { if ( ! PositionValid ( X, Y-1 ) ) return; MovePlayer( X, Y, X, Y-1 ); } void Down() { if ( ! PositionValid ( X, Y+1 ) ) return; MovePlayer( X, Y, X, Y+1 ); } void Left() { if ( ! PositionValid ( X-1, Y ) ) return; MovePlayer( X, Y, X-1, Y ); } void Right() { if ( ! PositionValid ( X+1, Y ) ) return; MovePlayer( X, Y, X+1, Y ); } int ShowConsoleCursor( bool show ) { CONSOLE_CURSOR_INFO ci = {0}; if ( ! GetConsoleCursorInfo( hOut, &ci ) ) return 1; ci.bVisible = show; if ( ! SetConsoleCursorInfo( hOut, &ci ) ) return 1; return 0; } int ConsoleDimension( short x, short y ) { SMALL_RECT windowCorners = {0,0,x-1,y-1}; COORD screenBufSize = {x, y}; if ( ! SetConsoleWindowInfo( hOut, 1, &windowCorners ) ) return 1; if( ! SetConsoleScreenBufferSize( hOut, screenBufSize )) return 1; return 0; } int Init() { if( InitConsole( title ) ) return 1; if( ConsoleDimension( ConsoleX, ConsoleY )) return 1; if ( ShowConsoleCursor( false ) ) return 1; MovePlayer( 0, 0, 20, 20 ); return 0; } void TheEnd() { system("cls"); puts("Good Bye! ;)"); } int ConsoleEvents( int count ) { switch( ir.EventType ) { case KEY_EVENT: { switch ( ir.Event.KeyEvent.wVirtualKeyCode ) { case VK_UP: if( ir.Event.KeyEvent.bKeyDown ) Up(); break; case VK_DOWN: if( ir.Event.KeyEvent.bKeyDown ) Down(); break; case VK_LEFT: if( ir.Event.KeyEvent.bKeyDown ) Left(); break; case VK_RIGHT: if( ir.Event.KeyEvent.bKeyDown ) Right(); break; case VK_ESCAPE: Run = false; TheEnd(); break; } } } return 0; } int MainLoop() { int count = 0; while( Run ) { if ( ! ReadConsoleInput( hIn, &ir, 1, &count )) return 1; while( count-- ) { if ( ConsoleEvents(count) ) return 1; } } return 0; } int main() { if ( Init() ) return 1; if ( MainLoop() ) return 1; return 0; }