змеиная игра в c и проблемы с рандомизацией и логикой - PullRequest
0 голосов
/ 18 апреля 2020

Первая проблема заключается в том, что координата дозы плода не изменяется после того, как змея потребляет ее, и моя вторая проблема заключается в том, что у меня возникла проблема с частью кода, где змея бьет себя и умирает, и можем ли мы что-то сделать с мерцанием графика из-за цикла я использовал кодовые блоки компилятора

#include<stdio.h>
#include<conio.h>
#include <windows.h>
#include <stdlib.h>
#include<time.h>

#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77

void gotoxy(int x, int y)
{
  static HANDLE h = NULL;
  if(!h)
    h = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD c = { x, y };
  SetConsoleCursorPosition(h,c);
}

void hidecursor()
{
   HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_CURSOR_INFO info;
   info.dwSize = 100;
   info.bVisible = FALSE;
   SetConsoleCursorInfo(consoleHandle, &info);
}

static int pos1=30,pos2=15;
int fruitx=0,fruity=0,gameover=0;
char ch;
int tailx[100],taily[100],counttail=0;

void left();
void up();
void right();
void down();
void draw();

void fruit(); // this is to generate fruit


void input_detection(  );

int main(){

    gotoxy(pos1,pos2);

        while (1){
            srand(time(NULL));  // this only works at the restart
            printf("%d",gameover); // this is to check the value of game over over which i am having errors
            draw();
            hidecursor();
            input_detection();
            Sleep(100);
        }
return 0;
}

void input_detection(){
  //if (gameover=1){ this was meant to be the code to end the game which dosenot work
    //exit(0);
  //}
     if (_kbhit()){
        ch=_getch();
    }
    if (ch==UP)
         up();
    if (ch==DOWN)
        down();
    if (ch==LEFT)
        left();
    if (ch==RIGHT)
        right();
    if(pos1==fruitx && pos2==fruity){
        fruit();
        counttail++;
    }

          int prevx=tailx[0],prevy=taily[0];
    int prev2x,prev2y;
    tailx[0]=pos1;
    taily[0]=pos2;
    for(int i=1;i<=counttail;i++){
       prev2x=tailx[i];
       prev2y=taily[i];
       tailx[i]=prevx;
       taily[i]=prevy;
       prevx=prev2x;
       prevy=prev2y;
    }
   for(int i=0;i<counttail;i++){
        if(pos1==tailx[i] && pos2==taily[i]) gameover++;// this is the part where it tells if it hits its own body the code is not working so added ++ to count the error 
    }
}
 void up(){
     pos2-=1;
    gotoxy(pos1,pos2);
        printf("0");
if(pos2==0) pos2=30;
 }
 void left(){
          pos1-=1;
    gotoxy(pos1,pos2);
        printf("0");
if(pos1==0) pos1=60;
 }
  void right(){
           pos1+=1;
    gotoxy(pos1,pos2);
       printf("0");
 if(pos1==60) pos1=1;
 }
  void down(){
           pos2+=1;
    gotoxy(pos1,pos2);
    printf("0");
    if(pos2==30) pos2=0;
 }


void draw(){
    int i,j;
    system("cls");
    srand(time(NULL));
    if (fruitx==0)
        fruitx=rand()%60;
    if (fruity==0)
        fruity=rand()%30;

     gotoxy(fruitx,fruity);
    printf("%c",147);
    for(int t=0;t<counttail;t++){
        gotoxy(tailx[t],taily[t]);
        printf("o");
    }
    for(i=0;i<=60;i++){
        for(j=0;j<=30;j++){
            if(i==0 || j==0 || i==60 ||j==30){
                gotoxy(i,j);
                printf("%c",176);
            }
        }
    }

}

void fruit(){
    srand(time(NULL));
    if (fruitx==0)
        fruitx=rand()%60;
    if (fruity==0)
        fruity=rand()%30;

}
...