SNAKE GAME
(C program)

Simple snake game. You have to catch frogs as soon as possible to win the game.
/* ##################################### # SNAKE AND FROGS IN A FIELD # # tested in Turbo C++,version 3.0 # ###############15/2/2008############# */ #include<dos.h> #include<time.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #define ESC 27 #define UP 72 #define LEFT 75 #define DOWN 80 #define RIGHT 77 int snakeHeadX=12, snakeHeadY=9, frogX=12, frogY=20; int lastSnakeHeadX=13, lastSnakeHeadY=9, newKey=RIGHT; int snakeLengthFlag, score=321, frogWaitingFlag; struct coordinate { char snakeHeadX, snakeHeadY; struct coordinate *next; } *up=NULL, *down=NULL, *ptr; void insert(int x, int y) { ptr=(struct coordinate*)malloc(sizeof(struct coordinate)); ptr->snakeHeadX=x; ptr->snakeHeadY=y; ptr->next=NULL; if(up==NULL) up=down=ptr; else up->next=ptr, up=ptr; } void Delete(int *x, int *y) { *x=down->snakeHeadX; *y=down->snakeHeadY; ptr=down->next; free(down); down=ptr; } void HideCursor() { textcolor(BLACK); gotoxy(1,1), cputs(" "), gotoxy(1,1); } void Draw() { textcolor(LIGHTRED); gotoxy(lastSnakeHeadX,lastSnakeHeadY), cputs("."); gotoxy(snakeHeadX,snakeHeadY), cputs("."); HideCursor(); delay(200); lastSnakeHeadX=snakeHeadX, lastSnakeHeadY=snakeHeadY; insert(snakeHeadX,snakeHeadY); } void UnDraw(char c) { int snakeTailX, snakeTailY; Delete(&snakeTailX,&snakeTailY); textcolor(LIGHTGREEN); gotoxy(snakeTailX,snakeTailY), cprintf("%c",c); } void MemoryFree() { while(down) ptr=down->next, free(down), down=ptr; up=NULL; } void congratulation() { int i; char s[]="CONGRATULATIONS"; snakeHeadX=1, snakeHeadY=13; lastSnakeHeadX=2, lastSnakeHeadY=13; MemoryFree(); clrscr(); for(i=1;i<=6;i++) Draw(), snakeHeadX++; for(i=1;i<=34;i++) { Draw(); snakeHeadX++; if(12<i&&i<28) UnDraw(s[i-13]); else UnDraw(' '); } MemoryFree(); exit(0); } void crash(int snakeHeadX, int snakeHeadY) { if(snakeHeadX<7 || snakeHeadX>34 || snakeHeadY<4 || snakeHeadY>23) goto down; for(ptr=down; ptr; ptr=ptr->next) if(ptr->snakeHeadX==snakeHeadX && ptr->snakeHeadY==snakeHeadY) goto down; return; down: Draw(); textcolor(WHITE); gotoxy(15,13), cprintf(" GAME OVER "); HideCursor(); MemoryFree(); delay(3000); exit(0); } void GetKey() { char k; if(kbhit()) { getkey: k=getch(); if(k==0) goto getkey; switch(k) { case ESC: MemoryFree(), exit(0); case UP : if(newKey!=DOWN ) newKey=k; break; case LEFT : if(newKey!=RIGHT) newKey=k; break; case DOWN : if(newKey!=UP ) newKey=k; break; case RIGHT: if(newKey!=LEFT ) newKey=k; break; } } if(newKey==UP) snakeHeadY--; else if(newKey==RIGHT) snakeHeadX++; else if(newKey==DOWN) snakeHeadY++; else snakeHeadX--; } void point() { score-=2*(25-frogWaitingFlag), frogWaitingFlag=1; if(score<=0) congratulation(); textcolor(WHITE); gotoxy(8,2), cprintf("%3d frogs required to win.",score); random: frogX=rand()%26+8, frogY=rand()%18+5; for(ptr=down; ptr; ptr=ptr->next) if(ptr->snakeHeadX==frogX && ptr->snakeHeadY==frogY) goto random; textcolor(LIGHTGREEN), gotoxy(frogX,frogY), cputs("‰"); } void DrawField() { int i; textmode(C40); textcolor(CYAN); gotoxy(6,3); for(i=6;i<36;i++) cputs("."); for(i=4;i<24;i++) gotoxy(6,i), cputs("."); gotoxy(36,3); for(i=4;i<24;i++) gotoxy(35,i), cputs("."); gotoxy(6,24); for(i=6;i<36;i++) cputs("."); textcolor(RED); gotoxy(14,1),cputs("_SNAKE GAME_"); gotoxy(4,25),cputs("http://programs.on.googlepages.com"); } void main() { int i; snakeLengthFlag=1; randomize(); DrawField(); for(i=1;i<=3;i++) snakeHeadX++, Draw(); frogWaitingFlag=25, point(); while(1) { if(frogWaitingFlag==25) { gotoxy(frogX,frogY), putch(' '); point(); } else frogWaitingFlag++; if(snakeLengthFlag==12) snakeLengthFlag=1; else UnDraw(' '), snakeLengthFlag++; GetKey(); if(snakeHeadX==frogX && snakeHeadY==frogY) { HideCursor(); sound(2000), delay(10), nosound(); point(); } crash(snakeHeadX,snakeHeadY); fflush(stdin); Draw(); } } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report suggestions: programs.on@gmail.com * ********************************************257*lines**/