promotion image of download ymail app
Promoted

I want a snake game code .?

I have to use Visual stdio 2010. programming language is C. Please help me for the game . Thanks.

1 Answer

Relevance
  • ??????
    Lv 7
    8 years ago
    Favorite Answer

    //

    // This code is translated from Basic to C

    //

    // starting from the basic program by Brett Kelts

    //

    // It uses the special DOS-characters 219,220,223 for

    // dividing up the cursor in lower and upper half

    //

    // Those characters aren't available anymore in Windows

    // character table, except for the console DOS-window.

    // So this is a console window program.

    //

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include <time.h>

    #include <conio.h>

    #include <windows.h>

    double X,Y,Y1;

    long J,Z,DIR,POIN,SNK;

    long SCR[100][100],OLDSCR[100][100];

    unsigned char XX[257],YY[257],keypr;

    int InitScreen()

    {

    int i,j;

    for(i=1;i<100;i++) for(j=1;j<100;j++) { SCR[i][j]=32; OLDSCR[i][j]=32; }

    return(1);

    }

    long SCREEN(long X, long Y)

    {

    return(SCR[X][Y]);

    }

    double DIV(double X, double Y)

    {

    double d;

    long l1,l2,l3;

    l1 = (long) X;

    l2 = (long) Y;

    l3 = l1 / l2;

    d = (double) l3;

    return(d);

    }

    double FNA(double Y)

    {

    return(DIV(Y+1.0,2.0));

    }

    int Check1()

    {

    unsigned char ch1;

    Y1 = FNA(Y);

    J = SCREEN((long)X,(long)Y1);

    if ((J==219)||((J==220)&&

    ((Y/2.0)==DIV(Y,2.0)))||

    ((J==223)&&((Y/2.0)!=DIV(Y,2.0))))

    return(0);

    if ((Y/2.0)!=DIV(Y,2.0)) if (J==220) ch1=219; else ch1=223;

    else if (J==223) ch1=219; else ch1=220;

    SCR[(long)X][(long)Y1]=ch1;

    return(1);

    }

    int Check2()

    {

    unsigned char ch1;

    Y1 = FNA(Y);

    J = SCREEN((long)X,(long)Y1);

    if ((J!=219)&&(!((J==223)&&

    ((Y/2.0)!=DIV(Y,2.0))))&&

    (!((J==220)&&((Y/2.0)==DIV(Y,2.0)))))

    return(0);

    if ((Y/2.0)!=DIV(Y,2.0)) if (J==219) ch1=220; else ch1=32;

    else if (J==219) ch1=223; else ch1=32;

    SCR[(long)X][(long)Y1]=ch1;

    return(1);

    }

    int Check3()

    {

    Y1 = FNA(Y);

    J = SCREEN((long)X,(long)Y1);

    if ((J==219)||((J==223)&&

    ((Y/2.0)!=DIV(Y,2.0)))||

    ((J==220)&&((Y/2.0)==DIV(Y,2.0))))

    Z=1; else Z=0;

    return(1);

    }

    int ClearSnakeSpace()

    {

    for(Y=19.0;Y<=21.0;Y+=1.0) for(X=32.0;X<=40.0;X+=1.0) Check2();

    return(1);

    }

    int GenFood()

    {

    long X3,Y3,TYPE;

    X3 = 2*(rand()%38+2); Y3 = rand()%18+2; TYPE = rand()%3;

    SCR[X3][Y3] = TYPE+176; SCR[X3+1][Y3] = TYPE+176;

    }

    int CheckArrowKeyPressed()

    {

    while (kbhit()) keypr = getch();

    if (keypr != 0)

    {

    if (keypr==72) DIR=1; // Up key

    else if (keypr==80) DIR=3; // Down key

    else if (keypr==75) DIR=4; // Left key

    else if (keypr==77) DIR=2; // Right key

    else if (keypr==27) exit(1); // Escape

    keypr=0;

    }

    }

    int MainEvent()

    {

    int i,M;

    long EN,X0,Y0,PO,sc,PT;

    unsigned char X2,Y2;

    POIN = 0; SNK = 0;

    clrscr();

    for(i=1;i<=20;i++) { SCR[1][i]=219; SCR[80][i]=219; }

    for(i=2;i<=79;i++) { SCR[i][1]=223; SCR[i][20]=220; }

    ClearSnakeSpace();

    for(i=1;i<=255;i++) { XX[i]=' '; YY[i]=' '; }

    EN=5; X0=33; Y0=20; DIR=2;

    for(M=1;M<=EN;M++) { XX[M]=X0; YY[M]=Y0; }

    for(M=1;M<=4;M++) GenFood();

    gotoxy(1,25); printf("Score- 0 points.");

    PO = 200;

    keypr = 0;

    HERE : X=(double) XX[EN]; Y=(double) YY[EN];

    Check2();

    X0=XX[1]; Y0=YY[1];

    X=(double)X0; Y=(double)Y0;

    Check1();

    CheckArrowKeyPressed();

    if (DIR==1) Y-=1.0;

    else if (DIR==2) X+=1.0;

    else if (DIR==3) Y+=1.0;

    else if (DIR==4) X-=1.0;

    Check3();

    if (Z==1) exit(1);

    else

    {

    for(i=EN-1;i>=1;i--) { XX[i+1]=XX[i]; YY[i+1]=YY[i]; }

    XX[1] = (unsigned char) X; YY[1] = (unsigned char) Y;

    }

    Y1 = FNA(Y);

    sc = SCREEN((long)X,(long)Y1);

    if ((sc>175) && (sc<179))

    {

    PT = sc-173;

    POIN += (PT-2)*((SNK*2)+5);

    gotoxy(1,25); printf("Score- %ld points.",POIN);

    SCR[(long)(DIV(X,2.0)*2.0)][(long)Y1]=' ';

    SCR[(long)(DIV(X,2.0)*2.0)+1][(long)Y1]=' ';

    PT *= 3;

    X2 = XX[EN]; Y2 = YY[EN];

    for(i=1;i<=PT;i++) XX[EN+i]=X2; YY[EN+i]=Y2;

    EN += PT;

    GenFood();

    ShowScreen();

    goto HERE;

    }

    ShowScreen();

    sleep(50);

    goto HERE;

    return(1);

    }

    int ShowScreen()

    {

    int i,j;

    for(i=1;i<=80;i++) for(j=1;j<=20;j++)

    {

    // Print only the changed characters

    // This is because the graphics of the

    // console window are very slow.

    if (SCR[i][j]!=OLDSCR[i][j])

    {

    gotoxy(i,j);

    printf("%c",SCR[i][j]);

    }

    OLDSCR[i][j] = SCR[i][j];

    }

    return(1);

    }

    int main()

    {

    HANDLE m_Screen = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_CURSOR_INFO curInf;

    SetConsoleTitle("SNAKE");

    GetConsoleCursorInfo(m_Screen, &curInf);

    curInf.bVisible = FALSE;

    SetConsoleCursorInfo(m_Screen, &curInf);

    srand(time(NULL));

    InitScreen();

    MainEvent();

    return(0);

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.