Yılan Oyunu

Kodlamayı en sevdiğim C++ ile neler yapabilirim diye düşünürken internette okuduğum bir yazıdak etkilenerek bir yılan oyunu yapmaya karar verdim. getch() fonksiyonunu kullanarak klavye tıklanma olaylarını algılayıp yılana yön verdirmeye çalıştım. windows.h kütüphanesi sayesinde her saniye ekran silinip yılan yeni konumuyla beraber ekrana veriliyor. Ekran çıktıları ve kod’u aşağıda bulabilirsiniz.

Ekran resimleri ve Program Kodu

Program kodu;

 

1 Comment

  1. #include
    #include
    #include
    #include
    #include
    #include
    #include

    using namespace std;

    void menu();
    void game();
    void controls(char s[][22],int temp[][22],int eating);
    void credits();
    void table(char s[][22],int score);
    bool checkFood(char s[][22],int fx,int fy);
    void spawnFood(char s[][22],int & fx,int & fy);
    bool gameover(char s[][22],int temp[][22]);
    void endgame(int score,int highscore);
    void help();
    void options();
    void difficulty();
    void pause();

    int lenght;
    char contr = ‘n’;
    int chek[32][22];
    double diff=0.5;

    int main()
    {
    srand(time(0));
    int i;
    char sl;
    for(i=0;i0)
    {
    cout<<""<<endl;
    cout<>sl;
    if(sl==’e’)
    {
    system(“cls”);
    goto start;
    }
    else goto quit;
    }
    start:
    {
    menu();
    }
    cin>>sl;
    if(sl==’1′)
    {
    system(“cls”);
    game();
    i=1;
    }
    else if(sl==’2′)
    {
    options();
    system(“cls”);
    goto start;
    i=1;
    }
    else if(sl==’3′) goto quit;
    else
    {
    system(“cls”);
    i=1;
    goto start;
    }
    }
    quit:
    {
    system(“exit”);
    }

    return 0;
    }

    void table(char s[][22],int score)
    {
    int x=32;
    int y=22;
    int i,j;
    for(i=0;i<y;i++)
    {
    for(j=0;j<x;j++)
    {
    if(j<(x-1)) cout<<s[j][i];
    if(j==(x-1)) cout<<s[j][i]<<endl;
    }
    }
    cout<<""<<endl;
    cout<<"skorun: "<<score;
    }

    void game()
    {
    char s[32][22];
    int temp[32][22];
    int i,j,fx,fy,x,y,score=0,highscore=score;
    double k;
    int eating;

    for(i=0;i<22;i++)
    {
    for(j=0;j0 && i<21) s[j][i]=' ';
    if(j==0 || j==31) s[j][i]='|';
    }
    }
    spawnFood(s,fx,fy);
    for(y=1;y<21;y++)
    {
    for(x=1;x<31;x++)
    {
    temp[x][y]=0;
    }
    }
    s[17][10]='o';
    s[16][10]='*';
    s[15][10]='*';

    temp[15][10]=3;
    temp[16][10]=2;
    temp[17][10]=1;

    system("cls");
    table(s,score);
    lenght=3;

    while(true)
    {
    eating=0;
    Sleep(diff*1000);
    if(kbhit())
    {
    contr=getch();
    if(contr=='0') pause();
    goto movemake;
    }
    else
    {
    goto movemake;
    }
    movemake:
    {
    for(x=1;x<31;x++)
    {
    for(y=1;yhighscore) highscore=score;
    }
    }
    gameEnd:
    {
    endgame(score,highscore);
    return;
    }
    }

    void controls(char s[][22],int temp[][22],int eating)
    {
    int i,x,y;
    for(i=lenght;i>0;i–)
    {
    if(i==lenght)
    {
    for(x=1;x<31;x++)
    {
    for(y=1;y1 && i<lenght)
    {
    for(x=1;x<31;x++)
    {
    for(y=1;y<21;y++)
    {
    if(temp[x][y]==i && s[x][y]=='*')
    {
    temp[x][y]=i+1;
    x=30;
    y=20;
    }
    }
    }
    }
    else if(i==1)
    {
    for(x=1;x<31;x++)
    {
    for(y=1;y<21;y++)
    {
    if(temp[x][y]==i && s[x][y]=='o')
    {
    movecheck:
    if(contr!='1' && contr!='2' && contr!='3' && contr!='4')
    {
    s[x][y]='*'; temp[x][y]=i+1;
    if(temp[x-1][y]==i+2)
    {
    s[x+1][y]='o'; temp[x+1][y]=1;
    return;
    }
    else if(temp[x+1][y]==i+2)
    {
    s[x-1][y]='o'; temp[x-1][y]=1;
    return;
    }
    else if(temp[x][y+1]==i+2)
    {
    s[x][y-1]='o'; temp[x][y-1]=1;
    return;
    }
    else if(temp[x][y-1]==i+2)
    {
    s[x][y+1]='o'; temp[x][y+1]=1;
    return;
    }
    }
    else if(contr=='1')
    {
    if(temp[x][y-1]==0)
    {
    s[x][y]='*'; temp[x][y]=i+1;
    s[x][y-1]='o'; temp[x][y-1]=1;
    return;
    }
    else
    {
    contr='n';
    goto movecheck;
    }
    }
    else if(contr=='2')
    {
    if(temp[x][y+1]==0)
    {
    s[x][y]='*'; temp[x][y]=i+1;
    s[x][y+1]='o'; temp[x][y+1]=1;
    return;
    }
    else
    {
    contr='n';
    goto movecheck;
    }
    }
    else if(contr=='3')
    {
    if(temp[x-1][y]==0)
    {
    s[x][y]='*'; temp[x][y]=i+1;
    s[x-1][y]='o'; temp[x-1][y]=1;
    return;
    }
    else
    {
    contr='n';
    goto movecheck;
    }
    }
    else if(contr=='4')
    {
    if(temp[x+1][y]==0)
    {
    s[x][y]='*'; temp[x][y]=i+1;
    s[x+1][y]='o'; temp[x+1][y]=1;
    return;
    }
    else
    {
    contr='n';
    goto movecheck;
    }
    }
    }
    }
    }
    }
    }
    }

    bool checkFood(char s[][22],int fx,int fy)
    {
    int x,y,c=0;
    for(x=1;x<31;x++)
    {
    for(y=1;y<21;y++)
    {
    if(s[x][y]=='o' && x==fx && y==fy)
    {
    c=1;
    }
    }
    }
    if(c==1) return true;
    else return false;
    }

    void spawnFood(char s[][22],int & fx,int & fy)
    {

    int i,j;
    fx=(rand()%30)+1;
    fy=(rand()%20)+1;
    for(j=1;j<31;j++)
    {
    for(i=1;i<21;i++)
    {
    if(s[j][i]!='*' && s[j][i]!='o')
    {
    s[fx][fy]='+';
    return;
    }
    else
    {
    fx=(rand()%30)+1;
    fy=(rand()%20)+1;
    i=1;
    j=1;
    }
    }
    }
    }

    bool gameover(char s[][22],int temp[][22])
    {
    int x,y,c=0;

    for(x=0;x<32;x++)
    {
    for(y=0;y<22;y++)
    {
    if((x==0 || x==31 || y==0 || y==21) && s[x][y]=='o')
    {
    c=1;
    }
    if(chek[x][y]==1 && s[x][y]=='o')
    {
    c=1;
    }
    }
    }
    if(c==1) return true;
    if(c==0) return false;
    }

    void endgame(int score,int highscore)
    {
    cout<<""<<endl<<endl;
    cout<<" ———————————————– "<<endl;
    cout<<"| * * * * * ****** * * |"<<endl;
    cout<<"| * * * * * * * * * * * * |"<<endl;
    cout<<"| * * * * * * * * ***** * * * |"<<endl;
    cout<<"| ******* * * * * * * * * * * |"<<endl;
    cout<<"| * * * ** * ** ****** * ** |"<<endl;
    cout<<" ———————————————– "<<endl;
    cout<<""<<endl<<endl;
    cout<<" EN YUKSEK SKOR: "<<score<<endl<<endl;
    cout<<" S K O R U N: "<<highscore<<endl;
    cout<<""<<endl<<endl;
    }

    void menu()
    {
    cout<<""<<endl;
    cout<<" ————————————– "<<endl;
    cout<<"| | "<<endl;
    cout<<"| * * * * * * * | "<<endl;
    cout<<"| * * * * * * * * * | "<<endl;
    cout<<"| * * * * * * * * * | "<<endl;
    cout<<"| * * * ******* * * * | "<<endl;
    cout<<"| * * ***** * * * ** | "<<endl;
    cout<<"| | "<<endl;
    cout<<"————————————— "<<endl;
    cout<<""<<endl<<endl;
    cout<<" M E N U: "<<endl<<endl;
    cout<<" 1: Oyunu baslat "<<endl<<endl;
    cout<<" 2: Ayarlar "<<endl<<endl;
    cout<<" 3: Cikis "<<endl<<endl;
    }

    void help()
    {
    char sel;
    system("cls");
    while(true)
    {
    cout<<" ———————————————-"<<endl;
    cout<<"| |"<<endl;
    cout<<"| 1 – yukari |"<<endl;
    cout<<"| |"<<endl;
    cout<<"| 3 – sola 4 – saga |"<<endl;
    cout<<"| |"<<endl;
    cout<<"| 2 – asagi |"<<endl;
    cout<<"| |"<<endl;
    cout<<"| 0 – DURDUR |"<<endl;
    cout<<"| |"<<endl;
    cout<<" ———————————————- "<<endl<<endl<<endl;
    cout<>sel;
    if(sel==’e’) return;
    else system(“cls”);
    }
    }

    void credits()
    {
    char sel;
    system(“cls”);
    while(true)
    {
    cout<<""<<endl<<endl;
    cout<<" Fikir: Yani Bilemiomm bence sino"<<endl<<endl;
    cout<<" Bas Programci: sino "<<endl<<endl;
    cout<<" Tasarimci: sino "<<endl<<endl;
    cout<<" Test Eden: sino "<<endl<<endl;
    cout<<" Ozel Tesekkurler: sino31 "<<endl<<endl<<endl;
    cout<>sel;
    if(sel==’e’) return;
    else system(“cls”);
    }
    }

    void options()
    {
    char sel;
    system(“cls”);
    while(true)
    {
    system(“cls”);
    cout<<""<<endl<<endl;
    cout<<" O P S I Y O N L A R : "<<endl<<endl;
    cout<<" 1: Zorlugu sec "<<endl<<endl;
    cout<<" 2: Kontroller "<<endl<<endl;
    cout<<" 3: Sunum "<<endl<<endl;
    cout<<" 4: Geri don "<<endl<>sel;
    if(sel==’1′) difficulty();
    else if(sel==’2′) help();
    else if(sel==’3′) credits();
    else if(sel==’4′) return;
    else system(“cls”);
    }
    }

    void difficulty()
    {
    char sel;
    system(“cls”);
    while(true)
    {
    cout<<""<<endl<<endl;
    cout<<" S U A N K I Z O R L U K:"<<endl<<endl;
    if(diff==1) cout<<" K O L A Y"<<endl<<endl;
    else if(diff==0.5) cout<<" N O R M A L"<<endl<<endl;
    else if(diff==0.2) cout<<" Z O R"<<endl<<endl;
    else if(diff==0.1) cout<<" C O K Z O R"<<endl<<endl;
    else if(diff==0.03) cout<<" E B E N I N"<<endl<<endl;
    cout<<" Z O R L U G U S E C:"<<endl<<endl;
    cout<<" 1: kolay "<<endl<<endl;
    cout<<" 2: Normal "<<endl<<endl;
    cout<<" 3: Zor "<<endl<<endl;
    cout<<" 4: Cok zor "<<endl<<endl;
    cout<<" 5: Ebenin "<<endl<<endl<<endl<<endl;
    cout<<" 6: Geri don "<<endl<>sel;
    if(sel==’1′)
    {
    diff=1;
    system(“cls”);
    }
    else if(sel==’2′)
    {
    diff=0.5;
    system(“cls”);
    }
    else if(sel==’3′)
    {
    diff=0.2;
    system(“cls”);
    }
    else if(sel==’4′)
    {
    diff=0.1;
    system(“cls”);
    }
    else if(sel==’5′)
    {
    diff=0.03;
    system(“cls”);
    }
    else if(sel==’6′) return;
    else system(“cls”);
    }
    }

    void pause()
    {
    char c;
    cout<<""<<endl<<endl;
    cout<>c;
    if(c==’e’) return;
    else goto ooo;
    }

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir