Skip to main content

Snake Game Proggramming code in C

Programming Languages

Welcome to coding world

Snake Game code in C




#include <graphics.h>

#include <stdio.h>

#include <stdlib.h>

#define T 2000

int n=0,px,py;

main()

{

 int gd=DETECT,gm,i,j;

 clrscr();

 gotoxy(25,4);

 printf("DON'T TUCH THE BORDER");

 gotoxy(25,6);

 printf("Press < p > for pause");

 gotoxy(17,8);

 printf("IN MIDDLE OF GAME, PRESS < Esc > TO EXIT");

 x:

 gotoxy(26,10);

 printf("press < s > to start ");

 gotoxy(26,12);

 printf(" Use SMALL case only");

 gotoxy(34,10);

 if(getch()=='s')

 initgraph(&gd,&gm,"c:\tc\bgi");

 else {

 clrscr();

 goto x;

 }

 line(0,0,0,479);

 line(0,0,639,0);

 line(639,0,639,479);

 line(0,479,639,479);

 pause();

 print();

 right(100,100);

}

 right(int x,int y)

 {

 char ch,fu='r';

 int i,j;

 for(i=x;i<=652;i++){

 for(j=y;j<=y+10;j++){

 putpixel(i,j,4);

 putpixel(i-10,j,16);

 }

 delay(T-n);

 check(i,j-1);

 if(kbhit()){

 ch=getch();

 if(ch=='p')

 pause();

 else

 choice(i,j-1,ch,fu);

 }

 }

 }

 left(int x,int y)

 {

 char ch,fu='l';

 int i,j;

 for(i=x;i>-12;i--){

 for(j=y;j<=y+10;j++){

 putpixel(i,j,4);

 putpixel(i+10,j,16);

 }

 delay(T-n);

 check(i,j-1);

 if(kbhit()){

 ch=getch();

 if(ch=='p')

 pause();

 else

 choice(i,j-1,ch,fu);

 }

 }

 }

 down(int x,int y)

 {

 char ch,fu='d';

 int i,j;

 for(j=y;j<=492;j++){

 for(i=x;i<=x+10;i++){

 putpixel(i,j,4);

 putpixel(i,j-10,16);

 }

 delay(T-n);

 check(i-1,j);

 if(kbhit()){

 ch=getch();

 if(ch=='p')

 pause();

 else

 choice(i-1,j,ch,fu);

 }

 }

 }

 up(int x,int y)

 {

 char ch,fu='u';

 int i,j;

 for(j=y;j>=-12;j--){

 for(i=x;i<=x+10;i++){

 putpixel(i,j,4);

 putpixel(i,j+10,16);

 }

 delay(T-n);

 check(i-1,j);

 if(kbhit()){

 ch=getch();

 if(ch=='p')

 pause();

 else

 choice(i-1,j,ch,fu);

 }

 }

 }

 choice(int i,int j,char ch,char fu)

 {

 if(fu=='r'){

 if(ch=='K'){

 j-=10;

 i-=10;

 left(i,j);

 }

 else if(ch=='P'){

 i-=10;

 down(i,j);

 }

 else if(ch=='H'){

 j-=10;

 i-=10;

 up(i,j);

 }

 else if(ch==27)

 esc();

 else {

 j-=10;

 right(i,j);

 }

 }

 else if(fu=='l'){

 if(ch=='M'){

 j-=10;

 /*i+=-10; no comments here free me*/

 right(i,j);

 }

 else if(ch=='P')

 down(i,j);

 else if(ch=='H'){

 j-=10;

 up(i,j);

 }

 else if(ch==27)

 esc();

 else {

 j-=10;

 left(i,j);

 }

 }

 else if(fu=='d'){

 if(ch=='H'){

 j-=10;

 i-=10;

 up(i,j);

 }

 else if(ch=='K'){

 i-=10;

 j-=10;

 left(i,j);

 }

 else if(ch=='M'){

 j-=10;

 right(i,j);

 }

 else if(ch==27)

 esc();

 else {

 i-=10;

 down(i,j);

 }

 }

 else if(fu=='u'){

 if(ch=='P'){

 j+=10;

 i-=10;

 down(i,j);

 }

 else if(ch=='M')

 right(i,j);

 else if(ch=='K'){

 i-=10;

 left(i,j);

 }

 else if(ch==27)

 esc();

 else {

 i-=10;

 up(i,j);

 }

 }

 }

check(int i,int j)

{

if (n>T-1750) {

gotoxy(7,5);

printf("U r Great U got Good Score, If possible Please Develop this game");

 gotoxy(24,7);

 printf("Press ENTER Key To Continue");

 getchar();

 esc();

 }

 if(i<3 || i>637 || j<3 || j>477){

 outtextxy(240,240,"YOU HAVE TOUUCHED THE BORDER");

 outtextxy(280,280,"GAME OVER");

 outtextxy(240,370,"Better Luck Next Time");

 outtextxy(200,310,"<press ENTER Key to continue>");

 getchar();

 esc();

 }

 else if(tuch(i,j)){

 rub();

 bell();

 n+=10;

 stage();

 print();

 }

 }

 int tuch(int i,int j)

 {

 int x=px,y=py,t,s,flag=0;

 for(t=x;t<=x+15;t++){

 for(s=y;s<=y+15;s++)

 if(j==s && i==t)

 flag=1;

 }

 return(flag);

 }

 rub()

 {

 setcolor(16);

 outtextxy(px,py,"¦");

 setcolor(15);

 }

 bell()

 {

 int i;

 for(i=1;i<=5;i++){

 sound(200*i);

 delay(10000);

 nosound();

 }

 }

 stage()

 {

 gotoxy(60,5);

 printf("U got %d points",n);

 sleep(1);

 gotoxy(60,5);

 printf(" ");

 }

 print()

 {

 px=random(636);

 py=random(460);

 if(px<10)

 px+=10;

 if(py<10)

 py+=10;

 outtextxy(px,py,"¦");

 }

 pause()

 {

 outtextxy(200,470,"< Press Any Key To Continue >");

 getch();

 setcolor(16);

 outtextxy(200,470,"< Press Any Key To Continue >");

 setcolor(15);

 }

 esc()

 {

 gotoxy(20,18);

 printf(" THANK YOU ");

 gotoxy(20,20);

 printf(" < Press any key to continue > ");

 getch();

 closegraph();

 exit(0);

 }

Popular posts from this blog

Good Morning Wishes

Aeza khan and Danish taimoor love story

Ayeza Khan bridal collection 2021 Ayeza Khan celebrate 10M on Instagram Danish Taimoor Danish Taimoor born on 16 February 1983 is a Pakistani actor and model.  He began his career in 2005 and seemed in "Mystery Series" (Do saal baad, Dracula); both episodes were directed by Imran khokhar and aired on Indus Vision. He is notable for his role in Urdu drama series' and telefilms in Pakistan.  Notable dramas include:  Haseena Moin's Meri Behen Maya  Mannchalay  Jab we Wed  Shert  Sari Bhool Humari thi  Aik Pal He made his film debut in Jalaibee in March 2015, directed by Yasir Jaswal

Ayeza Khan Bridal collection 2021

Pakistan Showbiz Ayeza khan celebrate 10M on Instagram Ayeza Khan & Danish Taimoor love story