kleine Bugs in meinem Spielchen, wie werd ich sie los????



  • hallo, ich hatte mal die freizeit und habe nen kleines Game programmiert soweit meine kenntnisse gereicht hatten habe ich sie denke ich auch genutzt. doch ist das konsolenspielchen noch leicht verbugt, es soll ein kleines autorennspiel darstellen wo die autos von oben kommen und der spieler den ihm entgegen kommenden autos ausweichen soll

    1. die autos werden per zufall auf einen bestimmten raum der x-Koordinate gestzt doch manchmal sind sie ausserhalb des spielfeldes und ich weiss nicht wie ich den raum der zufalls zahlen begrenzen kann auf zb 3 - 21.

    2. manch mal ist gleich bei programm start das model des gegner autos an das model des plazercars dran gesetzt d.h. also es passiert nix.

    naja ich glaube sonst sind mir keine weiteren bugs bekannt. hier habt ihr erst mal den quellcode, einfach kompilieren und GO!, waer toll wenn ihr mir sagt was ihr davon so haltet 🙂 THANX !!!

    #include <conio.h>
    #include <windows.h>
    #include <iostream>
        using namespace std;
    
    int X(22);  //breite
    int Y(24);  //laenge
    int car_x(7), car_y(20);
    int enemy_x(0),enemy_y(3);
    int Key;
    int punkte(0);
    int level(0);
    int Speed(150);
    
    int Field()
    {
    
        for(int y=3;y<Y;y++)            //links
        {
            gotoxy(2,y);
            cprintf("%c", 186);
        }
        for(int y=3;y<Y;y++)            // rechts
        {
            gotoxy(23,y);
            cprintf("%c", 186);
        }
        for(int x=3;x<=X;x++)          // oben
        {
            gotoxy(x,2);
            cprintf("%c", 205);
        }
        for(int x=3;x<=X;x++)          // unten
        {
            gotoxy(x,24);
            cprintf("%c", 205);
        }
        gotoxy(2,2);                   // links oben
        cprintf("%c", 201);
        gotoxy(2,24);                  // links unten
        cprintf("%c", 200);
        gotoxy(23,2);                   // rechts oben
        cprintf("%c", 187);
        gotoxy(23,24);                  // rechts unten
        cprintf("%c", 188);
    
      return 0;
    }
    
    int Car()
    {
        gotoxy(car_x, car_y);
        cprintf(" %c", 203);
        gotoxy(car_x, car_y+1);
        cprintf("%c", 177);cprintf("%c", 219);cprintf("%c", 177);
        gotoxy(car_x, car_y+2);
        cprintf(" %c", 219);
        gotoxy(car_x, car_y+3);
        cprintf("%c", 177);cprintf("%c", 219);cprintf("%c", 177);
        return 0;
    }
    
    void Keyboard()
    {
        if(kbhit())
        {
            Key = getch();
    
            switch (Key)
            {
                case 'a': car_x--; break;
                case 'd': car_x++; break;
                case 27: exit(1);
                case 'p':
                int kkey;
                bool pause = true;
                while(pause)
                {
                    gotoxy(11,10);
                    cprintf("PAUSE");
                    if(kbhit())
                    {
                        kkey = getch();
    
                        switch(kkey)
                        {
                            case 'p': pause = false; break;
                        }
                    }
    
                } break;
            }
        }
        if(car_x == 2)
        {
            car_x = 3;
        }
        if(car_x == 21)
        {
            car_x = 20;
        }
    
    }
    
    int Enemys()
    {
        srand(time(0));
        punkte += 5;
        if(enemy_y == 3)
        {
            punkte+=50;
            enemy_x = rand() % 21;
        }
    
        gotoxy(enemy_x, enemy_y);
        cprintf("%c", 177);cprintf("%c", 219);cprintf("%c", 177);
        gotoxy(enemy_x, enemy_y+1);
        cprintf(" %c", 219);
        gotoxy(enemy_x,enemy_y+2);
        cprintf("%c", 177);cprintf("%c", 219);cprintf("%c", 177);
        gotoxy(enemy_x,enemy_y+3);
        cprintf(" %c", 202);
        Sleep(Speed);
        enemy_y++;
        if(enemy_y == 22)
        {
            enemy_y = 3;
        }
    
      return 0;
    }
    
    int crash()
    {
        if(car_x == enemy_x)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x == enemy_x+1)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x+1 == enemy_x)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
        if(car_x == enemy_x+2)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x+2 == enemy_x)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x+2 == enemy_x)
        {
            if(car_y+2 == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
        return 0;
    }
    
    int Stats()
    {
        for(int x=26;x<41;x++)
        {
            gotoxy(x,11);
            cprintf("%c", 205);
        }
        for(int x=26;x<41;x++)
        {
            gotoxy(x,17);
            cprintf("%c", 205);
        }
    
        for(int y=12;y<17;y++)
        {
            gotoxy(25,y);
            cprintf("%c", 186);
        }
        for(int y=12;y<17;y++)
        {
            gotoxy(41,y);
            cprintf("%c", 186);
        }
        gotoxy(25,11);
        cprintf("%c",201);
        gotoxy(41,11);
        cprintf("%c",187);
        gotoxy(25,17);
        cprintf("%c",200);
        gotoxy(41,17);
        cprintf("%c",188);
    
        gotoxy(27,13);
        cprintf("Punkte: %d", punkte);
        if(punkte % 1250 == 0)
        {
            Speed-= 10;
            if(Speed == 0)
            {
                Speed = 10;
            }
            level+= 1;
        }
    
        gotoxy(27,15);
        cprintf("Level : %d", level);
        return 0;
    }
    
    int Help()
    {
        for(int x=26;x<41;x++)
        {
            gotoxy(x,2);
            cprintf("%c", 205);
        }
        for(int x=26;x<41;x++)
        {
            gotoxy(x,9);
            cprintf("%c", 205);
        }
    
        for(int y=3;y<9;y++)
        {
            gotoxy(25,y);
            cprintf("%c", 186);
        }
        for(int y=3;y<9;y++)
        {
            gotoxy(41,y);
            cprintf("%c", 186);
        }
        gotoxy(25,2);
        cprintf("%c",201);
        gotoxy(41,2);
        cprintf("%c",187);
        gotoxy(25,9);
        cprintf("%c",200);
        gotoxy(41,9);
        cprintf("%c",188);
    
        gotoxy(27,3);
        cprintf("Steuerung:");
        gotoxy(27,5);
        cprintf("A   - LINKS");
        gotoxy(27,6);
        cprintf("D   - RECHTS");
        gotoxy(27,7);
        cprintf("P   - PAUSE");
        gotoxy(27,8);
        cprintf("Esc - BEENDEN");
    
        return 0;
    }
    
    int Game_over()
    {
        clrscr();
        bool over = true;
        while(over)
        {
            gotoxy(1,10);
            cprintf("        >>> G.A.M.E - O.V.E.R <<<        ");
            gotoxy(1,11);
            cprintf("                                         ");
            gotoxy(1,12);
            cprintf("                                         ");
            gotoxy(1,13);
            cprintf("             - YOUR SCORE -              ");
            gotoxy(1,15);
            cprintf("                  %d",punkte);
    
            if(kbhit())
            {
                Key = getch();
                switch(Key)
                {
                    case 13: over = false; break;
                }
            }
            Sleep(100);
        }
        return 0;
    }
    
    main()
    {
        bool play = true;
        while(play)
        {
    
            _setcursortype(_NOCURSOR);
            system("mode con COLS=42 LINES=25");
            SetConsoleTitle(" X-Car Race :) ");
    
            Field();
            Help();
            Stats();
            Car();
            Keyboard();
            Enemys();
            if(crash()) play = false;
        }
        Game_over();
        return 0;
    }
    


  • Ich hab' mal drübergewurschtelt:

    #include <iostream>
    
    #include <ctime> // fehlt für time( );
    #include <cstdio> // für putchar( );
    
    #include <windows.h>
    #include <conio.h> // für getch( ); kbhit( );
    
    using namespace std;
    
    // Eine conio.h unabhängige show_cursor( show ) welche nur noch von windows.h abhängt:
    
    void show_cursor( bool show )
    {
        static CONSOLE_CURSOR_INFO cci;
        GetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
        cci.bVisible = show;
        SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
    }
    
    // Eine conio.h unabängige gotoxy( x, y ) welche nur noch von windows.h abhängt:
    
    void gotoxy( int x, int y )
    {
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), pos );
    }
    
    // Eine conio.h unabängige clrscr( ) welche nur noch von windows.h abhängt:
    
    void clrscr( )
    {
        void *con_out = GetStdHandle( STD_OUTPUT_HANDLE );
    
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo( con_out, &csbi );
    
        COORD pos;
        memset( &pos, 0, sizeof( COORD ) );
    
        unsigned long chars_written;
        FillConsoleOutputCharacter( con_out, ' ', csbi.dwSize.X	* csbi.dwSize.Y, pos, &chars_written );
    
        SetConsoleCursorPosition( con_out, pos );
    }
    
    /*
    int X(22);  //breite
    int Y(24);  //laenge
    int car_x(7), car_y(20);
    int enemy_x(0),enemy_y(3);
    int Key;
    int punkte(0);
    int level(0);
    int Speed(150);
    
    diese Initialisierungen sind bei einfachen Variablen eigentlich nicht gebräuchlich.
    Globale Variablen sollten vermieden werden!
    */
    
    // int Field( )
    //
    // welchen integer sollte Field denn (sinnvoll) zurückgeben!?
    // selbstsprechende Funktionsnamen:
    //
    void draw_field( int left, int top, int right, int bottom )
    {
        // warum doppelt gemoppelt:
    
        for(int y = top; y <= bottom; y++ ) {
    
            gotoxy( left, y );
            putchar( 186 );
            gotoxy( right, y );
            putchar( 186 );
        }
    
        for(int x = left; x <= right; x++ ) {
    
            gotoxy( x, top );
            putchar( 205 );
            gotoxy( x, bottom );
            putchar( 205 );
        }
    
        gotoxy( left, top );
        putchar( 201 );
        gotoxy( left, bottom );
        putchar( 200 );
        gotoxy( right, top );
        putchar( 187 );
        gotoxy( right, bottom );
        putchar( 188 );
    }
    
    void update_car( int car_pos )
    {
        const char car[ 4 ][ 5 ] = {
            {  32,  32,  32,  32,  32 },
            {  32,  32, 203,  32,  32 },
            {  32, 177, 219, 177,  32 },
            {  32, 177, 219, 177,  32 }
        };
    
        for( int y = 0; y < 4; ++y ) {
    
            for( int x = 0; x < 5; ++x ) {
    
                if( !( ( car_pos == 3 ) && ( x == 0 ) ) &&
                    !( ( car_pos == 19 ) && ( x == 4 ) ) ) {
    
                    gotoxy( car_pos + x - 2, y + 19 );
                    putchar( car[ y ][ x ] );
                }
            }
        }
    }
    
    // hier wäre das erste mal ein Rückgabewert um der do-while( )-Schleife in main( )
    // zu signalisieren, dass sie sich beenden soll.
    //
    bool keyboard( int &car_x )
    {
        int key;
    
        if( kbhit( ) ) {
    
            key = getch();
    
        switch( key ) {
    
            case 'A':
            case 'a':
    
                if( car_x > 3 )
                    --car_x;
    
                break;
    
            case 'D':
            case 'd':
    
                if( car_x < 19 )
                    ++car_x;
    
                break;
    
            case 27:
    
                return false;;
    
            case 'P':
            case 'p':
    
                /*
                int kkey;
                bool pause = true;
    
                while(pause)
                {
                    gotoxy(11,10);
                    printf("PAUSE");
                    if(kbhit())
                    {
                        kkey = getch();
    
                            switch(kkey)
                            {
                                case 'p': pause = false; break;
                            }
                        }
                    }
                    break;
    
                    umständlich, einfacher:
                    */
    
                    gotoxy( 9, 10 );
                    printf( "PAUSE" );
    
                    do {
    
                        key = getch( );
    
                    } while( key != 'p' && key != 'P' );
    
                    gotoxy( 9, 10 );
                    printf( "     " );
                    break;
            }
        }
    
        return true;
    }
    
    // int Enemys( )
    //
    // welchen integer sollte Field denn (sinnvoll) zurückgeben!?
    //
    void update_enemy( int &enemy_x, int &enemy_y, int &speed, int &score )
    {
        // srand(time(0));
        // ^
        // den Zufallsgenerator nur einmal beim Programmstart initialisieren!
    
        score += 5;
    
        const char enemy[ 4 ][ 3 ] = {
            {  32, 202,  32 },
            { 177, 219, 177 },
            { 177, 219, 177 },
            {  32,  32,  32 }
        };
    
        for( int y = 0; y < 4; ++y ) {
    
            for( int x = 0; x < 3; ++x ) {
    
                if( !( ( enemy_y == 4 ) && ( y == 3 ) ) ) {
    
                    if( enemy_y - y < 23 ) {
    
                        gotoxy( enemy_x + x - 1, enemy_y - y );
                        putchar( enemy[ y ][ x ] );
                    }
                }
            }
        }
    
        /*
        gotoxy(enemy_x, enemy_y);
        printf("%c", 177);printf("%c", 219);printf("%c", 177);
        gotoxy(enemy_x, enemy_y+1);
        printf(" %c", 219);
        gotoxy(enemy_x,enemy_y+2);
        printf("%c", 177);printf("%c", 219);printf("%c", 177);
        gotoxy(enemy_x,enemy_y+3);
        printf(" %c", 202);
    
        */
    
        Sleep( speed );
    
        if( enemy_y == 25 ) {
    
            enemy_y = 4;
            enemy_x = 4 + static_cast< int >( ( 16.0 * rand( ) / static_cast< float >( RAND_MAX ) ) );
    
        } else {
    
            ++enemy_y;
        }
    }
    
    bool crash( int car_pos, int enemy_x, int enemy_y )
    {
        /*
        if(car_x == enemy_x)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x == enemy_x+1)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x+1 == enemy_x)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x == enemy_x+2)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x+2 == enemy_x)
        {
            if(car_y == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        if(car_x+2 == enemy_x)
        {
            if(car_y+2 == enemy_y+2)
            {
                Beep(700,100);
                return 1;
            }
        }
    
        umständlich
        */
    
        if( ( enemy_y == 21 ) && enemy_x >= ( car_pos - 2 ) && enemy_x <= ( car_pos + 2 ) ) {
    
            Beep(700,100);
            return true;
        }
    
        return false;
    
    }
    
    // int Stats( )
    //
    // welchen integer sollte Stats( ) denn (sinnvoll) zurückgeben!?
    // selbstsprechende Funktionsnamen:
    //
    void draw_stats()
    {
        draw_field( 23, 11, 40, 17 );
    
        gotoxy( 26, 13 );
        printf( "Punkte :    0" );
    
        gotoxy( 26, 15 );
        printf( "Level  :    0" );
    }
    
    void update_stats( int score, int &level, int &speed )
    {
        if( !( score % 1250 ) ) {
    
            speed = ( ( speed - 10 ) == 0 ) ? 10 : speed - 10;
            ++level;
        }
    
        gotoxy( 35, 13 );
        printf( "%4i", score );
    
        gotoxy( 35, 15 );
        printf( "%4i", level );
    }
    
    // int Help( )
    //
    // welchen integer sollte Help( ) denn (sinnvoll) zurückgeben!?
    // selbstsprechende Funktionsnamen:
    //
    void draw_help()
    {
        draw_field( 23, 1, 40, 9 );
    
        gotoxy( 25, 3 );
        printf( "Steuerung:" );
        gotoxy( 25, 5 );
        printf( "A   - LINKS" );
        gotoxy( 25, 6 );
        printf( "D   - RECHTS" );
        gotoxy( 25, 7 );
        printf("P   - PAUSE" );
        gotoxy( 25, 8 );
        printf("Esc - BEENDEN" );
    }
    
    // int Game_over( )
    //
    // welchen integer sollte Game_over( ) denn (sinnvoll) zurückgeben!?
    //
    void game_over( int score )
    {
        clrscr();
    
        // bool over = true;
        // while(over)
        // {
        // ^
        // überflüssig!
    
            gotoxy(1,10);
            printf("        >>> G.A.M.E - O.V.E.R <<<        ");
    
            /*
            gotoxy(1,11);
            printf("                                         ");
            gotoxy(1,12);
            printf("                                         ");
    
            wesshalb gibst du Leerzeilen aus?        
            */
    
            gotoxy(1,13);
            printf("             - YOUR SCORE -              ");
            gotoxy(1,15);
            printf("                  %d", score );
    
            /*
            if( kbhit( ) )
            {
                switch( getch( ) )
                {
                    case 13: over = false; break;
                }
            }
            Sleep(100);
        }
        */
    
        while( getch( ) != 13 )
            ;
    }
    
    // main()
    //
    // Standard ist:
    int main( )
    {	
        int score = 0, level = 0, speed = 150;
        int enemy_x = 7, enemy_y = 4;
        int car_pos = 7;
    
        srand( static_cast< unsigned int >( time( 0 ) ) );
    
        system( "mode con COLS=42 LINES=25" );
        SetConsoleTitle( "X-Car Race :)" );
        show_cursor( false );
    
        draw_field( 1, 1, 21, 23 );
        draw_help( );
        draw_stats( );
    
        bool play = true;
    
        // while(play)
        //
        do {
            // _setcursortype(_NOCURSOR);
            // ^
            // wenn, dann am Programmbeginn!
            //
            // system("mode con COLS=42 LINES=25");
            // SetConsoleTitle("X-Car Race :)");
            // ^
            // beides nur einmal zu Beginn!
    
            // Field();
    
            update_stats( score, level, speed );
            update_car( car_pos );
            play = keyboard( car_pos );
            update_enemy( enemy_x, enemy_y, speed, score );
    
            // if(crash()) play = false;
    
        } while( !crash( car_pos, enemy_x, enemy_y ) && play );
    
        game_over( score );
        show_cursor( true );
        clrscr( );
    
        // return 0;
        // ^
        // am ende von main( ) mit 0 überflüssig.
    }
    

    Kurz und bündig - ohne Kommentare:

    #include <iostream>
    
    #include <ctime>
    #include <cstdio>
    
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    void show_cursor( bool show );
    void gotoxy( int x, int y );
    void clrscr( );
    
    void draw_field( int left, int top, int right, int bottom );
    void draw_help( );
    void draw_stats( );
    void update_stats( int score, int &level, int &speed );
    
    bool keyboard( int &car_x );
    
    void update_car( int car_pos );
    void update_enemy( int &enemy_x, int &enemy_y, int &speed, int &score );
    
    bool crash( int car_pos, int enemy_x, int enemy_y );
    void game_over( int score );
    
    int main( )
    {	
        int score = 0, level = 0, speed = 150;
        int enemy_x = 7, enemy_y = 4;
        int car_pos = 7;
    
        srand( static_cast< unsigned int >( time( 0 ) ) );
    
        system( "mode con COLS=42 LINES=25" );
        SetConsoleTitle( "X-Car Race :)" );
        show_cursor( false );
    
        draw_field( 1, 1, 21, 23 );
        draw_help( );
        draw_stats( );
    
        bool play = true;
    
        do {
            update_stats( score, level, speed );
            update_car( car_pos );
            play = keyboard( car_pos );
            update_enemy( enemy_x, enemy_y, speed, score );
    
        } while( !crash( car_pos, enemy_x, enemy_y ) && play );
    
        game_over( score );
        show_cursor( true );
        clrscr( );
    }
    
    void show_cursor( bool show )
    {
        static CONSOLE_CURSOR_INFO cci;
        GetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
        cci.bVisible = show;
        SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
    }
    
    void gotoxy( int x, int y )
    {
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), pos );
    }
    
    void clrscr( )
    {
        void *con_out = GetStdHandle( STD_OUTPUT_HANDLE );
    
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        GetConsoleScreenBufferInfo( con_out, &csbi );
    
        COORD pos;
        memset( &pos, 0, sizeof( COORD ) );
    
        unsigned long chars_written;
        FillConsoleOutputCharacter( con_out, ' ', csbi.dwSize.X	* csbi.dwSize.Y, pos, &chars_written );
    
        SetConsoleCursorPosition( con_out, pos );
    }
    
    void draw_field( int left, int top, int right, int bottom )
    {
        for(int y = top; y <= bottom; y++ ) {
    
            gotoxy( left, y );
            putchar( 186 );
            gotoxy( right, y );
            putchar( 186 );
        }
    
        for(int x = left; x <= right; x++ ) {
    
            gotoxy( x, top );
            putchar( 205 );
            gotoxy( x, bottom );
            putchar( 205 );
        }
    
        gotoxy( left, top );
        putchar( 201 );
        gotoxy( left, bottom );
        putchar( 200 );
        gotoxy( right, top );
        putchar( 187 );
        gotoxy( right, bottom );
        putchar( 188 );
    }
    
    void draw_help()
    {
        draw_field( 23, 1, 40, 9 );
    
        gotoxy( 25, 3 );
        printf( "Steuerung:" );
        gotoxy( 25, 5 );
        printf( "A   - LINKS" );
        gotoxy( 25, 6 );
        printf( "D   - RECHTS" );
        gotoxy( 25, 7 );
        printf("P   - PAUSE" );
        gotoxy( 25, 8 );
        printf("Esc - BEENDEN" );
    }
    
    void draw_stats()
    {
        draw_field( 23, 11, 40, 17 );
    
        gotoxy( 26, 13 );
        printf( "Punkte :    0" );
        gotoxy( 26, 15 );
        printf( "Level  :    0" );
    }
    
    void update_stats( int score, int &level, int &speed )
    {
        if( !( score % 1250 ) ) {
    
            speed = ( ( speed - 10 ) == 0 ) ? 10 : speed - 10;
            ++level;
        }
    
        gotoxy( 35, 13 );
        printf( "%4i", score );
        gotoxy( 35, 15 );
        printf( "%4i", level );
    }
    
    bool keyboard( int &car_x )
    {
        int key;
    
        if( kbhit( ) ) {
    
            key = getch();
    
        switch( key ) {
    
            case 'A':
            case 'a':
    
                if( car_x > 3 )
                    --car_x;
    
                break;
    
            case 'D':
            case 'd':
    
                if( car_x < 19 )
                    ++car_x;
    
                break;
    
            case 27:
    
                return false;;
    
            case 'P':
            case 'p':
    
                gotoxy( 9, 10 );
                printf( "PAUSE" );
    
                do key = getch( );
                while( key != 'p' && key != 'P' );
    
                gotoxy( 9, 10 );
                printf( "     " );
                break;
            }
        }
    
        return true;
    }
    
    void update_car( int car_pos )
    {
        const char car[ 4 ][ 5 ] = {
            {  32,  32,  32,  32,  32 },
            {  32,  32, 203,  32,  32 },
            {  32, 177, 219, 177,  32 },
            {  32, 177, 219, 177,  32 }
        };
    
        for( int y = 0; y < 4; ++y ) {
    
            for( int x = 0; x < 5; ++x ) {
    
                if( !( ( car_pos == 3 ) && ( x == 0 ) ) &&
                    !( ( car_pos == 19 ) && ( x == 4 ) ) ) {
    
                    gotoxy( car_pos + x - 2, y + 19 );
                    putchar( car[ y ][ x ] );
                }
            }
        }
    }
    
    void update_enemy( int &enemy_x, int &enemy_y, int &speed, int &score )
    {
        score += 5;
    
        const char enemy[ 4 ][ 3 ] = {
            {  32, 202,  32 },
            { 177, 219, 177 },
            { 177, 219, 177 },
            {  32,  32,  32 }
        };
    
        for( int y = 0; y < 4; ++y ) {
    
            for( int x = 0; x < 3; ++x ) {
    
                if( !( ( enemy_y == 4 ) && ( y == 3 ) ) ) {
    
                    if( enemy_y - y < 23 ) {
    
                        gotoxy( enemy_x + x - 1, enemy_y - y );
                        putchar( enemy[ y ][ x ] );
                    }
                }
            }
        }
    
        Sleep( speed );
    
        if( enemy_y == 25 ) {
    
            enemy_y = 4;
            enemy_x = 4 + static_cast< int >( ( 16.0 * rand( ) / static_cast< float >( RAND_MAX ) ) );
    
        } else {
    
            ++enemy_y;
        }
    }
    
    bool crash( int car_pos, int enemy_x, int enemy_y )
    {
        if( ( enemy_y == 21 ) && enemy_x >= ( car_pos - 2 ) && enemy_x <= ( car_pos + 2 ) ) {
    
            Beep(700,100);
            return true;
        }
    
        return false;
    }
    
    void game_over( int score )
    {
        clrscr();
        gotoxy(1,10);
        printf("        >>> G.A.M.E - O.V.E.R <<<        ");
        gotoxy(1,13);
        printf("             - YOUR SCORE -              ");
        gotoxy(1,15);
        printf("                  %d", score );
    
        while( getch( ) != 13 )
            ;
    }
    

    Greetz, Swordfish



  • ähm jo... wie gesagt sword ich habe den code nur soweit geschrieben wie es auch mein "skill" zu lies. oO von deinem code hab ich keinen blassen was der macht.... aber vieleicht kannst du dich ja auf meinen niederwertigen code einlassen und mir zeigen wo der fehler is damit die bugs weg gehen?!



  • T0bi schrieb:

    [...] von deinem code hab ich keinen blassen was der macht.... [...]

    genau das selbe, wie deiner (bis auf den klitzekleinen Unterschied, daß die "Gegner" auf unterschiedlichen enemy_x-Werten daherkommen. nicht das gesamte Spiel ständig neu gezeichnet wird und die Gegner lässig aus dem Rand hervorkommen bzw. wieder in diesem verschwinden).

    Alles was du zu meinem Code an "Skills" (mann ich hasse dieses Wort) brauchst sind wahrlich nur Grundlagen von C++:

    • include-files
    • namespaces
    • Funktionen
    • Ganzzahlige Variabletypen
    • Arrays 😉
    • if-Verzweigungen
    • den trinären Operaor ? :
    • while( )- bzw. do-while( )-Schleifen
    • for( )-Schleifen
    • casts 😉
    • Referenzen 😉

    😉 hast du nicht verwendet

    Da du alles andere bereits selbst verwendet hast stufe ich meinen Code (der Verwendeten Sprachelemente bezüglich) nicht "hochwertiger".

    Vielleicht verrätst du mir, was du daran nicht verstehst?

    Greetz, Swordfish



  • Hallo,

    Swordfish, deine Autos sind echt süß 👍 🙂 :).

    MFG winexec*



  • hi sword du hast schon recht, ich hatte mir deinen quelltext eben noch mal in ruhe betrachtet und naja verstehe eigentlich fdoch des meiste nur evtl. ein zwei dinge nicht... eins zB. "printf( "%4i", score );" ich kenne nur die formate(d,e,s,c,f) was macht i und was tut die 4 davor gibt die die stellen an??? und ich hatte mich noch gefragt wie du es machst das die autos so schoen am rand verschwinden? und vieleicht noch eins kannst du mir zeigen wie ich es evtl. anstellen kann das nicht nur ein auto sondern mehrer erscheinen... bezüglich dies wollte ich das in einem anderen game nutzen um mehrere elemente(pongschläger *g*) mit unterschiedlichen tasten steuern kann, habs immer noch nich geschaft wie das geht. 😕 waer nett.

    PS: @winexec dachte du wolltest mir wegen meinen snake prog helfen *schnief*



  • Wie geil ist das denn? 🙂

    Battlefield 2 wird heut noch deinstalliert!



  • DarthZiu schrieb:

    Wie geil ist das denn? 🙂

    T0bi schrieb:

    Battlefield 2 wird heut noch deinstalliert!

    Tja, ist sicherlich nicht soviel Denkarbeit 😃

    Greetz, Swordfish


Anmelden zum Antworten