void check()
{
	int diff1= mod((t[1][0]-x))<20;
	int   diff2= mod((t[1][1]-y))<20;
	int diff3= mod((t2[1][0]-x))<20;
	int   diff4= mod((t2[1][1]-y))<20;
	if(diff1 && diff2)
	{	if(sound==1)
		PlaySound("bell1.WAV",NULL,SND_FILENAME|SND_ASYNC);
		printed=0;
				
		size++;
		if(no_of_pl==1)
		{
			score=score+n*size*intelligence;
		
			vitoa(score,str);

		}
		vitoa(size,si);
			vitoa(size,ch);
		
			t[size][1]=t[size-1][1];
	
			t[size][0]=t[size-1][0];
			
		if(size==20 && lan==1)
			screen=3;
		
	}
	if(no_of_pl==2)	
	 if(diff3 && diff4)
	{	if(sound==1)
		PlaySound("bell1.WAV",NULL,SND_FILENAME|SND_ASYNC);
		printed=0;
				
		size2++;
		vitoa(size2,si2);
		
	
			t2[size2][1]=t2[size2-1][1];
			t2[size2][0]=t2[size2-1][0];
	
		
		
		
	}



}
static void print_status_entry(game_t *p_game, int n, int16_t x, int16_t y, int32_t val)
{
  char buf[8];

  /* FIXME! */
  vitoa(val, buf, n, '0');
  print_font(&p_game->small_font, vRGB(128,255,255), x, y, buf);
}
void screen6()
{
	char speed[6],inte[6];
	glPointSize(1.0);
	write("GAME SOUND : ",100,600);
	write("YES ",450,600);
	write("NO ",600,600);
	write("SNAKE SPEED :",100,450);
	vitoa(n,speed);
	write(speed,400,450);
	write("SKILL LEVEL OF RAT:",100,300);
	vitoa(intelligence,inte);
	write(inte,500,300);
	write("NO OF PLAYERS :",100,200);
	write("ONE",480,200);
	write("TWO",680,200);
	glLoadIdentity();
	carr=1;
	rectangle2(400,500);
	rectangle2(500,350);
	carr=0;
	rectangle2(400,400);
	
	rectangle2(500,250);
	carr=3;
	rectangle2(450,200);
	rectangle2(650,200);
	rectangle2(420,600);
	rectangle2(570,600);
	if(no_of_pl==1)
		rectangle3(455,205);
	else
		rectangle3(655,205);
	if(sound==1)
		rectangle3(425,605);
	else
		rectangle3(575,605);
	backbutton();
	glFlush();
}
void main(int argc,char ** argv)
{

	fp=fopen("gfile.dat","rb");
	if(fp!='\0')
	{
		fread(&best_score,4,1,fp);vitoa(best_score,bstr);	
		fclose(fp);	
	}
	srand(20);
	srand(rand());


		state[1]='6';
		t[1][0]=140;
		t[1][1]=20;
		t[2][0]=120;
		t[2][1]=20;
		state[2]='6';
	size=2;

	
		state2[1]='6';
		t2[1][0]=140;
		t2[1][1]=660;
		t2[2][0]=120;
		t2[2][1]=660;
		state2[2]='6';
	size2=2;
		glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
	glutInitWindowSize(1024,768);
	glutInitWindowPosition(0,0);
	glutCreateWindow("game");
	glutDisplayFunc(display);

	glutMouseFunc(mouse);
	glutPassiveMotionFunc(passivemotion);
	glutSpecialFunc(spec_keys);
	glutKeyboardFunc(keyboard);
	glClearColor(0.3,0.2,0.6,1.0);
	init();
	glutFullScreen();
	 
	glutMainLoop();

}
void screen2()
{
	if(po==0 && sound==1)
	{
		PlaySound("GAMESOUND.WAV",NULL,SND_FILENAME|SND_ASYNC);po=1;
	}
			hurdle();
		
			snake_or_rat_tra(2);
			ck_if_ea_it();
			update_sna();
			eatable();
		check();
	
		if(hurdle_check(t[1][0],t[1][1]) && no_of_pl==1 )
		{
			
			
			glutPostRedisplay();
			if(sound==1)
			PlaySound("1.WAV",NULL,SND_FILENAME|SND_ASYNC);
			plife=plife-1;
			if(plife==0)
			{
			screen=3;
			screen3();
			}		
		}
		if(no_of_pl==2)
		{
		
		if(hurdle_check(t[1][0],t[1][1]))
		{
			
			
		//	glutPostRedisplay();
			if(sound==1)
			PlaySound("1.WAV",NULL,SND_FILENAME|SND_ASYNC);
			if(compu==0)
			{
				t[size][0]=0;t[size][1]=0;state[size]=0;
				size--;
			}
			vitoa(size,si);
			//		screen=3;
	//		screen3();
		}
			if(compu==1)
			computer();
		if(hurdle_check(t2[1][0],t2[1][1]))
		{
			
		

		//	glutPostRedisplay();
			if(sound==1)
			PlaySound("1.WAV",NULL,SND_FILENAME|SND_ASYNC);
				
			if(compu==0)
			{
				t2[size2][0]=0;t2[size2][1]=0;state2[size2]=0;
				size2--;
			}

			vitoa(size2,si2);			
		//		screen=3;
//			screen3();
		}

		if(size==0 || size2==0 || count==20 )
		{		screen=3;
				screen3();
		}

		}
		if(no_of_pl==1)
		{
		write("LIFE",850,20);
		if(plife==3)
		{
			for(i=0;i<3;i++)
				lifedraw(950+i*25,20);
		}
		else if(plife==2)
		{
			for(i=0;i<2;i++)
				lifedraw(950+i*25,20);
		}
		else if(plife==1)
		{
			for(i=0;i<1;i++)
				lifedraw(950+i*25,20);
		}
		}
}
void screen3()
{
	
	char s1[20],s2[20];
	plife=3;
	if(confail==1)
	{
		write("Connection Failed",400,350);
		backbutton();

	}
	else if(confail==0)
	{
	if(no_of_pl==1 && lan==0)
	{
	tit=1;
	write("GAME OVER!!!..",330,600);
	tit=0;
	vitoa(score,str);
	vitoa(best_score,bstr);
	write("LAST BEST SCORE :",100,400);
	write(bstr,500,400);
	write("YOUR SCORE :",100,300);
	write(str,390,300);
	if(best_score <=score)
	{
		
		
		write("CONGRATS !!!!",100,350);
	}
	
	glLoadIdentity();
	glBegin(GL_POLYGON);
	glColor3f(0.0,0.0,0.0);
		glVertex2i(41,52);
		glVertex2i(174,52);
		glVertex2i(174,104);
		glVertex2i(41,104);
		
	glEnd();
	glBegin(GL_POLYGON);
	glColor3f(0.2,0.2,0.2);
		glVertex2i(44,50);
		glVertex2i(176,50);
		glVertex2i(176,102);
		glVertex2i(44,102);
		
	glEnd();
	if(lan!=1)
	{
	write("RETRY",60,60);
	backbutton();
	}
	}
	else if(no_of_pl==2 || lan==1)
	{
		if(compu==1)
		{
			write("player 1 score:",100,700);
			vitoa(size,s1);
			write(s1,400,700);
			write("Computer score:",100,650);
			vitoa(size2,s2);
			write(s2,400,650);
			if(size>size2)
				write("PLAYER1 WON THE GAME",100,600);
			else if(size==size2)
				write("DRAW",100,600);
			else
				write("Computer WON THE GAME",100,550);
		}
		else
		{
		write("player 1 score:",100,700);
		vitoa(size,s1);
		write(s1,400,700);
		write("player 2 score:",100,650);
		vitoa(size2,s2);
		write(s2,400,650);
		if(size>size2)
			write("PLAYER1 WON THE GAME",100,600);
		else if(size==size2)
			write("DRAW",100,600);
			else
				write("PLAYER2 WON THE GAME",100,550);
		}
		if(lan!=1)
		{
glLoadIdentity();
	glBegin(GL_POLYGON);
	glColor3f(0.0,0.0,0.0);
		glVertex2i(41,52);
		glVertex2i(174,52);
		glVertex2i(174,104);
		glVertex2i(41,104);
		
	glEnd();
	glBegin(GL_POLYGON);
	glColor3f(0.2,0.2,0.2);
		glVertex2i(44,50);
		glVertex2i(176,50);
		glVertex2i(176,102);
		glVertex2i(44,102);
		
	glEnd();
	write("RETRY",60,60);
	backbutton();
		}
	
	}
	}
	
}
void mouse(int btn,int states,int x,int y)
{
	
	if(screen==1 && btn==GLUT_LEFT_BUTTON && states==GLUT_DOWN)
	{
		switch(transvar)
		{
		case 0:if(flag==0)
			   {
			
			    	glutTimerFunc(100,timer,n);
			 	
			   }	
			   screen=2;
			   break;
		case 1:no_of_pl=2;
			   compu=1;
			   screen=2;
			   if(flag==0)
			   glutTimerFunc(100,timer,20);
				break;
		case 2:screen=6;
				break;
		case 3:	screen=5;
				break;
		case 4:screen=7;
				break;
		
		}
		y=768-y;
		if((x>=850 && x<=950)&& (y>=50 && y<=100))
		exit(0);
	}
	if(screen==6)
	{
	y=768-y;
	
	if(btn==GLUT_LEFT_BUTTON && states==GLUT_DOWN)
	{	
		if((x>=400 && x<=420) && (y>=500 && y<=520))
		{	n=n+5;
		
			if(sound==1)	
			PlaySound("button9.WAV",NULL,SND_FILENAME|SND_ASYNC);
		}
		
		else if((x>=400 && x<=420) && (y>=400 && y<=420))
		{	n=n-5;
		
			if(sound==1)	
			PlaySound("button9.WAV",NULL,SND_FILENAME|SND_ASYNC);
		}

		else if((x>=500 && x<=520) && (y>=350 && y<=370))
		{	if(intelligence<4)
			intelligence++;
			
		
			if(sound==1)	
			PlaySound("button9.WAV",NULL,SND_FILENAME|SND_ASYNC);
		}
		else if((x>=500 && x<=520) && (y>=250 && y<=270))
		{	if(intelligence>1)
			intelligence--;
			
		
			if(sound==1)	
			PlaySound("button9.WAV",NULL,SND_FILENAME|SND_ASYNC);
		}
		else if((x>=450 && x<=470)&& (y>=200 && y<=220))
		{
			no_of_pl=1;
		}
		else if((x>=650 && x<=670)&& (y>=200 && y<=220))
		{
			no_of_pl=2;
		}
		else if((x>=420 && x<=440)&& (y>=600 && y<=620))
		{
			sound=1;
		}
		else if((x>=570 && x<=590)&& (y>=600 && y<=620))
		{
			sound=0;
		}
		else if((x>=850 && x<=950)&& (y>=50 && y<=100))
		{
			if(flag==0)
			{
				flag=1;	
				glutTimerFunc(100,timer,n);
			}
		
			screen=1;
			glLoadIdentity();
			glPointSize(20.0);
		}
	}

		glutPostRedisplay();
		
		po=0;
		
		
	}

	if((screen==5||screen==7)&& btn==GLUT_LEFT_BUTTON && states==GLUT_DOWN)
	{
		y=768-y;
		if((x>=850 && x<=950)&& (y>=50 && y<=100))
			screen=1;
	}
	if(screen==3  && confail==0 && btn==GLUT_LEFT_BUTTON && states==GLUT_DOWN)
	{
		y=768-y;
		if((x>=44 && x<=176)&&(y>=50 && y<=102))
		{

			screen=2;
			size=2;
			po=0;
			state[1]='6';
			t[1][0]=140;
			t[1][1]=20;
			t[2][0]=120;
			t[2][1]=20;
			state[2]='6';
			size=2;
			x=0;y=0;
			glLoadIdentity();
			for(i=3;i<=50;i++)
			{
				state[i]=0;
				t[i][1]=0;
			}
			i=1;j=0;po=0;pro=0;
			if(no_of_pl==2)
			{	

				state2[1]='6';
				t2[1][0]=140;
				t2[1][1]=660;
				t2[2][0]=120;
				t2[2][1]=660;
				state2[2]='6';
				size2=2;
		
				for(i=3;i<=50;i++)
				{
					state2[i]=0;
					t2[i][1]=0;
				}
				count=0;
				vitoa(size,si);
				vitoa(size2,si2);
			}
			if(no_of_pl==1)
			{
				if(best_score < score)
				{	
					best_score=score;
		
					vitoa(best_score,bstr);
				}
				score=0;vitoa(score,str);
			}
		}
		else if((x>=850 && x<=950)&& (y>=50 && y<=100))
		{
			exit(0);
		}
	}
	 else if(screen==3 && confail==1 && btn==GLUT_LEFT_BUTTON && states==GLUT_DOWN)
	{
		 y=768-y;
		if((x>=850 && x<=950)&& (y>=50 && y<=100))
		{
			confail=0;
			screen=1;
		}
	
	}
	
}
void keyboard(unsigned char key,int x,int y)
{
	
	
	
	if(screen==1)
	{
		if(key=='1')
		{		
			if(flag==0)
			{
			
				glutTimerFunc(100,timer,n);
				
			}	
			screen=2;
		}
		else if(key=='2')
		screen=5;
		
		
		else if(key=='3')
			screen=6;

		else if(key=='4')
		screen=7;
		else if(key=='5')
		{
			no_of_pl=2;
			compu=1;
			screen=2;
			if(flag==0)
			glutTimerFunc(100,timer,20);
			
		
		}
	//	PlaySound("button1.WAV",NULL,SND_FILENAME|SND_ASYNC);
				po=0;
	
	}
	else if(screen==5 && key=='b')
	{	screen=1;
		PlaySound("button1.WAV",NULL,SND_FILENAME|SND_ASYNC);
				po=0;
	}
	else if(screen==6  )
	{
		if(key=='b')
		{
			if(flag==0)
			{flag=1;	
			glutTimerFunc(100,timer,n);}
		
		screen=1;glLoadIdentity();glPointSize(20.0);
		
		
		}
		if(key=='1')
			no_of_pl=1;
		else if(key=='2')
			no_of_pl=2;
			
		if(key=='y'||key=='Y')
			sound=1;
		else if(key=='n'||key=='N') 
			sound=0;
		
		PlaySound("button1.WAV",NULL,SND_FILENAME|SND_ASYNC);
				po=0;
	}
	else if(screen==7 && key=='b')
		screen=1;
	
	else if(screen==3 && key=='e'||key=='E')
	{
		screen=2;
		size=2;
		po=0;
		state[1]='6';
		t[1][0]=140;
		t[1][1]=20;
		t[2][0]=120;
		t[2][1]=20;
		state[2]='6';
		size=2;
		x=0;y=0;
		glLoadIdentity();
		for(i=3;i<=50;i++)
		{
			state[i]=0;
			t[i][1]=0;
		}
		i=1;j=0;po=0;pro=0;
		if(no_of_pl==2)
		{	

			state2[1]='6';
			t2[1][0]=140;
			t2[1][1]=660;
			t2[2][0]=120;
			t2[2][1]=660;
			state2[2]='6';
			size2=2;
		
			for(i=3;i<=50;i++)
			{
				state2[i]=0;
				t2[i][1]=0;
			}
			count=0;
			vitoa(size,si);
			vitoa(size2,si2);
		}
		if(no_of_pl==1)
		{
			if(best_score < score)
			{	
				best_score=score;
		
				vitoa(best_score,bstr);
			}
			score=0;vitoa(score,str);
		}
		

	}
	
		
	if(screen==2 && (key=='0'|| key==' '|| key=='p'))
	{
		screen=4;
	}
	else if(screen==4 && (key=='0'|| key==' '|| key=='p'))
	{
		screen=2;
	}
	
	
	
	
	if(key=='w')
	{	if(state2[1]!=DOWN)
		state2[1]=UP;
	}

	else if(key=='d')
	{
		if(state2[1]!=LEFT)
		state2[1]=RIGHT;
	}
		
	else if(key=='s')
	{	
		if(state2[1]!=UP)
		state2[1]=DOWN;
	}

	else if(key=='a')
	{
		if(state2[1]!=RIGHT)
		state2[1]=LEFT;
	}


	else if(key=='q'||key=='Q') 
	{	
		fp=fopen("gfile.dat","wb");
		fwrite(&best_score,4,1,fp);
		fclose(fp);
		exit(0);
			
	
	}
		
	if(screen!=2)
	glutPostRedisplay();

	



}