I was coding snake game. The game is working normally but I have a little problem.I can't set again commands block.For example The commands are accumulating when I have pressed the buttons,frequency.

And note:My language is Turkish. If you want understand variable mean, may be want use googletranslate or boubletranslate..

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <ctype.h>
#define yukari 72
#define asagi 80
#define sol 75
#define sag 77

///Prototipler*****
void git(int,int);
void ekrani_ciz();
void yonlendir(char);
void bekle();
void matris_dose();
void ogren_matris();//Matristeki yerlerin değerlerini öğrenmek için
void matrisi_yaz();
void deger_azalt();
void yem();
void giris();
int tekrar();
int rekor_oku();
void rekor_kaydet(int);
void intro();

///Globaller****
int kafa=3;
int taban_matris[22][22]= {0};
int x_koord=5,y_koord=5;
int yem_x=3,yem_y=3;
int puan=0,zorluk;
char isim[20],isim_rekor[20];

int main()
{
    char yon,yon2,acaba;
    int rekor;
    //yilanin uzunlugunu tutar
    system("title Snake v2.0");
    system("color f0");
basla:
    system("cls");
    rekor=rekor_oku();
    intro();
    git(1,1);
    giris();
    ekrani_ciz();//Duvarları çizer
    matris_dose();//Duvarlari belirle(sanal olarak belirler ekranı ciz fonksiyonundan bağımsızdır)

    puan-=zorluk;
    taban_matris[5][5]=kafa;//İlk baslangıc noktası
    matrisi_yaz();//Matris sistemindeki yem yılan vb şeyleri ekrana yazdırır
    git(60,13);//İsmin Yazılacagi yeri ayarlar
    printf("%s",isim);//Oyuncunun ismini yazdırır
    git(50,19);
    printf("Bir onceki rekor:");
    git(53,21);
    printf("%s %d",isim_rekor,rekor);
    getch();
    yon=getch();
    yon2=yon;//Şu anki yonün yedeğini alır
    yonlendir(yon);
    do
    {
        //eger bir tusa basılırsa yonelmek icin
        if(kbhit())
        {
            yon2=yon;//yonun yedeğini almak için
            acaba=getch();//yon tuslarında ki -32'yi tutmak icin
            if(acaba==-32)
                yon=getch();//yon tuslarindaki ascii degerini tutmak icin
        }

        if((yon2==yukari||yon2==asagi)&&(yon==sol||yon==sag))
            yonlendir(yon);//Yilanin yonunu belirlemek icin
        else if((yon2==sag||yon2==sol)&&(yon==yukari||yon==asagi))
            yonlendir(yon);//Yilanin yonunu belirlemek icin
        else
        {
            yonlendir(yon2);//Yilanin yonunu belirlemek icin
            yon==yon2;
        }
        if(taban_matris[y_koord][x_koord]==0||taban_matris[y_koord][x_koord]==-1||taban_matris[y_koord][x_koord]==kafa-1||taban_matris[y_koord][x_koord]==kafa-2)
            taban_matris[y_koord][x_koord]=kafa;
        else
        {
         if(puan>rekor)
            {
               git(51,17);
               printf("..Yeni Bir Rekor Tebrikler..");
               rekor_kaydet(puan);
            }
            tekrar();
            goto basla;
        }

        matrisi_yaz();

        if(taban_matris[yem_x][yem_y]!=-1)
        {
            yem();
            git(60,15);
            printf("Puan: %d",puan);
            kafa++;
        }
        else
            deger_azalt();
        Sleep(500/zorluk);
    }
    while(1);

    return 0;
}

///***Kullanıcı Arabirimleri****
//tekrar etmek icin
int tekrar()
{
    char secim;
    int satir,sutun;
    do
    {
        git(50,5);
        printf("Tekrar Oynamak");
        git(53,7);
        printf("ister Misiniz?(Enter\\Esc)");
        secim=getch();
        if(secim==13)
        {
            for(satir=0; satir<=21; satir++)
            {
                for(sutun=0; sutun<=21; sutun++)
                {
                    taban_matris[satir][sutun]=0;
                }
            }
            kafa=3;
            matris_dose();
            x_koord=5,y_koord=5;
            yem_x=3,yem_y=3;
            puan=0;
            return 1;
        }
        else if(secim==27)
            exit(2);
    }
    while(1);
}

void intro()
{
   static int bi_daha=0;
   git(40,5);
   printf("Snake v2.0'a hosgeldiniz");
   git(41,7);
   printf("Bu Oyun Let'sCoding");
   git(42,8);
   printf("Labratuvarlarinda uretilmistir");
   if(bi_daha>=1)
   {
      git(40,10);
      printf("Isiminizi yazarken F7 ve F8 tuslari");
      git(41,11);
      printf("Sizin icin yaralri olabilir ^_-");
      git(40,13);
      printf("Ayrica oyunun zorluk derecesi");
      git(41,14);
      printf("Maximum 9 degildir denemek isteyenler");
      git(42,15);
      printf("Yeni bir yol bulabilirler ^_-");
   }
   bi_daha=1;
}

//Ana Menü
void giris()
{
    int secim;
    printf("\n\t\t---Hosgeldiniz---");
    printf("\n\n\t1-Kolay\n\t 2\n\t  3\n\t   4\n\t    ");
    printf("5-Orta\n\t     6\n\t      7\n\t       8\n\t        9-Zor");
    do
    {
        printf("\n\n  Zorluk seviyenizi seciniz:");
        zorluk=getch();
        zorluk-=48;
    }
    while(zorluk<=0);
    printf("%d\n\n  Adinizi giriniz:",zorluk);
    scanf("%s",isim);
    system("cls");
}

///****kontrol odası***********
//Rekor Değerini Okuma
int rekor_oku()
{
    int zafer ;

    FILE *oku;
    oku = fopen("rekor.dat","r");

    fscanf(oku,"%s",isim_rekor);
    fscanf(oku,"%d",&zafer);
    fclose(oku);
    return zafer;
}
//Rekor değerini yazdırma
void rekor_kaydet(int yeni_rekor)
{

    FILE  *kaydet;
    kaydet = fopen("rekor.dat","w");
    fprintf(kaydet,"%s ",isim);
    fprintf(kaydet,"%d",yeni_rekor);
    fclose(kaydet);
}

//imleci kordinat verme
void git(int x,int y)
{
    COORD koord;
    koord.X = x;
    koord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), koord);
}

//yılanin boyunun ayni kalmasi
void deger_azalt()
{
    int satir,sutun;
    for(satir=0; satir<=21; satir++)
    {
        for(sutun=0; sutun<=21; sutun++)
        {
            if(taban_matris[satir][sutun]>0)
                taban_matris[satir][sutun]--;
        }
    }
}

//Yemi yerlesitirir
void yem()
{

    do
    {
        srand(time(NULL));
        yem_x=rand()%20+1;
        yem_y=rand()%20+1;
    }
    while(taban_matris[yem_x][yem_y]!=0);
    taban_matris[yem_x][yem_y]=-1;
    puan+=zorluk;
}

//yonlerin kontrolunu saglar
void yonlendir(char yon)
{
    switch(yon)
    {
    case yukari://Yukari
        y_koord--;
        break;
    case sol://Sol
        x_koord--;
        break;
    case sag://Sag
        x_koord++;
        break;
    case asagi://Asagi
        y_koord++;
        break;
    }
}

///***Goruntu birimleri**************
//Ekrana tablo cizme
void ekrani_ciz()
{
    int counter_cizici;
    /*
        git(7,0);//üst koordinatlar
        for(counter_cizici=1; counter_cizici<=21; counter_cizici++)
        {
            printf("%d ",counter_cizici%10);
        }
    */
    git(5,1);//Sol üst köşe
    printf("%c",201);

    //Ust yatay cizgiler
    for(counter_cizici=0; counter_cizici<=40; counter_cizici++)
    {
        printf("%c",205);
    }
    printf("%c",187);//Sağ üst köşe

    //Dikey çizgiler
    for(counter_cizici=0; counter_cizici<=19; counter_cizici++)
    {
        git(5,counter_cizici+2);
        printf("%c",186);//sağ dikey

        git(47,counter_cizici+2);
        printf("%c",186);//Sol dikey
    }

    git(5,22);
    printf("%c",200);//Sağ alt köşe

    //Alt çizgi
    for(counter_cizici=0; counter_cizici<=40; counter_cizici++)
    {
        printf("%c",205);
    }

    printf("%c",188);//Sol alt köşe
}

///***Matris Tahtasi***
//Matrisle birlikte sınırları çizme
void matris_dose()
{
    int satir=0,sutun=0;

    //-9 değeri duvarları belirler
    for(satir=0; satir<=21; satir++)
    {
        sutun=0;
        taban_matris[satir][sutun]=-9;
        sutun=21;
        taban_matris[satir][sutun]=-9;
    }

    for(sutun=0; sutun<=21; sutun++)
    {
        satir=0;
        taban_matris[satir][sutun]=-9;
        satir=21;
        taban_matris[satir][sutun]=-9;
    }
}

//butun matrisin degerini örenmek icin
void ogren_matris()
{
    int satir,sutun;
    for(satir=0; satir<=21; satir++)
    {
        git(7,satir+1);
        for(sutun=0; sutun<=21; sutun++)
        {
            printf("%d ",taban_matris[satir][sutun]);
        }
        printf("\n");
    }
}

//kullanici icin goruntusu
void matrisi_yaz()
{
    int satir,sutun;

    for(satir=0; satir<=21; satir++)
    {
        for(sutun=0; sutun<=21; sutun++)
        {
            //yemi koymak icin
            if(taban_matris[satir][sutun]==-1)
            {
                git(sutun*2+5,satir+1);
                printf("%c",3);
            }

            //Son kuyrugu silmek icin
            if(taban_matris[satir][sutun]==1)
            {
                git(sutun*2+5,satir+1);
                printf("  ");
            }

            //yilani cizmek icin
            else if(taban_matris[satir][sutun]==kafa-1)
            {
                git(sutun*2+5,satir+1);
                printf("*");
            }

            if(taban_matris[satir][sutun]==kafa)
            {
                git(sutun*2+5,satir+1);
                printf("%c",1);
            }
        }
    }
}

Recommended Answers

All 2 Replies

You need to process each keystroke from the player, quicker. Since you have conio.h, you should be able to use kbhit() to detect if a keystroke has been made.

Windows has it's own equivalent way to detect if a key has been pressed, and your program needs to use ONE (not both), of them, to stop the backup of the key strokes, in your snake game.

In this "inner loop", you can't have any buffered input to stop the unbuffered input, of course.

I hate to spring this kind of things on you, but if you expect some Americans to look at your code (which I am a native speaker of), you may want to read/write it like us too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.