Snake 2 Classic



  • Hallo, hab mal den Klassiker Snake 2 umgesetzt. Viel Spass!!!
    V03-
    Update: Speed Handling

    //SNAKE C_C++_ DEV Melatroid
    
    //MADE WITH CODE::BOCKS
    
    //GNU GCC COMPILER
    
    
    #include <iostream>
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    #include <conio.h>
    
    #define frogger	10
    
    
    bool debug=false; //ACTIVATES DEBUG MODUS
    
    
    using namespace std;
    
    
    //LAYOUT WALLS ASCII
    char hl = 205, vl = 186, xx = 206,
    tl = 201, gl = 200, tr = 187,
    gr = 188, xt = 203, xg = 202,
    xl = 204, xr = 185,
    apple =3, body=207 , head=229;
    
       // WINDOWSIZE 30x30
    char m[28][15];//Matrix Array for Gamefield NUM= X,NUM= Y
    char c; //KEYS
    
    //PROTOTYPEN
    void welcome();
    void xwritematrix();
    void xprintmatrix();
    void xshowapple();
    void xwallslide();
    void xprintbodymatrix();
    void xshowbody();
    void xwritebodymatrix();
    void copybodycoords();
    void xresetbodymatrix();
    void xshowspecial();
    void textcolor(WORD color);
    void xspeedrules();
    
    //VARIABLEN
     unsigned int speed=500;
        int points=0;
        int bX[200],bY[200];
    
     int  shoX,sveY; //COORDS FOR SNAKE
     int  ahoX,aveY; //START FOR APPLE
     int  ihox,iveY; //SPECIALITEM
        int  bodyparts=0; //COUNT THE BODYPARTS
        int  speccou=0; //COUNT THE RANDOMISING
     bool applebit=false; // COUNT THE APPLES
     bool xwelco=true; //SHOWS WELCOME
    
     char bodymatrix[28][15];//INT BODY
     char restart = 'n';//
     bool gameover = false; //GAMEOVER
     bool flip=false;//FLIP SPEICALITEM
     bool actspec=false;  //ACTIVATE SPECIALITEM
     unsigned int  xmatrix=0,ymatrix=0; //Pointer for Gamefieldcontrol
    
    
    
    
    int main() {
    
    // OPEN CONSOLE IN FORMAT 380x400
    	HWND console = GetConsoleWindow();
    	RECT ConsoleRect;
    	GetWindowRect(console, &ConsoleRect);
    
        MoveWindow(console, 0, 0, 380, 400, TRUE);
    
    srand((unsigned)time(NULL)); // INIT RANDOM GENERATOR
    
    welcome();
    while(restart == 'n')
    {
    
    shoX=13,sveY=7; //SET START SNAKE COORDS
    ahoX=7,aveY=5;//SET APPLE SNAKE COORDS
    ihox=8,iveY=9;//SET SPECIAL SNAKE COORDS
    gameover = false; //RESET GAMOVER
    bodyparts =0;
    speed=500;
    points=0;
    xresetbodymatrix(); //RESET THE BODY MATRIX
    
      while(!gameover)
    {
        if(kbhit()) // IF KEYPINUT
        {
            c = getch(); //CHANGE CHAR
        }
    
        if (c=='a') shoX--; //GO LEFT
        if (c=='d') shoX++; //GO RIGHT
        if (c=='w') sveY--; //GO UP
        if (c=='s') sveY++; //GO DOWN
    
     xwallslide(); // SLIDE WALLS // CREATE SNAKE COORD
     xwritematrix(); // WRITE THINGS IN MATRIX
     copybodycoords(); // COPY COORDS
     xwritebodymatrix();  //SHOW SNAKE BODY
    
     xshowapple(); // CREATE APPLE COORD
     xshowspecial(); //SHOW A SPECIAL ITEM
     xprintmatrix(); // SHOW THE GAMEFIELD
     Sleep(speed); //CHANGE SPEED UP
    
     if(debug)xprintbodymatrix(); //DEBUGMODUS SHOWS BODYMATRIX
    }
    
    cout << "  \n\n         GAMEOVER " << endl;
    cout << "  \n\n     Punktestand: " << points << endl<< endl<< endl;
    cout << "  \n\n     NEUSTART mit -n-" << endl;
    cin >> restart;
    
    }
    	return 0;
    }
    void welcome()
    {
    
    textcolor(frogger);
    if (welcome) {
    
    printf("\n\n\n\n                 _       \n");
    printf("                | |    II    \n");
    printf(" ___ _ __   __ _| | _____       \n");
    printf("/ __| '_ \\ / _` | |// //_\\      \n");
    printf("\\__ \\ | | | (_| |   <  __/      \n");
    printf("|___/_| |_|\\__,_|_|\\_\\___|      \n\n\n");
    printf("            W          \n");
    printf("          A S D        \n\n");
    printf("      by melatroid      \n\n\n");
    
    Sleep (2000);xwelco=false;
        }
    }
    
    void textcolor(WORD color){
        //eSetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY);
    	SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
    }
    
    void xwallslide() //IN FUNKTION XSHOWSNAKE
    {
    
    if (shoX<0)shoX=27;
    else if (shoX==28) shoX=0;
    
    if (sveY<0) sveY=14;
    else if (sveY==15) sveY=0;
    
    }
    
    void xshowspecial()
    {
        int i,j,itrand;
        int shake=false;
    
             if (applebit&&!actspec)
             {itrand = rand() % 30 + 1; if (itrand<10){actspec=true;} else actspec=false;}
    
             if (actspec) {itrand=rand() % 30 + 1; if (itrand < 5) shake=true;else {shake=false;} }
    
            if ((ihox == shoX  && iveY == sveY) || shake) //IF EAT SPECIAL, SET RANDOM FIELD
            {
                ihox = rand() % 27 + 1;
                iveY = rand() % 14 + 1;
                if (!shake) points+=50;      //POINTS UP
            }
            while (bodymatrix [ihox][iveY] == 'b' )//IF APPLE ON A BODY FIELD;CHANGE FIELD
                {
                ihox = rand() % 27 + 1;
                iveY = rand() % 14 + 1;
                }
    
    }
    
    void xspeedrules()
    {
         if (speed <= 500 && speed > 400) speed-=30;
         if (speed <= 400 && speed > 300) speed-=25;
         if (speed <= 300 && speed > 200) speed-=20;
         if (speed <= 200 && speed > 100) speed-=15;
         if (speed <= 100 && speed > 50)  speed-=10;
         if (speed <= 50)  speed -=5;
    }
    
    void xshowapple()
    {
    
        int i,j;
    
            if (ahoX == shoX  && aveY == sveY) //IF EAT APPLE, SET RANDOM FIELD
            {
                ahoX = rand() % 27 + 1;
                aveY = rand() % 14 + 1;
                applebit=true; // APPLEBIT
                bodyparts++;  // IN MORE BODY PART
                xspeedrules(); // CHANGE SPEED PARAMETER
                points+=10;                           //POINTS UP
            }
    
            while (bodymatrix [ahoX][aveY] == 'b' )//IF APPLE ON A BODY FIELD;CHANGE FIELD
                {
                ahoX = rand() % 27 + 1;
                aveY = rand() % 14 + 1;
                }
    }
    
    void copybodycoords()
    {
        int      i; i=bodyparts;//i=1
        int      j; j=i-1;      //j=0
        int      k=0;
    
            for (;k<=bodyparts;k++){
                bX[i] = bX[j];
                bY[i] = bY[j];
                i--;j--;
                }
                bX[0] = shoX;
                bY[0] = sveY;
    }
    void xwritebodymatrix()
    {
        int i,j,k;
            for(j=0;j<15;j++) //WRITE Y FIELD
                {
                for(i=0;i<28;i++)// WRITE X FIELD
                    {
                         bodymatrix[i][j] = '.';
                      for (k=0;k<bodyparts;k++)
                        {
                              if (bodyparts>=(k+1)) {if (bodymatrix[bX[k]][bY[k]]) {bodymatrix[bX[k]][bY[k]] = 'b';}}
                        }
                    }
                }
    }
    
    void xresetbodymatrix()
     {
    
    int i,j;
    
        for(j=0;j<15;j++)// RESET Y FIELD
                {
                for(i=0;i<28;i++)// RESET X FIELD
                    {
                    bodymatrix[i][j] = ' ';
                    }
                }
     }
    
    void xwritematrix()
    {
      xmatrix=0,ymatrix=0;//WRITE FROM ZERO
        int i,j;
        textcolor(frogger);
    
    for(ymatrix=0;ymatrix<15;ymatrix++)
    {
        for(xmatrix=0;xmatrix<28;xmatrix++)
            {
             m[xmatrix][ymatrix] = ' '; //CREATE MATRIX WITH SPACEKEY
            if(bodymatrix[xmatrix][ymatrix] == 'b'){m[xmatrix][ymatrix] = body;}//CHECK BODY FIELDS
            if(xmatrix == ahoX && ymatrix == aveY) {m[xmatrix][ymatrix] = apple;}//WRITE APPLE
            if(actspec){if(xmatrix == ihox && ymatrix == iveY) {if(!flip){m[xmatrix][ymatrix] = '+'; flip=true;} else if(flip) {m[xmatrix][ymatrix] = '*'; flip=false;} }}  //WRITE SPECIAL
            if(xmatrix == shoX && ymatrix == sveY) {m[xmatrix][ymatrix] = head;}//WRITE HEAD
            if(bodymatrix[xmatrix][ymatrix] == 'b' && m[xmatrix][ymatrix] == head) {gameover = true;}
            }
        }
    }
    
    void xprintbodymatrix()
    {
        int i,j;
        cout << "\nBodymatrix DEBUGMODE" << endl;
        for(j=0;j<15;j++)
                {
                for(i=0;i<28;i++)
                    {cout << bodymatrix[i][j];}
                cout << endl;
                }
    }
    
    void xprintmatrix()
    {
       system("cls")  ;
    cout << "\n\n       Punktestand " << points << endl; //" Speed " << speed << endl;
    int i, j;
        cout << tl; for (i=0;i<28;i++){cout << hl;}cout << tr << endl;
        for (j=0;j<15;j++){cout << vl;for (i=0;i<28;i++) {cout << m[i][j];}cout << vl << endl;}
        cout << gl; for (i=0;i<28;i++){cout << hl;}cout << gr << endl;
    
    //cout << "   X=" << shoX <<   "   Y="<< sveY <<  "      BODYPARTS " << bodyparts << endl;
    //cout << speed << endl;
    }
    
    


  • @melatroid sagte in Snake 2 Classic:

    cout << tl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << hl << tr << endl;
    cout <<vl<<m[0][0]<<m[1][0]<<m[2][0]<<m[3][0]<<m[4][0]<<m[5][0]<<m[6][0]<<m[7][0]<<m[8][0]<<m[9][0]<<m[10][0]<<m[11][0]<<m[12][0]<<m[13][0]<<m[14][0]<<m[15][0]<<m[16][0]<<m[17][0]<<m[18][0]<<m[19][0]<<m[20][0]<<m[21][0]<<m[22][0]<<m[23][0]<<m[24][0]<<m[25][0]<<m[26][0]<<m[27][0]<<vl<<endl;
    cout <<vl<<m[0][1]<<m[1][1]<<m[2][1]<<m[3][1]<<m[4][1]<<m[5][1]<<m[6][1]<<m[7][1]<<m[8][1]<<m[9][1]<<m[10][1]<<m[11][1]<<m[12][1]<<m[13][1]<<m[14][1]<<m[15][1]<<m[16][1]<<m[17][1]<<m[18][1]<<m[19][1]<<m[20][1]<<m[21][1]<<m[22][1]<<m[23][1]<<m[24][1]<<m[25][1]<<m[26][1]<<m[27][1]<<vl<<endl;
    cout <<vl<<m[0][2]<<m[1][2]<<m[2][2]<<m[3][2]<<m[4][2]<<m[5][2]<<m[6][2]<<m[7][2]<<m[8][2]<<m[9][2]<<m[10][2]<<m[11][2]<<m[12][2]<<m[13][2]<<m[14][2]<<m[15][2]<<m[16][2]<<m[17][2]<<m[18][2]<<m[19][2]<<m[20][2]<<m[21][2]<<m[22][2]<<m[23][2]<<m[24][2]<<m[25][2]<<m[26][2]<<m[27][2]<<vl<<endl;
    cout <<vl<<m[0][3]<<m[1][3]<<m[2][3]<<m[3][3]<<m[4][3]<<m[5][3]<<m[6][3]<<m[7][3]<<m[8][3]<<m[9][3]<<m[10][3]<<m[11][3]<<m[12][3]<<m[13][3]<<m[14][3]<<m[15][3]<<m[16][3]<<m[17][3]<<m[18][3]<<m[19][3]<<m[20][3]<<m[21][3]<<m[22][3]<<m[23][3]<<m[24][3]<<m[25][3]<<m[26][3]<<m[27][3]<<vl<<endl;
    cout <<vl<<m[0][4]<<m[1][4]<<m[2][4]<<m[3][4]<<m[4][4]<<m[5][4]<<m[6][4]<<m[7][4]<<m[8][4]<<m[9][4]<<m[10][4]<<m[11][4]<<m[12][4]<<m[13][4]<<m[14][4]<<m[15][4]<<m[16][4]<<m[17][4]<<m[18][4]<<m[19][4]<<m[20][4]<<m[21][4]<<m[22][4]<<m[23][4]<<m[24][4]<<m[25][4]<<m[26][4]<<m[27][4]<<vl<<endl;
    cout <<vl<<m[0][5]<<m[1][5]<<m[2][5]<<m[3][5]<<m[4][5]<<m[5][5]<<m[6][5]<<m[7][5]<<m[8][5]<<m[9][5]<<m[10][5]<<m[11][5]<<m[12][5]<<m[13][5]<<m[14][5]<<m[15][5]<<m[16][5]<<m[17][5]<<m[18][5]<<m[19][5]<<m[20][5]<<m[21][5]<<m[22][5]<<m[23][5]<<m[24][5]<<m[25][5]<<m[26][5]<<m[27][5]<<vl<<endl;
    cout <<vl<<m[0][6]<<m[1][6]<<m[2][6]<<m[3][6]<<m[4][6]<<m[5][6]<<m[6][6]<<m[7][6]<<m[8][6]<<m[9][6]<<m[10][6]<<m[11][6]<<m[12][6]<<m[13][6]<<m[14][6]<<m[15][6]<<m[16][6]<<m[17][6]<<m[18][6]<<m[19][6]<<m[20][6]<<m[21][6]<<m[22][6]<<m[23][6]<<m[24][6]<<m[25][6]<<m[26][6]<<m[27][6]<<vl<<endl;
    cout <<vl<<m[0][7]<<m[1][7]<<m[2][7]<<m[3][7]<<m[4][7]<<m[5][7]<<m[6][7]<<m[7][7]<<m[8][7]<<m[9][7]<<m[10][7]<<m[11][7]<<m[12][7]<<m[13][7]<<m[14][7]<<m[15][7]<<m[16][7]<<m[17][7]<<m[18][7]<<m[19][7]<<m[20][7]<<m[21][7]<<m[22][7]<<m[23][7]<<m[24][7]<<m[25][7]<<m[26][7]<<m[27][7]<<vl<<endl;
    cout <<vl<<m[0][8]<<m[1][8]<<m[2][8]<<m[3][8]<<m[4][8]<<m[5][8]<<m[6][8]<<m[7][8]<<m[8][8]<<m[9][8]<<m[10][8]<<m[11][8]<<m[12][8]<<m[13][8]<<m[14][8]<<m[15][8]<<m[16][8]<<m[17][8]<<m[18][8]<<m[19][8]<<m[20][8]<<m[21][8]<<m[22][8]<<m[23][8]<<m[24][8]<<m[25][8]<<m[26][8]<<m[27][8]<<vl<<endl;
    cout <<vl<<m[0][9]<<m[1][9]<<m[2][9]<<m[3][9]<<m[4][9]<<m[5][9]<<m[6][9]<<m[7][9]<<m[8][9]<<m[9][9]<<m[10][9]<<m[11][9]<<m[12][9]<<m[13][9]<<m[14][9]<<m[15][9]<<m[16][9]<<m[17][9]<<m[18][9]<<m[19][9]<<m[20][9]<<m[21][9]<<m[22][9]<<m[23][9]<<m[24][9]<<m[25][9]<<m[26][9]<<m[27][9]<<vl<<endl;
    cout <<vl<<m[0][10]<<m[1][10]<<m[2][10]<<m[3][10]<<m[4][10]<<m[5][10]<<m[6][10]<<m[7][10]<<m[8][10]<<m[9][10]<<m[10][10]<<m[11][10]<<m[12][10]<<m[13][10]<<m[14][10]<<m[15][10]<<m[16][10]<<m[17][10]<<m[18][10]<<m[19][10]<<m[20][10]<<m[21][10]<<m[22][10]<<m[23][10]<<m[24][10]<<m[25][10]<<m[26][10]<<m[27][10]<<vl<<endl;
    cout <<vl<<m[0][11]<<m[1][11]<<m[2][11]<<m[3][11]<<m[4][11]<<m[5][11]<<m[6][11]<<m[7][11]<<m[8][11]<<m[9][11]<<m[10][11]<<m[11][11]<<m[12][11]<<m[13][11]<<m[14][11]<<m[15][11]<<m[16][11]<<m[17][11]<<m[18][11]<<m[19][11]<<m[20][11]<<m[21][11]<<m[22][11]<<m[23][11]<<m[24][11]<<m[25][11]<<m[26][11]<<m[27][11]<<vl<<endl;
    cout <<vl<<m[0][12]<<m[1][12]<<m[2][12]<<m[3][12]<<m[4][12]<<m[5][12]<<m[6][12]<<m[7][12]<<m[8][12]<<m[9][12]<<m[10][12]<<m[11][12]<<m[12][12]<<m[13][12]<<m[14][12]<<m[15][12]<<m[16][12]<<m[17][12]<<m[18][12]<<m[19][12]<<m[20][12]<<m[21][12]<<m[22][12]<<m[23][12]<<m[24][12]<<m[25][12]<<m[26][12]<<m[27][12]<<vl<<endl;
    cout <<vl<<m[0][13]<<m[1][13]<<m[2][13]<<m[3][13]<<m[4][13]<<m[5][13]<<m[6][13]<<m[7][13]<<m[8][13]<<m[9][13]<<m[10][13]<<m[11][13]<<m[12][13]<<m[13][13]<<m[14][13]<<m[15][13]<<m[16][13]<<m[17][13]<<m[18][13]<<m[19][13]<<m[20][13]<<m[21][13]<<m[22][13]<<m[23][13]<<m[24][13]<<m[25][13]<<m[26][13]<<m[27][13]<<vl<<endl;
    cout <<vl<<m[0][14]<<m[1][14]<<m[2][14]<<m[3][14]<<m[4][14]<<m[5][14]<<m[6][14]<<m[7][14]<<m[8][14]<<m[9][14]<<m[10][14]<<m[11][14]<<m[12][14]<<m[13][14]<<m[14][14]<<m[15][14]<<m[16][14]<<m[17][14]<<m[18][14]<<m[19][14]<<m[20][14]<<m[21][14]<<m[22][14]<<m[23][14]<<m[24][14]<<m[25][14]<<m[26][14]<<m[27][14]<<vl<<endl;

    Das ist ja mal ne Konstruktion 🙃 Im restlichen Code gibt es doch schon for-Schleifen?



  • @zeropage Hat sich historisch so ergeben ;9



  • Dann schreib das mal schnell um oder möchtest du, daß dein Code bei r/badcode landet?



  • @Th69 Done



  • @melatroid Jetzt könntest du deinen Post noch so bearbeiten, dass du die Code Tags benutzt. Dann kann man vlt wirklich was erkennen 😉

    Einfach den Post editieren, den Source Code markieren und auf </> über dem Text Eingabefeld klicken (links davon kannst du noch die Sprache auswählen).



  • @Schlangenmensch Dank an die Schlangenmenschen


Anmelden zum Antworten