Wie erstelle ich eine Kollision im Konsolen Spiel ?



  • toll das kann ich auch mit


    oder

    aber es tuts nicht



  • ///////////////////////////////////////////////////////////////////////////////
    #include <windows.h>
    #include <conio.h>
    #include <stdio.h>
    ///////////////////////////////////////////////////////////////////////////////
    #define FELD_BREITE 80
    #define FELD_HOEHE 25
    ///////////////////////////////////////////////////////////////////////////////
    const WandWaagerecht = '-';
    const WandSenkrecht = '|';
    const Freiraum = ' ';
    const Spieler = '0';
    const SpielerExplodiert = 'X'; // BOOM :D
    ///////////////////////////////////////////////////////////////////////////////
    HANDLE hOut, hIn;
    bool Wand [ FELD_BREITE ] [ FELD_HOEHE ]; // Alle Elemente werden automatisch beim Programmstart mit false initialisiert.
    int SpielerX, SpielerY;
    ///////////////////////////////////////////////////////////////////////////////
    
    void GehZuXY ( int x, int y )
    {
        COORD pos = { x, y };
        SetConsoleCursorPosition( hOut, pos );
    }
    
    void InitialisiereConsole( const char* title )
    {
        if ( ( hIn = GetStdHandle(STD_INPUT_HANDLE) ) == INVALID_HANDLE_VALUE )
    	{
            puts ( "GetStdHandle failed." );
    		exit (1);
    	}
        if ( ( hOut = GetStdHandle(STD_OUTPUT_HANDLE) ) == INVALID_HANDLE_VALUE )
    	{
            puts ( "GetStdHandle failed." );
    		exit (1);
    	}
        SetConsoleTitle( title );
    }    
    
    bool IstWand ( int x, int y )
    {
    	return	Wand [ x ] [ y ];
    }
    
    void ZeichneElement ( int element, int x, int y )
    {
    	GehZuXY ( x, y );
    	putchar ( element );
    }
    
    void WandZeichnen () 
    {
    	int x, y;
    
    	for ( x = 10, y = 10; x <= 30; x++ )
    	{
    		ZeichneElement ( WandWaagerecht, x, y );
    		Wand [ x ] [ y ] = true;
    	}
    
    	for ( x = 31, y = 10; y <= 20; y++ )
    	{
    		ZeichneElement ( WandSenkrecht, x, y );
    		Wand [ x ] [ y ] = true;
    	}
    }
    
    bool PositionGueltig ( int x, int y )
    {
    	return x >= 0 && x < FELD_BREITE && y >= 0 && y < FELD_HOEHE;
    }
    
    void Boom ( int x, int y ) // Just another boom
    {
    	GehZuXY ( x, y );
    	ZeichneElement ( SpielerExplodiert, x, y );
    	GehZuXY ( 0, 0 );
    	exit ( 0 );
    }
    
    void SetzeSpieler ( int vonX, int vonY, int nachX, int nachY )
    {
    	if ( ! PositionGueltig ( vonX, vonY ) )
    		return;
    
    	if ( ! PositionGueltig ( nachX, nachY ) )
    		return;
    
    	if ( IstWand ( nachX, nachY ) )
    	{
    		ZeichneElement ( Freiraum, vonX, vonY );
    		Boom ( nachX, nachY );
    	}
    
    	ZeichneElement ( Freiraum, vonX, vonY );
    	ZeichneElement ( Spieler, nachX, nachY );
    	SpielerX = nachX;
    	SpielerY = nachY;
    
    }
    
    void Hauptschleife ()
    {
    	int c;
    	while ( 1 )
    	{
    		c = getch ();
    
    		switch ( c )
    		{
    			case 'w':
    				SetzeSpieler ( SpielerX, SpielerY, SpielerX, SpielerY - 1 );
    			break;
    
    			case 'a':
    				SetzeSpieler ( SpielerX, SpielerY, SpielerX - 1, SpielerY );
    			break;
    
    			case 's':
    				SetzeSpieler ( SpielerX, SpielerY, SpielerX, SpielerY + 1 );
    			break;
    
    			case 'd':
    				SetzeSpieler ( SpielerX, SpielerY, SpielerX + 1, SpielerY );
    			break;
    		}
    	}
    }
    
    void CursorAnzeige ( bool bZeigCursor )
    {
    	CONSOLE_CURSOR_INFO ci = {0};
    
        if ( ! GetConsoleCursorInfo( hOut, &ci ) )
    	{
            puts ( "GetConsoleCursorInfo failed." );
    		exit (1);
    	}
    
        ci.bVisible = bZeigCursor;
    
        if ( ! SetConsoleCursorInfo( hOut, &ci ) )
    	{
    		puts ( "SetConsoleCursorInfo failed." );
    		exit (1);
    	}
    }
    
    int main( void )
    {
    	int startX = 12, startY = 12;
    	SpielerX = startX, SpielerY = startY;
    
    	InitialisiereConsole( "Boom Second Movement" );
    	CursorAnzeige ( 0 );
    	WandZeichnen ();
    
    	GehZuXY ( startX, startY );
    	putchar ( Spieler );
    	Hauptschleife ();
    	return 0;
    }
    


  • ich will aber nich den ganzen code ändern 😞 und ausserdem hat der irgentwas mit const ...



  • *r0fl*



  • ASO - ich habs - ich hatte da was immer auf c++ gestellt aber musste auf c

    jetzt tuts 👍 🕶



  • Herzlichen Glückwunsch!



  • der code ist einfach nur 👎 vool spagetticode und auch so - übels komplizirt

    kann man denn nicht

    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;
    }
    
    void Boom()
    {
     Beep(500,100);
     Beep(1000,100);
    }
    

    irgentwie auf mein code übertragen 🙄 ???



  • Ich glaube einfach mal nur du hast keine Ahnung von C++? Solltest du evtl. mal nachholen.

    Gruß Tobi.



  • Dein spagetti -Code ist vool schwer aber ich will nix sagen , weilich es nich tbeser kann ich guck nochmal und dann werd ich mich melden 😉 ciu und danke nochmal



  • warum tuts das denn nicht

    if ( x++ == Feld[5][5]) {Beep(300,100);}
    

    oder

    if ( Position[1]++ == Feld[5][5]) {Beep(300,100);}
    

    oder

    if( Feld[Position[1]-1][Position[0]]== Feld[5][5]) {Beep(300,100);}
    

    🙄 🙄 🙄
    ???


Anmelden zum Antworten