#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <winbgim.h>
#include <stdio.h>
#include <conio.h>
CONST RADIUS = 30;                   //sets size of target

void wait ( int seconds )
{
    clock_t endwait;
    endwait = int(clock () + seconds * CLK_TCK);
    while (clock() < endwait) {}
}

void firecheck (int& fire2, int& firex2, int& firey2, int ytarg, int xtarg, int& score2)
{
    int firing;
    firing=0;
    while(firing<5)
    {
	if(fire2[firing] == 1)//draws bullet and checks for scoring
	{
	    setcolor(BLUE);
	    setfillstyle(SOLID_FILL , BLUE);
	    fillellipse(firex2[firing], firey2[firing], 3, 3);
	    setcolor(RED);
	    setfillstyle(SOLID_FILL , RED);
	    if ((firey2[firing] <= 30) || (firey2[firing] <= (ytarg - 15)))
		fire2[firing] = 0;
	    firey2[firing]=firey2[firing] - 10;

	    if (((firey2[firing] <= (ytarg + 5)) && (firey2[firing] > (ytarg - 5))))
	    {

		if((firex2[firing] > (xtarg - (radius / 10))) && (firex2[firing] < (xtarg + (RADIUS/10))))		    
		{
		    score2 = score2 + 30;
		    fire2[firing] = 0;
		}
		if((fire2[firing] == 1) && ((firex2[firing] > (xtarg - (radius / 2))) && (firex2[firing] < (xtarg + (RADIUS/2)))))
		{
		    score2 = score2 + 20;
		    fire2[firing] = 0;
		}
		   
		if((fire2[firing] == 1) &&((firex2[firing] > (xtarg - radius)) && (firex2[firing] < (xtarg + RADIUS))))
		{
		    score2 = score2 + 10;
		    fire2[firing] = 0;
		}
	    }
	}
	firing++;
    }
    firing=0;
}

main ()
{
    char c,msg[80],times[80];
    int	ct,ct2,ct3,ct4,radius,timer;
    int maxx,maxy,midx,midy,randy,randx,xtarg,ytarg;
    int gun,pos,fire [5]= {0, 0, 0, 0, 0}, score,firex [5]= { 0, 0, 0, 0, 0 }, firey [5]= { 0, 0, 0, 0, 0};
    int firing=0;

    ct = 0;
    ct2 = 0;
    ct3 = 0;                       //initializes variables
    score = 0;
    timer = 35;
    
    
    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(3);                                    //instructions and scoring 3 secs to see
    
    initwindow(800, 600);         
    maxx = getmaxx( );
    maxy = getmaxy( );            //initializes gaphics and finds max and mid points
    midx = (maxx/2);
    midy = (maxy/2);
    
    srand ( time (NULL) );         // Initialize random generator
    
    xtarg = midx;
    ytarg = midy;
    gun = midx;
    setcolor(RED);       
    setfillstyle(SOLID_FILL , RED);
    time_t timestart;                    //intializes time variables
    time_t timenow;
    timestart = time (NULL);            //gets time at start of game

    do
    {
	do
	{
	    int page = (getvisualpage() % 2) + 1; // alternate page: 1 and 2
	    setactivepage(page);      // draw on hidden page ...
            ct++;             //counter to move taget ever 10 loops
	    firecheck(fire [5], firex [5], firey [5], ytarg, xtarg, score);
	    sprintf(times, "time: %d", timer);
	    sprintf(msg, "score: %d", score);
	    cleardevice();
	    setfillstyle(SOLID_FILL , YELLOW);
	    setcolor(YELLOW);
	    fillellipse(xtarg, ytarg, RADIUS, RADIUS);
	    setfillstyle(SOLID_FILL , WHITE);                             //draws target and info to screen
	    setcolor(WHITE);
	    fillellipse(xtarg, ytarg, (RADIUS / 2), (RADIUS / 2));
	    setfillstyle(SOLID_FILL , RED);
	    setcolor(RED); 
	    fillellipse(xtarg, ytarg, (RADIUS / 10), (RADIUS / 10));      //draws target and info to screen
	    setfillstyle(SOLID_FILL , BLUE);
	    setcolor(BLUE);
	    fillellipse(gun, 590, 5, 5);
	    setcolor(RED);
	    setfillstyle(SOLID_FILL , RED);
	    outtextxy(700,570,msg);
	    outtextxy(20,570,times);
		
	    if(ct > 10)                       //centers target in needed and moves target
	    {
		ct = 0;
		if((xtarg > 700) || (xtarg < 50) || (ytarg < 50) || (ytarg > 500))
		{
		    xtarg=midx;
		    ytarg=midy;
		}
		pos = ((rand() % 2) / 1);            //decides weather to move taget up or down
		randx = rand() % 11;                        //decides how much to move xtarg
		randy = rand() % 11;                        //decides how much to move ytarg
		
		if(pos == 1)                     //moves positive
		{
		    xtarg = xtarg + randx;
		    ytarg = xtarg + randy;
		}
		if(pos == 0)                    //moves negative
		{
		    xtarg = xtarg - randx;
		    ytarg = xtarg - randy;
		}
	    }
	    
	    timenow = time (NULL);
	    timer = timer - (timenow - timestart);        // gets time minus timeone second ago 
	    timestart = timenow;
	    delay(0);  //  to slow gameplay
	    setvisualpage(page);     // ... now make hidden page visible
	}while((kbhit( ) == 0) && (timer>=0));               //moves target every ten counts
	
	setfillstyle(SOLID_FILL , BLUE);
	setcolor(BLUE);
	fillellipse(gun, 590, 5, 5);        //draws gun and score
	setcolor(RED);
	setfillstyle(SOLID_FILL , RED);
	outtextxy(700,570,"score: ");
	
	c = (char) getch();               // checks for user input and does actions
	if(c == KEY_RIGHT)
	    gun = gun + 5;
	if(c == KEY_LEFT)
	    gun = gun - 5;
	if(c == KEY_UP)
	{
	    firing=0;
	    while(firing<5)
	    {
		if(fire[firing]==0)
		{
		    fire[firing] = 1;
		    firex[firing] = gun;
		    firey[firing] = maxy - 20;
		    firing = 20;
		}
		firing++;
	    }
	}
    } while(timer >= 0);
    
    closegraph( );  //Ends graphical play returns to dos window

    cout << "\n\n\nYour score was " << score;
    if(score > 1700)
	cout << "\nWow really good score! Your totally punk rock!";  //tells them how good
    else if(score > 1000)
	cout << "\nGood job, your punk rock, but not the best!";
    else if(score > 500)
	cout << "\nGood";
    else if(score > 250)
	cout << "\nNot bad";
    else if(score < 150)
	cout << "\nBad game practice!";
    wait(5);
}
