Snake game.c
#include<STDLIB.H>
#include<stdio.h> #include<conio.h> #include<time.h> int height =23 ,width =78,x,y,fruitx,fruity,score,gameend,flag; int tailx[100],taily[100]; int piece=0; //x = height and y= width void makelogic(){ int i ; int prevx,prevy,prev2x,prev2y; prevx=tailx[0]; prevy=taily[0]; tailx[0]=x; taily[0]=y; for(i =1;i <=piece;i++){ prev2x=tailx[i]; prev2y=taily[i]; tailx[i]=prevx; taily[i]=prevy; prevx=prev2x; prevy=prev2y; } switch(flag){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } if(x==0||y==0||x==height||y==width){ gameend = 1; } if(x==fruitx && y == fruity){ label3: fruitx = rand ()%23 ; if (fruitx==0){ goto label3; } label4: fruity = rand ()%78 ; if (fruity==0) goto label4; score++; piece++; } } void input(){ if (kbhit()){ switch (getch()){ case 'w': flag = 1; break; case 's': flag = 2; break; case 'a': flag = 3; break; case 'd': flag = 4; break; } } } void setup(){ time_t t; x=height/2; y=width/2; srand((unsigned) time(&t)); label1: fruitx = rand ()%23 ; if (fruitx==0){ goto label1; } label2: fruity = rand ()%78 ; if (fruity==0){ goto label2; } gameend= 0; score = 0; } void draw(){ int i,j,k,ch; system("cls"); for (i =0 ; i<= height;i++){ for (j=0;j<=width;j++){ if(i == 0 || j==0 || i==height || j==width){ printf("\033[46;36;3m*"); } else{ if(i == x && j==y){ printf("\033[42;1;3m "); } else if(i == fruitx && j== fruity){ printf("\033[41;31;2m0"); } else{ ch = 0; for(k=1;k<=piece;k++){ if(i==tailx[k] && j==taily[k]){ printf("\033[42;1;3mo"); ch=1; } } if(ch==0){ printf("\033[40;1;3m "); } } } } printf("\n"); } printf("score = %d",score); } void gameover(){ int i; printf("\033[31;1;3m"); printf("\033[40;1;3m"); system("cls"); for(i=0;i<=10;i++){ printf("_"); } printf("\t\t\t /\\ \t \t |\\ /| |----------"); printf("\n"); printf("|\t\t\t / \\ \t\t | \\ / | |"); printf("\n"); printf("|\t\t\t / \\ \t | \\ / | |"); printf("\n"); printf("|"); printf(" _________\t / \\ | \\ / | |__________"); printf("\n"); printf("| | | /________________\\ | \\/ | |"); printf("\n"); printf("| | | / \\ |\t | |"); printf("\n"); printf("| | | / \\ |\t | |"); printf("\n|_________| |/ \\ |\t | |__________"); printf("\n\n\n"); printf("\n\t\t __________ \t \t\t|__________"); printf("\n\t\t|\t\t|\\ |\t| |"); printf("\n\t\t|\t\t| \\ |\t| |"); printf("\n\t\t|\t\t| \\ |\t| |"); printf("\n\t\t|__________ | \\ |\t| |"); printf("\n\t\t|\t\t| \\ |\t| |"); printf("\n\t\t|\t\t| \\ |\t| |"); printf("\n\t\t|\t\t| \\ |\t| |"); printf("\n\t\t|__________ | \\|\t|----------"); printf("\033[31;1;4m score = %d",score); getchar(); return 0; } int main(){ int z; setup(); while(gameend != 1){ //sleep(1); input(); draw(); makelogic(); //sleep(1); } if(gameend=1){ gameover(); } getchar(); // return 0; }
Very cool bro
ReplyDelete