Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions AnimateEnemies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
Name Yujin Bae
teacher: Mr. Guglielmi
Date:November, 10, 2020
description: This is the animate enemies class of the final project space invaders game.
this class will draw all the enemies and animatye them to move left, right and then down.
*/

// The "AnimateEnemies" class.
import java.awt.*;
import hsa.Console;

public class AnimateEnemies extends Thread
{
//varuiable declaration section
private Console c; // The output console
private Enemy[] enemies; //an array of enemies declared
private int s; //the speed at which the enemies will move
private int x = 0; //for horizontal movement of enemies
private int y = 0; //for vertical movements of enemeis
boolean right = true; //whether enemies should move right or not
boolean down = false; //whether enemies should move down or not
private Player p1; //the player 1 to check collision
private Player p2 = null; //the player 2 to check collision
private boolean enemyHit[] = new boolean [30]; //boolean for holding enemy hit

//this is the constructor method
//console c for parameter passing
//int s for the speed of the enemies
//player p for collision checking of that player's missile
public AnimateEnemies (Console c, int s, Player p1)
{
this.p1 = p1;
this.c = c;
this.s = s;
}


//this is the constructor method with overload parameter
//console c for parameter passing
//int s for the speed of the enemies
public AnimateEnemies (Console c, int s, Player p1, Player p2)
{
this.p1 = p1;
this.p2 = p2;
this.c = c;
this.s = s;
}


//this method will chnage manipulate the variables x and y to change the movements of the enemies
private void directions ()
{
if (right)
{
x++;
if (x == 250)
{
right = false;
down = true;
}
}
else
{
x--;
if (x == 0)
{
right = true;
down = true;
}
}
if (down)
{
y += 5;
down = false;
}
}



//this method will make the enemy randomly shoot a bomb.
private void dropBomb (int x, int y)
{
double ran; //this variable decides wether to shoot a bomb or not
ran = Math.random ();
if (ran < 0.005)
{
Bomb b = new Bomb (c, x, y);
try
{
b.start ();
}
catch (Exception e)
{
}
}
}


//this method will draw the enemies.
private void drawEnemies ()
{
while (true)
{
//draws the enemies!!
enemies = new Enemy [30]; //create an array of enemeis
for (int i = 0 ; i < 30 ; i++)
{
if (!enemyHit [i]) //the enemy will not be drawn if it was hit
{
if (i < 10) //the first row of enemies
{
enemies [i] = new Enemy (c, i * 25 + x, 25 + y);
dropBomb (i * 25 + x, 25 + y);
if (p2 == null)
{
collision (p1, i);
}
else
{
collision (p1, i);
collision (p2, i);
}
}
else if (i < 20) //the second row
{
enemies [i] = new Enemy (c, i % 10 * 25 + x, 50 + y);
if (p2 == null)
{
collision (p1, i);
}
else
{
collision (p1, i);
collision (p2, i);
}
}
else //the third row
{
enemies [i] = new Enemy (c, i % 10 * 25 + x, 75 + y);
if (p2 == null)
{
collision (p1, i);
}
else
{
collision (p1, i);
collision (p2, i);
}
}
}
} //enemy drawing for loop's end\
directions ();
try
{
sleep (80 - s * 10);
}
catch (Exception e)
{
}
} //end of while loop
} //end of the draw enemies method



private void collision (Player p, int i)
{
int ex = enemies [i].getEnemyX (); //enemy x cooridnate
int ey = enemies [i].getEnemyY (); //enemy y coordinate
int mx = p.getMissileX (); //missile x coordinate
int my = p.getMissileY (); //missile y coordinate

//if there is a collision
if ((ex <= mx && mx <= ex + 20) && (ey <= my && my <= ey + 20))
{
c.setColor (Color.black);
c.fillRect (enemies [i].getEnemyX (), enemies [i].getEnemyY (), 20, 20);
enemyHit [i] = true;
p.setEnemyHit(true);
}
}


public boolean[] getEnemyHit ()
{
return enemyHit;
}


public void run ()
{
drawEnemies ();

}
} // AnimateEnemies class


43 changes: 33 additions & 10 deletions Bomb.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.awt.*;
import hsa.Console;

public class Bomb
public class Bomb extends Thread
{
//variable declaration
private Console c; // The output console
Expand All @@ -19,33 +19,50 @@ public class Bomb
private boolean playerHit; //this varibale indicates wether the bomb has hit a player or not

//this method will construct the bomb object
//The console is passes to display output
public Bomb (Console c, int ex, int ey)
//console c for parameter passing, int ex and ey for the enemie's x and y coordinates
public Bomb (Console c, int x, int y)
{
//The parameter's console c is passed to this class's console c to display output
this.c = c;
ex = x;
ey = y;
drawBomb ();
this.x = x;
this.y = y;

}


//this method will draw the graphics of the bomb
private void drawBomb ()
{
c.setColor (Color.yellow);
c.fillRect (x + 17, y, 5, 20);
for (int i = 0 ; i < 500 ; i++)
{
c.setColor (Color.black);
c.fillRect (x + 8, y + 16, 4, 2);
c.setColor (Color.white);
c.fillRect (x + 8, y + 18, 4, 20);
y++;
try
{
sleep (10);
}
catch (Exception e)
{
}
}

}


//this method will check if the bomb has hit a player or not
//takes a player object as an input to
private void setPlayerHit (Player p)
{
int px = p.getPlayerX ();
if ((px <= x && x <= px + 50) && (y >= 450 && y <= 475))
{
playerHit = true;
}


else
{
playerHit = false;
Expand All @@ -54,11 +71,17 @@ private void setPlayerHit (Player p)


//this method wil;l return wether the bomb has hit a player or not
public boolean getPlayerHit ()
public boolean getPlayerHit (Player p)
{
setPlayerHit (p);
return playerHit;
}



public void run ()
{
drawBomb ();
}
} // Bomb class


44 changes: 21 additions & 23 deletions Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ public class Enemy
private Console c; // The output console
private int x; //the x coordinate of the enemy
private int y; //the y coordinate of the enemy
private boolean enemyHit; //indicates wether enemy is hit by player missile
private boolean enemyHit = false; //whether the neemy is hit or not
boolean right = true; //whether enemies should move right or not
boolean down = false; //whether enemies should move down or not

//this is the constructor method.
//The console is passes to display output
//console c for the console the enemy will be drawn in
//int x and y for the coordinates of the enemy
public Enemy (Console c, int x, int y)
{
//The console is passes to display output
this.c = c;
this.x = x;
this.y = y;
if (!enemyHit)
{
drawEnemy ();
dropBomb ();
}
drawEnemy ();
}


//this method will draw the graphics of an enemy
//int s fo rthe speed of the enemy
private void drawEnemy ()
{
c.setColor (Color.black); //sets colour to black
Expand All @@ -57,21 +58,12 @@ private void drawEnemy ()
}


//this method will make the enemy randomly shoot a bomb.
private void dropBomb ()
{
double ran; //this vriable decides wether to shoot a bomb or not
ran = Math.random ();
if (ran < 0.1)
{
Bomb b = new Bomb (c, x, y);
}
}


//this method check if enemy is hit by player missile
private void setEnemyHit (int mx, int my)
private void setEnemyHit (Player p)
{
int mx = p.getMissileX ();
int my = p.getMissileY ();

if ((x <= mx && mx <= x + 20) && (y <= my && my <= y + 20))
{
enemyHit = true;
Expand All @@ -82,10 +74,16 @@ private void setEnemyHit (int mx, int my)
}
}

public int getEnemyX ()
{
return x;
}

//this method will return wether the enemy has been hit or not.
public boolean getEnemyHit ()

public int getEnemyY ()
{
return enemyHit;
return y;
}
} // Enemy class


Loading