
			/******************************************/

			/*  Elaine Chan                           */

			/*                                        */

            /*  HW6                                   */

			/*  CSC 142                               */

			/*  email address:                        */

			/*      er-chan@yahoo.com                 */

			/*                                        */

			/*              Space Invaders            */

			/*                                        */

			/******************************************/ 







#include "gp142.h"

#include <stdio.h>

#include <math.h>



#include <time.h>

	/* 

	* <time.h> is included so the current time can be accessed

	* to initialize the random number generator.  

	*/



/* Logical constants */

#define TRUE    		1

#define FALSE   		0



/* 

 * For many of the GP142 functions, a line width of

 * zero means to fill the shape being drawn.  The

 * constant FILL is used in function calls when a solid

 * shape is desired.

 */

#define FILL 0





/*

 * Symbolic constants.  Change as needed.

 */



/* Menu buttons: */



/* Top-left coordinates of button palette */

#define MENU_TOP		(GP142_YMAX - 5)

#define MENU_LEFT		(-GP142_XMAX + 5)



/* Height and width of buttons in pixels */

#define BUTTON_HEIGHT  	25

#define BUTTON_WIDTH 	75



/* Number of buttons */

#define NUM_BUTTONS 	3



/*   Mouse click in some button */

#define BUTTON_CLICK 1



/*   Mouse click elsewhere */

#define OTHER_CLICK 0



/* Height and width of ship */

#define SHIP_HEIGHT 24

#define SHIP_WIDTH  18



/* Position and size of video screen */

#define BOARDEDGE 50

#define SCREEN_X (-GP142_XMAX+2*BOARDEDGE)

#define SCREEN_Y (-GP142_YMAX+BOARDEDGE)

#define SCREEN_WIDTH (GP142_XMAX + GP142_XMAX - 3*BOARDEDGE)

#define SCREEN_HEIGHT (GP142_YMAX + GP142_YMAX - 2*BOARDEDGE)

#define SHIP_LOCATION (-GP142_YMAX+BOARDEDGE) + 15



/* Size of alien ships and time between their movements */

#define ALIEN_WIDTH 10

#define TIME_PER_ATTACK 10



/* Aliens start off this many levels above the ship */

#define DIST_TO_SHIP 7    



/* Number of stars and aliens on the screen */

#define NUMBER_OF_STARS 100

#define NUMBER_OF_ALIENS 10



/* Each alien should be represented as a structure "space_alien" */

typedef struct{int alive;int color; int hits; 
              int x; int y;}space_alien; /* new (alive=0 dead) */ 



/*****************************************************************************



     FUNCTION PROTOTYPES



*****************************************************************************/



/* Initialize random number generator. */

void initialize_random_numbers();



/*  create and draw the outer space background */

void create_background(GP142_point background[]);

void draw_background(GP142_point background[]);



/* Draw the buttons */

void draw_buttons();

/* instructions on top of screen */
void draw_instructions(); /* new */


/* Return kind of mouse click (in a button, etc.) */

int classify_mouse_click(int mouse_x, int mouse_y);



/* Process button click at location x, y.  The mouse only matters */

/* when clicking on buttons. */

void handle_button(int x, int y, int *ship_speed, int *quit);



/* Process keyboard press which moves ship or fires ship */

//void handle_key(char key_pressed, int *ship_direction, int *shot);
void handle_key(char key_pressed, int *ship_direction, int shot[3]); /*new */


/* Update ship's x, y coordinates, given current x, y */

/* location, direction (+1 or -1), and speed in pixels per tick. */

void move_ship(int *x, int *y, int *direction, int speed);



/* Draw all aliens given the array which keeps track of space aliens */

void draw_aliens( space_alien aliens[]); /* new */



/* Draw the ship at position x, y with the given color */

void draw_ship(int x, int y, int color);



/* Display error message if internal error is detected. */

void something_is_wrong (void);



/* Initializes the array of space aliens */

void create_aliens( space_alien aliens[]); /* new */



/* Moves the aliens down toward the ship based on the timer */

void update_aliens(space_alien aliens[], int *timer, int *ship_x);  



/* Causes the ship to shoot */

void fire_shot(space_alien aliens[], int *car_x);
 

/* Causes the ship to shoot 3 aliens at once */

void fire_shot3in1(space_alien aliens[], int *car_x);



/*****************************************************************************



     MAIN



*****************************************************************************/



/*

 * Main program contents:

 *   - Major program variables that persist throughout execution

 *   - Event loop that processes GP142 events until the user quits,

 *     and updates variables and redraws the window as needed.

 */

int main(void)

{

	/* Event handling and buttons: */

	int  quit;				/* = "user has selected quit"				*/

	int  event;				/* the most recent GP142 event				*/

	char key_pressed;		/* last keyboard character from key event	*/

	int  mouse_x, mouse_y;	/* x and y coordinates of latest mouse event*/

	int  click_kind;		/* kind of mouse click (button, etc.)		*/



	/* Information about the ship											*/

	int ship_x, ship_y;		/* current position of the ship					*/

	int ship_direction;		/* current direction: +1=right, -1=left			*/

	int ship_color;			/* color of the ship							*/

	int ship_speed;			/* speed in pixels per tick						*/	

	

	int timer = 0;			/* Keeps track of how often aliens attack		*/

	//int shot = FALSE;		/* Keeps track of if a shot was just fired		*/
    int shot[3]={FALSE,FALSE,FALSE}; /* new  for 3 kinds of bullets*/
	

	/* An array of points to keep track of the stars in the background */

	GP142_point background[NUMBER_OF_STARS];	



	/* Stores all the information about all the aliens */

	space_alien aliens[NUMBER_OF_ALIENS]; /* new alive=0 dead */



	initialize_random_numbers();



	/* Initialize graphics package, hide the text window, clear the graphics window	*/

	/* turn logging off, and start animation.			*/

	GP142_open();

	GP142_logging(LOG_OFF);

	GP142_clear();

	GP142_show_text(0); 

	GP142_animate(ANI_RUN);



	/* set initial conditions for the ship */

	ship_x = 0;

	ship_y = SHIP_LOCATION;

	ship_direction = +1;

	ship_speed = 7;

	ship_color = ICE_BLUE;

	

	/* Draw background, buttons, ship, and aliens */

	create_background(background);	

	draw_background(background);

	draw_buttons( );
	
	draw_instructions(); 

	create_aliens( aliens);

	draw_aliens(aliens); 

	draw_ship(ship_x,ship_y,ship_color);




	/*

	 * Main event loop:

	 * ---- ----- -----

	 * Wait for the next user action, decode it, and call an

	 * appropriate function to handle it.  Repeat until the

	 * user selects quit.

	 */

	quit = FALSE;

	while (!quit) {

		

		/* get next event */

		event = GP142_await_event(&mouse_x, &mouse_y, &key_pressed);

		

		/* perform appropriate action depending on kind of event */

		switch (event) {

		case GP142_QUIT:

			/* Quit selected */

			quit = TRUE;

			break;

			

		case GP142_KBD:

			/* Key pressed.  Determine if the user is trying to move	*/

			/* the ship or shoot from the ship.						 	*/

			/* Note that scanf CANNOT be used with GP142.				*/

//			handle_key(key_pressed, &ship_direction, &shot);

			handle_key(key_pressed,&ship_direction,shot);  /* new */
			break;

			

		case GP142_MOUSE:

			/* Mouse click.  Determine if in a menu button	*/

			/* or elsewhere and process accordingly.		*/

			click_kind = classify_mouse_click(mouse_x, mouse_y);

			if (click_kind == BUTTON_CLICK) {

				handle_button(mouse_x, mouse_y, &ship_speed, &quit);

			} else {

				/* the only mouse clicks that do anything are the buttons */

			}

			break;

			

		case GP142_PERIODIC:

			/* Timer interval event.  Update everything	*/

			/* that needs to change as time advances.	*/

			update_aliens(aliens, &timer,&ship_x); 

			move_ship(&ship_x, &ship_y, &ship_direction, ship_speed);

			timer++; 
			
			break;

			

		default:

			/* unknown event */

			break;

			

		}  /*end switch */



		/* Redraw everything */

		

		draw_buttons();

		draw_instructions();

		draw_background(background);

		draw_ship(ship_x,ship_y,ship_color);

		draw_aliens( aliens); 

//		if (shot) { 

//			fire_shot(aliens, &ship_x);		/* If the user fired a shot...	*/

//			shot = FALSE;	}

	if (shot[0]) { 

			fire_shot(aliens, &ship_x);		/* If the user fired a shot...	*/

			shot[0] = FALSE;	}


    if(shot[1]){
		
		fire_shot3in1(aliens,&ship_x); /* hits three aliens at once */
	
		shot[1] =FALSE; }













	}  /* end event loop */

	

	

	/* Termination: shut down graphics window and exit */

	GP142_close();

	return 0;

}  /*end main */





/*****************************************************************************



     BACKGROUND LANDSCAPE



*****************************************************************************/





/* Positions the stars randomly in outer space */

void create_background(GP142_point background[])

{

	int i;

	int star_x, star_y;

	

	for (i = 0; i < NUMBER_OF_STARS; i++)

	{

		star_x = -GP142_XMAX+2*BOARDEDGE + (rand() % (GP142_XMAX-BOARDEDGE+GP142_XMAX+2*BOARDEDGE));	

		star_y = -GP142_YMAX+BOARDEDGE + (rand() % (GP142_YMAX-BOARDEDGE+GP142_YMAX+BOARDEDGE));

		background[i].x = star_x;

		background[i].y = star_y;

	}



}



/* Draw the outer space looking background */

void draw_background(GP142_point background[])

{

	int i;



	GP142_rectangleXY(BLACK, SCREEN_X, SCREEN_Y, SCREEN_X+SCREEN_WIDTH, SCREEN_Y+SCREEN_HEIGHT, FILL);

	for (i = 0; i < NUMBER_OF_STARS; i++)

	{

		GP142_rectangleXY(WHITE, background[i].x, background[i].y, background[i].x+1, background[i].y+1, FILL);

	}



} 







/*****************************************************************************



     BUTTONS AND MOUSE CLICKS



*****************************************************************************/





/* Draws the buttons */

void draw_buttons( )

{

	int i;

	int text_offset = MENU_LEFT+10;	/* offset of button text in button */

	int font_size = 12;				/* size of button text */

	

	/* draw rectangles for each button */

	for(i = 0; i < NUM_BUTTONS; i++) {

		GP142_rectangleXY(BLACK,

			MENU_LEFT,

			MENU_TOP - i * BUTTON_HEIGHT,

			MENU_LEFT + BUTTON_WIDTH,

			MENU_TOP - (i + 1)*BUTTON_HEIGHT, 1);

	}

	

	/* display button text */	

	i = MENU_TOP - (BUTTON_HEIGHT/2) - (font_size/2);

	GP142_printfXY(BLACK, text_offset, i, font_size, "Faster");

	

	i -= BUTTON_HEIGHT;

	GP142_printfXY(BLACK, text_offset, i, font_size, "Slower");

	

	i -= BUTTON_HEIGHT;

	GP142_printfXY(BLACK, text_offset, i, font_size, "Quit");

	

}



/* Classify mouse click at (mouse_x,mouse_y) and return kind */

int  classify_mouse_click(int mouse_x, int mouse_y)

{

	if ((mouse_x > MENU_LEFT) &&

		(mouse_x < MENU_LEFT + BUTTON_WIDTH) &&

		(mouse_y < MENU_TOP) &&

		(mouse_y > (MENU_TOP - NUM_BUTTONS*BUTTON_HEIGHT)))

		return BUTTON_CLICK;

	else

		return OTHER_CLICK;

}







/* Process button click at location x, y.  Buttons can only */

/* change the ship's speed or quit the game.				*/

void handle_button(int x, int y, int *ship_speed, int *quit)

{

	int buttonNum;		/* number of selected button */



	/* Complain if x coordinate couldn't be inside a button */

	if (x < MENU_LEFT || x > MENU_LEFT + BUTTON_WIDTH) {

		something_is_wrong();

		return;

	}

	

	/* Convert y coordinate to button number from top. */

	buttonNum = (MENU_TOP - y)/BUTTON_HEIGHT;	 

	

	switch(buttonNum) {

		

	case 0:

		/* speed up */

		*ship_speed = *ship_speed + 1;

		break;

	case 1:

		/* slow down */

		*ship_speed = *ship_speed - 1;

		if (*ship_speed < 0) *ship_speed = 0;

		break;

	case 2:

		/* exit the game */

		*quit = TRUE;

		break;

	default:

		/*Bad news if this is ever reached! */

		something_is_wrong();

		break;

	}

}



/*****************************************************************************



     KEYBOARD WAS PRESSED



*****************************************************************************/



/* The ship can shoot or move right or left depending on a key stroke */

//void handle_key(char key_pressed, int *ship_direction, int *shot)
void handle_key(char key_pressed, int *ship_direction, int shot[3]) /* new */
{

	switch(key_pressed) {



	case ' ':						/* fire a shot */

//		*shot = TRUE;

		shot[0] = TRUE;

		break;

	case 'f' :  /*fire the three in one weapon*/
		shot[1]=TRUE;
		break;
/* make new case for another weapon */

	case '<' :  

    case ',' :
		
		*ship_direction=-1;
		
		break;

    case '>' :

	case '.' :

		*ship_direction=+1;

		break;  


	default:

		break;

	}

}







/*****************************************************************************



     THE SHIP AND ANY SHOT IT FIRES



*****************************************************************************/



/* When the user wants to fire a shot */

void fire_shot(space_alien aliens[], int *ship_x)

{

	int i ,j ,k;

	int increment = (SCREEN_WIDTH)/(NUMBER_OF_ALIENS+1);

	int start = SCREEN_X + increment;

	int shot_height = SCREEN_Y + SCREEN_HEIGHT;

/* new temporary part */
int originalx, originaly; /*aliens[i].x and y before it goes chaotic */




int x[4],y[4]; 


/*end initialization for temporary part */

	for (i = 0; i < NUMBER_OF_ALIENS; i++)

	{

		/* Check if shot was near an alien */

	//	if (abs(*ship_x - start - i*increment) < 5) //original
         if(abs( *ship_x - aliens[i].x) < 5)      
		{

			shot_height = SHIP_LOCATION + 50*DIST_TO_SHIP;		

			if(aliens[i].alive)aliens[i].hits++;
		
	GP142_lineXY(YELLOW, *ship_x, SHIP_LOCATION + 3*SHIP_HEIGHT/2, *ship_x, shot_height, FILL);


/* should hit 3 aliens at once experimental bomb thrower */
//	for(k=0;k<10;k++){ 
//		for(j=0;j<30000;j++){ /* time delay */}
//    GP142_ovalXY(MAGENTA,*ship_x-k*5,SHIP_LOCATION + 3*SHIP_HEIGHT/2 +35*k -4,
//		*ship_x + k*5,SHIP_LOCATION+3*SHIP_HEIGHT/2 + 35*k +4,0);
//	}
  
/* end experimental bomb */
  
	  // makes blue crossed lines on aliens 
	for(k=-1;k<2;k++){  
		for(j=-1;j<2;j++){
	GP142_lineXY(ICE_BLUE,aliens[i].x+k*18,aliens[i].y+j*18,
		         aliens[i].x-k*18,aliens[i].y-j*18,0);
	}/* end of for loop over j */
	}/* end of for loop over k */
				 
/* begin new part */
	/* save original coordinates of aliens[i] */
	originalx=aliens[i].x;
	originaly=aliens[i].y;
/* redefining x[4] and y[4] */
	x[0] =originalx-20; y[0]=originaly;
	x[1]= originalx; y[1]=originaly-10;
	x[2]=originalx +20; y[2]=originaly;
	x[3]=originalx; y[3]=originaly +10;
	
	/*laser shot makes the alien move chaotically then return to original*/		 
    /*erase previous aliens[i] */
	
	for(k=4;k>0;k--){
		for (j=0;j<20000;j++) { /*time delay */}	
	
	aliens[i].color=BLACK;
	draw_aliens(aliens);
	/*new position */
	aliens[i].color=YELLOW + i;
	aliens[i].x=x[k];aliens[i].y=y[k];		 
    draw_aliens(aliens);
	}/* end of loop over k(reused) */

aliens[i].x=originalx; aliens[i].y=originaly;
draw_aliens(aliens);
	/*end new part */
		 } /* end of yes if ship hit the alien */

	}/*end of for loop over i */



}



/* Update ship's x, y coordinates, given current x, y */

/* location, direction (+1 or -1), and speed in pixels per tick, */

void move_ship(int *x, int *y, int *direction, int speed) {


	int trial_x;		/* new value for x coordinate */



	/* Move ship.  If it moves off the edge, reverse direction */

	trial_x = *x + (*direction * speed);

	if (trial_x < -GP142_XMAX+2*BOARDEDGE + SHIP_WIDTH || 
		    trial_x > GP142_XMAX-BOARDEDGE - SHIP_WIDTH)

		*direction = - (*direction);

	else

		*x = trial_x;

}



/* Draw the ship at location x, y */

void draw_ship(int x, int y, int color) {

	GP142_rectangleXY(color, x - SHIP_WIDTH/2, y,

						x + SHIP_WIDTH/2, y + SHIP_HEIGHT, FILL);

	GP142_triangleXY(color,x - SHIP_WIDTH/2, y + SHIP_HEIGHT, x, y + 3*SHIP_HEIGHT/2,

						x + SHIP_WIDTH/2, y +SHIP_HEIGHT, FILL);

	GP142_triangleXY(RED,x - SHIP_WIDTH/2, y, x - SHIP_WIDTH, y,

						x - SHIP_WIDTH/2, y +SHIP_HEIGHT/2, FILL);

	GP142_triangleXY(RED,x + SHIP_WIDTH/2, y, x + SHIP_WIDTH, y,

						x + SHIP_WIDTH/2, y +SHIP_HEIGHT/2, FILL);

	GP142_printfXY(BLACK, x - SHIP_WIDTH/5, y + 3*SHIP_HEIGHT/4, 10, "1");

	GP142_printfXY(BLACK, x - SHIP_WIDTH/5, y + SHIP_HEIGHT/3, 10, "4");

	GP142_printfXY(BLACK, x - SHIP_WIDTH/5, y, 10, "2");

}



/*****************************************************************************



     THE ALIENS...



*****************************************************************************/





/* Draw all aliens given all the info about an alien in each space alien structure */

void draw_aliens(space_alien aliens[]) {

	int i, color;
	int won=1; /* you won */


	for (i = 0; i < NUMBER_OF_ALIENS; i++)

	{


		if(aliens[i].alive){
		
		color=aliens[i].color;
		/* An alien consists of a circle with two lines through it */
		  
   GP142_circleXY(color,aliens[i].x, aliens[i].y, 5);

			GP142_lineXY(color, aliens[i].x - ALIEN_WIDTH, aliens[i].y +ALIEN_WIDTH,

				aliens[i].x + ALIEN_WIDTH ,aliens[i].y - ALIEN_WIDTH , FILL);

			GP142_lineXY(color,aliens[i].x + ALIEN_WIDTH, aliens[i].y+ALIEN_WIDTH,

				aliens[i].x - ALIEN_WIDTH ,aliens[i].y - ALIEN_WIDTH , FILL);

//new experiment
			if(color==CYAN)GP142_lineXY(color,aliens[i].x,aliens[i].y,
				aliens[i].x,SHIP_LOCATION,FILL);//new experiment

		won=0; /* did not win */
		}/* only draw live aliens */ 

		
	}/* end of for loop over i */


	if(won){ GP142_triangleXY(ORANGE,-300,-250,300,-250,0,0,FILL);
		GP142_printfXY(BLUE,-200,-225,20,
			"      Y O U    W O N  wonderful   Y O U    W O N       ");


	}/* end of winning message */
}



/* Initialize each alien. */

void create_aliens(space_alien aliens[]) 

{
  
	int i;
	int increment = (SCREEN_WIDTH)/(NUMBER_OF_ALIENS+1);
	int start = SCREEN_X + increment;

	for (i = 0; i < NUMBER_OF_ALIENS; i++)
	{

//		/* An alien consists of a circle with two lines through it */
		aliens[i].alive=1; /*alive=0 means dead */
		
		/* new part */
		if(i==5||i==8){aliens[i].hits=2; aliens[i].color=RED;}
		else if (i % 2 ==0){aliens[i].hits=0;aliens[i].color=BLUE;}
		else {aliens[i].hits=1;aliens[i].color=GREEN;}
//		aliens[i].color=RED; /*change this later */
//		aliens[i].hits=0;
		/*end of new part */

		aliens[i].x= start+increment*i;
		aliens[i].y=SHIP_LOCATION + 50*DIST_TO_SHIP;
		

	} /* end of loop over for i */





}



/* Every so often (timer) the aliens should move down a level */

void update_aliens(space_alien aliens[], int *timer, int *ship_x)

{

	/* add code here so that aliens drop down a level (and attack)

	   every so often (depending on TIME_PER_ATTACK) */

int ystart=SHIP_LOCATION + 50*DIST_TO_SHIP;
int trial_y , i ,trial_x , k;
int t=(*timer/TIME_PER_ATTACK);

for(i=0;i<NUMBER_OF_ALIENS;i++){

//	trial_y= ystart - (t % 7 )*50; /* may have to include sizes of ship ,aliens */

	trial_y = ystart-(t%14)*50; /* new */

	 if(i==5||i==8)trial_y=trial_y;
	 else if(i%2 == 0) trial_y=trial_y+50;
	 else trial_y=trial_y -50;

	 if(trial_y > ystart) trial_y=ystart - 300; 

	
	 if((t%14)>6){ trial_y=ystart-(7-(t%14)%7)*50;/*new*/

	
	
	
	
	
	/* new part   later could go back and use random colors & levels
	     for now  we have 3 levels    change create_aliens later */

     if(i==5||i==8)trial_y=trial_y;
	 else if(i%2 == 0) trial_y=trial_y-50;//changed to -50 
	 else trial_y=trial_y +50;}//changed to +50

	 if(trial_y > ystart) trial_y=ystart - 300; 

	 
	
	
	
	
	/* new part   later could go back and use random colors & levels
	     for now  we have 3 levels    change create_aliens later */


	/*end of new part */
//	trial_y= aliens[i].y -(t % 7)*5; // too fast
	if(trial_y < (SHIP_LOCATION + ALIEN_WIDTH)) trial_y=ystart; //start from top
	aliens[i].color=BLACK;
	draw_aliens(aliens);
     if(aliens[i].hits>2) aliens[i].alive=0; //dead is zero
	//aliens[i].color=RED;
	if(aliens[i].hits==0)aliens[i].color=BLUE;
	if(aliens[i].hits==1)aliens[i].color=GREEN;
	if(aliens[i].hits==2)aliens[i].color=RED;
	 aliens[i].y=trial_y;

	 //experimental moves aliens
// attempt to slow down aliens

	 if((*timer%70)==1){
trial_x=aliens[i].x+(SCREEN_WIDTH/(NUMBER_OF_ALIENS +1));
if(trial_x>(SCREEN_WIDTH + SCREEN_X))trial_x=SCREEN_X+SCREEN_WIDTH/(NUMBER_OF_ALIENS +1);
aliens[i].x=trial_x;
	 }


	 
	 if (((t%14)==7 ) && aliens[i].hits==0) {    //aliens fire at ship

		 
		 aliens[i].color=CYAN; draw_aliens(aliens);
	 //newest
// check if ship was hit
     if(abs(*ship_x - aliens[i].x)<5){ /*yes was hit*/
		 for (k=0;k<NUMBER_OF_ALIENS;k++){
		 if(aliens[k].hits==2){aliens[k].hits--; break;} //a red or green will be
		 else if(aliens[k].hits==1){aliens[k].hits--;break;} // promoted by decreasing
		 
		 //alien hit points on one alien
	 }//end of for k 
	 }/* end of if shop go hit */
	 
	 }	 

	draw_aliens(aliens);

}//end of for loop
  




//redraw



}





/*****************************************************************************



     OTHER



*****************************************************************************/





/* Initialize random number generator */

void initialize_random_numbers() {

	time_t t;			/* current time */

	srand (time(&t));

}



/* Tells user which keys will operate the ship */

void draw_instructions()

{

	GP142_printfXY(GOLD, -200, GP142_YMAX - 25, 15, "Press '<' to move left.");

	GP142_printfXY(GOLD, -50, GP142_YMAX - 35, 15, "Press '>' to move right.");

	GP142_printfXY(GOLD, 110, GP142_YMAX - 45, 15, "Press spacebar to shoot.");

	GP142_printfXY(GOLD,-200,GP142_YMAX -50,15,"Press ' f ' to shoot 3 aliens at once");

	GP142_printfXY(BLACK,-295,-GP142_YMAX +15, 10,"Elaine Chan  ");

}

// this is under development
/* When the user wants to fire a that hits 3 aliens at one shot */

void fire_shot3in1(space_alien aliens[], int *ship_x)

{

	int i ,j ,k;

	int increment = (SCREEN_WIDTH)/(NUMBER_OF_ALIENS+1);

	int start = SCREEN_X + increment;

	int shot_height = SCREEN_Y + SCREEN_HEIGHT;

/* new temporary part */
int originalx, originaly; /*aliens[i].x and y before it goes chaotic */




int x[4],y[4]; 


/*end initialization for temporary part */

	for (i = 0; i < NUMBER_OF_ALIENS; i++)

	{

		/* Check if shot was near an alien */

	//	if (abs(*ship_x - start - i*increment) < 5) //original
         if(abs( *ship_x - aliens[i].x) < 5)      
		{

			shot_height = SHIP_LOCATION + 50*DIST_TO_SHIP;		
/* hit three aliens at once */
			if(aliens[i].alive)aliens[i].hits++;
		

			if(i==0){if(aliens[i+1].alive)aliens[i+1].hits++;}
			else if (i==9){if(aliens[i-1].alive)aliens[i-1].hits++;}
			else {if(aliens[i+1].alive)aliens[i+1].hits++;
		        	if(aliens[i-1].alive)aliens[i-1].hits++;}
		   	
			/*end hit three at once except for hit 2 if on end*/
			
	GP142_lineXY(YELLOW, *ship_x, SHIP_LOCATION + 3*SHIP_HEIGHT/2, *ship_x, shot_height, FILL);


/* should hit 3 aliens at once experimental bomb thrower */
	for(k=0;k<10;k++){ 
		for(j=0;j<30000;j++){ /* time delay */}
    GP142_ovalXY(MAGENTA,*ship_x-k*5,SHIP_LOCATION + 3*SHIP_HEIGHT/2 +35*k -4,
		*ship_x + k*5,SHIP_LOCATION+3*SHIP_HEIGHT/2 + 35*k +4,0);
	}
  

/* end experimental bomb */

    // makes blue crossed lines on aliens 
	for(k=-1;k<2;k++){  
		for(j=-1;j<2;j++){
	GP142_lineXY(ICE_BLUE,aliens[i].x+k*18,aliens[i].y+j*18,
		         aliens[i].x-k*18,aliens[i].y-j*18,0);
	}/* end of for loop over j */
	}/* end of for loop over k */
				 
/* begin new part */
	/* save original coordinates of aliens[i] */
	originalx=aliens[i].x;
	originaly=aliens[i].y;
/* redefining x[4] and y[4] */
	x[0] =originalx-20; y[0]=originaly;
	x[1]= originalx; y[1]=originaly-10;
	x[2]=originalx +20; y[2]=originaly;
	x[3]=originalx; y[3]=originaly +10;
	
	/*laser shot makes the alien move chaotically then return to original*/		 
    /*erase previous aliens[i] */
	
	for(k=4;k>0;k--){
		for (j=0;j<20000;j++) { /*time delay */}	
	
	aliens[i].color=BLACK;
	draw_aliens(aliens);
	/*new position */
	aliens[i].color=YELLOW + i;
	aliens[i].x=x[k];aliens[i].y=y[k];		 
    draw_aliens(aliens);
	}/* end of loop over k(reused) */

aliens[i].x=originalx; aliens[i].y=originaly;
draw_aliens(aliens);
	/*end new part */
		 } /* end of yes if ship hit the alien */

	}/*end of for loop over i */



}


/* Display error message if internal error is detected. */

void something_is_wrong (void) {

	GP142_printfXY (RED, -120, -GP142_YMAX + 20, 16, "BAAAADDD NEWS!");

}





