Mini project Snake game using C
The snake game is a simple console-based game developed in C programming language. It is suited for mini-projects.
This project is a learning milestone for a beginner who wants to step into basic gaming in C. If you are looking for other mini-projects, follow the link below:-
Here is List of Mini projects in C
Source Code
Loading…
#include#include #include #include #include #include #include #include #include #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 int length; int bend_no; int len; char key; void record(); void load(); int life; void Delay(long double); void Move(); void Food(); int Score(); void Print(); void gotoxy(int x, int y); void GotoXY(int x,int y); void Bend(); void Boarder(); void Down(); void Left(); void Up(); void Right(); void ExitGame(); int Scoreonly(); struct coordinate{ int x; int y; int direction; }; typedef struct coordinate coordinate; coordinate head, bend[500],food,body[30]; int main() { char key; Print(); system("cls"); load(); length=5; head.x=25; head.y=20; head.direction=RIGHT; Boarder(); Food(); //to generate food coordinates initially life=3; //number of extra lives bend[0]=head; Move(); //initialing initial bend coordinate return 0; } void Move() { int a,i; do{ Food(); fflush(stdin); len=0; for(i=0;i=70||head.y=30||check!=0) { life--; if(life>=0) { head.x=25; head.y=20; bend_no=0; head.direction=RIGHT; Move(); } else { system("cls"); printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n"); record(); exit(0); } } } void Food() { if(head.x==food.x&&head.y==food.y) { length++; time_t a; a=time(0); srand(a); food.x=rand()%70; if(food.x"); else printf("*"); } /*body[len].x=head.x-i; body[len].y=head.y;*/ len++; } Bend(); if(!kbhit()) head.x++; } void Bend() { int i,j,diff; for(i=bend_no;i>=0&&len 0) for(j=1;j0) for(j=1;j Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n"); printf("\n\nPress any key to play game..."); if(getch()==27) exit(0); } void record(){ char plname[20],nplname[20],cha,c; int i,j,px; FILE *info; info=fopen("record.txt","a+"); getch(); system("cls"); printf("Enter your name\n"); scanf("%[^\n]",plname); //************************ for(j=0;plname[j]!='\0';j++){ //to convert the first letter after space to capital nplname[0]=toupper(plname[0]); if(plname[j-1]==' '){ nplname[j]=toupper(plname[j]); nplname[j-1]=plname[j-1];} else nplname[j]=plname[j]; } nplname[j]='\0'; //***************************** //sdfprintf(info,"\t\t\tPlayers List\n"); fprintf(info,"Player Name :%s\n",nplname); //for date and time time_t mytime; mytime = time(NULL); fprintf(info,"Played Date:%s",ctime(&mytime)); //************************** fprintf(info,"Score:%d\n",px=Scoreonly());//call score to display score //fprintf(info,"\nLevel:%d\n",10);//call level to display level for(i=0;i You can download the source code here.
Loading...