#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <winbgim.h>
#include <stdio.h>
#include <conio.h>
CONST INT RADIUS = 30;
CONST INT MAXX = 799;
CONST INT MAXY = 599;
CONST INT NUM1 = 5;
CONST INT NUM2 =5;
CONST INT NUM3 = 5;
CONST INT NUM4 =5;
CONST INT NUM5 = 5;
CONST INT AST =30;
CONST INT AST2 =30;
CONST INT AST3 =30;
CONST INT AST4 =30;
CONST INT SMALL = 5;
CONST INT MED = 10;
CONST INT LARGE =20;



void wait ( int seconds );
void drawgun (int& gunx, int& guny,int dir);
void turn_right(int& dir);
void turn_left(int& dir);
void userinput (int& gunx, int& guny,int& dir,int& firing,int firex[],int NUM,int firey[],int NUM2,int fire[],int NUM3,int fird[],int NUM4);
void drawbullets(int& firing,int firex[],int NUM1,int firey[],int NUM2,int fire[], int NUM3,int fired[],int NUM4,int dir);
void scoring(int& firing,int firex[],int NUM1,int firey[],int NUM2,int fire[], int NUM3,int fired[],int NUM4,int& score,int astriods[],int AST,int astriodx[],int AST2);
void drawastriod (int astriods[],int AST,int astriodx[],int AST2,int astriodr[],int AST3,int astriodm[],int AST4);
void createast(int astriods[],int AST,int astriodx[],int AST2,int astriodr[],int AST3,int astriodm[],int AST4,int ct);
void moverast(int astriods[],int AST,int astriodx[],int AST2,int astriodr[],int AST3,int astriodm[],int AST4);



int main ()
{
    char c,msg[80],times[80];
    int	radius,lives,dir;
    int midx,midy,randy,randx;
    int gunx,guny,pos,fire [NUM1]= {0, 0, 0, 0, 0}, score;
    int firex [NUM1]= { 0, 0, 0, 0, 0 }, firey [NUM1]= { 0, 0, 0, 0, 0};
    int firing=0, fired[NUM1];
    int astriods[30],astriodx[30],astriodr[30],astriodm[30],ct=0,ct2=0;

    dir=0;
    lives=3;             //initializes variables
    score = 0;
    srand ( time (NULL) ); 
    do
    {
	astriods[ct2]=0;
	ct2++;
    }while(ct2<30);
    ct2=0;
    do
    {
	astriodx[ct2]=0;
	ct2++;
    }while(ct2<30);
    ct2=0;
    do
    {
	astriodr[ct2]=0;
	ct2++;
    }while(ct2<30);
    ct2=0;
    do
    {
	astriodm[ct2]=0;
	ct2++;
    }while(ct2<30);
    
    cout << "\nPUSE SIDE ARROWS TO CONTROL AND UP TO SHOOT";
    cout << "\n you have 35 seconds to get a high score.";
    cout << "\nLargest circle = 10 pts Medium circle = 20 pts Small circle = 30 pts";
    wait(2);                                    //instructions and scoring 3 secs to see
    
    initwindow(800, 600);         
    midx = (MAXX/2);
    midy = (MAXY/2);
    
    srand ( time (NULL) );         // Initialize random generator
   
    gunx = midx;
    guny = midy;
    setcolor(RED);       
    setfillstyle(SOLID_FILL , RED);
    ct2=0;
    do
    {
	
	drawgun(gunx,guny,dir);
	userinput(gunx,guny,dir,firing,firex,NUM2,firey,NUM3,fire,NUM1,fired,NUM4);
	do
	{
	    createast(astriods,AST,astriodx,AST2,astriodr,AST3,astriodm,AST4,ct);
	    if(ct2>100)
	    {
		ct2=0;
		cleardevice();
		drawgun(gunx,guny,dir);
		moverast(astriods,AST,astriodx,AST2,astriodr,AST3,astriodm,AST4);
		drawbullets(firing,firex,NUM1,firey,NUM2,fire,NUM3,fired,NUM4,dir);
		drawastriod(astriods,AST,astriodx,AST2,astriodr,AST3,astriodm,AST4);
	    }
	    scoring(firing,firex,NUM1,firey,NUM2,fire,NUM3,fired,NUM4,score,astriods,AST,astriodx,AST2);
	    sprintf(msg, "score: %d", score);
	    setcolor(RED);
	    setfillstyle(SOLID_FILL , RED);
	    outtextxy(2,2,msg);
	    ct2++;

	    
	    if(ct<1000)
	    {
		ct++;
	    }
	    else
	    {
		ct=0;
	    }
	    
	}while(kbhit() == 0);

	
    }while(lives>0);
    
    getch();
    closegraph();
    return EXIT_SUCCESS;

}


void wait ( int seconds )
{
    clock_t endwait;
    endwait = int(clock () + seconds * CLK_TCK);
    while (clock() < endwait) {}
}



void moverast(int astriods[],int AST,int astriodx[],int AST2,int astriodr[],int AST3,int astriodm[],int AST4)
{
    int astnum=0;
    while(astnum<30)
    {
	if(astriods[astnum]==1)
	{
	    astriodx[astnum] = (astriodx[astnum] + 1);
	}
	astnum++;
    }
    astnum=0;
    while(astnum<30)
    {
	if(astriods[astnum]!=0)
	{
	    if((astriodx[astnum])>MAXX)
		astriodx[astnum] = 0;
	    if((astriodx[astnum])< 0)
		astriodx[astnum] = MAXX;
	}
	astnum++;
    }
}


void createast(int astriods[],int AST,int astriodx[],int AST2,int astriodr[],int AST3,int astriodm[],int AST4,int ct)
{
    int astnum=0,randx;
    if(ct==0)
    {
	while(astnum<5)
	{
	    if(astriods[astnum]==0)
	    {
		randx = rand() % MAXX;
		astriods[astnum] = 1;
		astriodx[astnum] = randx;
		astriodr[astnum] = LARGE;
		astriodm[astnum] = ((randx%3)+1);
		astnum = 2000;
	    }
	    astnum++;
	}
	
    }
	    
}

void userinput (int& gunx, int& guny,int& dir,int& firing,int firex[],int NUM1,int firey[],int NUM2,int fire[], int NUM3,int fired[],int NUM4)
    
{
    char c;
    c = (char) getch();               // checks for user input and does actions
    if(c == KEY_RIGHT)
	turn_right(dir);
    if(c == KEY_LEFT)
	turn_left(dir);
    if(c == KEY_UP)
    {
	if(dir==3)
	    guny = guny - 2;
	if(dir==2)
	    gunx = gunx - 2;
	if(dir==1)
	    guny = guny + 2;
	if(dir==0)
	    gunx = gunx + 2;
    }
    if(c == KEY_DOWN)
    {
	if(dir==3)
	    guny = guny + 1;
	if(dir==2)
	    gunx = gunx + 1;
	if(dir==1)
	    guny = guny - 1;
	if(dir==0)
	    gunx = gunx - 1;
    }
    if(c == 120)
    {
	firing=0;
	while(firing<5)
	{
	    if(fire[firing]==0)
	    {
		fire[firing] = 1;
		firex[firing] = gunx;
		firey[firing] = guny;
		fired[firing] = dir;
		firing = 20;
	    }
	    firing++;
	}
    }
    if (gunx > MAXX)
	gunx = 0;
    if (gunx < 0)
	gunx = MAXX;
    if (guny > MAXY)
	guny = 0;
    if (guny < 0)
	guny = MAXY;
}

void drawastriod (int astriods[],int AST,int astriodx[],int AST2,int astriodr[],int AST3,int astriodm[],int AST4)
{
    int astnum=0;
    
    while(astnum<30)
    {
	if(astriods[astnum]!=0)
	{
	    if((astriodr[astnum])== SMALL)
	    {
		setcolor(GREEN);
		setfillstyle(SOLID_FILL , GREEN);
		fillellipse((astriodx[astnum]),((((astriodm[astnum])*(astriodx[astnum]))%MAXY)), (astriodr[astnum]), (astriodr[astnum]));
	    }
	    if((astriodr[astnum])== MED)
	    {
		setcolor(BLUE);
		setfillstyle(SOLID_FILL , BLUE);
		fillellipse((astriodx[astnum]),((((astriodm[astnum])*(astriodx[astnum]))%MAXY)), (astriodr[astnum]), (astriodr[astnum]));
	    }
	    if((astriodr[astnum])== LARGE)
	    {
		setcolor(YELLOW);
		setfillstyle(SOLID_FILL , YELLOW);
		fillellipse((astriodx[astnum]),(((astriodm[astnum])*(astriodx[astnum]))%MAXY), (astriodr[astnum]), (astriodr[astnum]));     
	    }
	}
	astnum++;
    }
}

void drawgun (int& gunx, int& guny,int dir)
{
    setcolor(RED);
    if(dir==0)
    {
	line(gunx, guny, (gunx-5), (guny-5));
	line(gunx, guny, (gunx-5), (guny+5));
    }
    if(dir==1)
    {
	line(gunx, guny, (gunx-5), (guny-5));
	line(gunx, guny, (gunx+5), (guny-5));
    }
    if(dir==2)
    {
	line(gunx, guny, (gunx+5), (guny-5));
	line(gunx, guny, (gunx+5), (guny+5));
    }
    if(dir==3)
    {
	line(gunx, guny, (gunx-5), (guny+5));
	line(gunx, guny, (gunx+5), (guny+5));
    }
}

void drawbullets (int& firing,int firex[],int NUM1,int firey[],int NUM2,int fire[], int NUM3,int fired[],int NUM4,int dir)
{
    firing=0;
    while(firing<5)
    {
	if(fire[firing]!=0)
	{
	    if((fired[firing])==3)
		firey[firing] = firey[firing]-1;
	    if((fired[firing])==2)
		firex[firing] = firex[firing]-1;
	    if((fired[firing])==1)
		firey[firing] = firey[firing]+1;
	    if((fired[firing])==0)
		firex[firing] = firex[firing]+1;
	    putpixel(firex[firing],firey[firing],RED);
	}
	firing++;
    }
}



void scoring(int& firing,int firex[],int NUM1,int firey[],int NUM2,int fire[], int NUM3,int fired[],int NUM4,int& score,int astriods[],int AST,int astriodx[],int AST2)
{
    firing=0;
    while(firing<5)
    {
	if(fire[firing]!=0)
	{
	    if((firex[firing])>MAXX)
		fire[firing]=0;
	    if((firey[firing])>MAXY)
		fire[firing]=0;
	    if((firex[firing])<0)
		fire[firing]=0;
	    if((firey[firing])<0)
		fire[firing]=0;
	    if((getpixel((firex[firing]+1),(firey[firing])))== YELLOW)
	{
	    fire[firing]=0;
	    score=score+10;
	}
	}
		     firing++;
    }
	
}



void turn_right(int& dir)
{
    dir = dir + 1;
    if (dir == 4)
	dir = 0;
}


void turn_left(int& dir)
{
    dir = dir-1;
    if (dir < 0)
	dir = 3;
}
