From 8ec67df0059cd287bb8f5b35b4b9d6764a481a9f Mon Sep 17 00:00:00 2001 From: yuj7121 <95194014+yuj7121@users.noreply.github.com> Date: Sat, 26 Mar 2022 19:12:19 -0400 Subject: [PATCH] Add files via upload Draft 2. November 12, 2020 --- AnimateEnemies.java | 197 ++++++++++++++++++++++++++++++++++++++++++++ Bomb.java | 43 +++++++--- Enemy.java | 44 +++++----- GameScreen.java | 72 ++++++++++++++++ Level.java | 101 +++++++++-------------- MainMenu.java | 10 +-- Missile.java | 85 ++++++++++++++++++- Player.java | 190 ++++++++++++++++++++++++++++++++++++------ 8 files changed, 617 insertions(+), 125 deletions(-) create mode 100644 AnimateEnemies.java create mode 100644 GameScreen.java diff --git a/AnimateEnemies.java b/AnimateEnemies.java new file mode 100644 index 0000000..632a217 --- /dev/null +++ b/AnimateEnemies.java @@ -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 + + diff --git a/Bomb.java b/Bomb.java index 448c821..d36958b 100644 --- a/Bomb.java +++ b/Bomb.java @@ -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 @@ -19,13 +19,13 @@ 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; } @@ -33,12 +33,27 @@ public Bomb (Console c, int ex, int ey) //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 (); @@ -46,6 +61,8 @@ private void setPlayerHit (Player p) { playerHit = true; } + + else { playerHit = false; @@ -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 + + diff --git a/Enemy.java b/Enemy.java index 2331628..928c441 100644 --- a/Enemy.java +++ b/Enemy.java @@ -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 @@ -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; @@ -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 + + diff --git a/GameScreen.java b/GameScreen.java new file mode 100644 index 0000000..a079352 --- /dev/null +++ b/GameScreen.java @@ -0,0 +1,72 @@ +/* +Name Yujin Bae +teacher: Mr. Guglielmi +Date:L november, 10, 2020 +description: This is the gamescreen class of the final project space invaders game. +this class will load levels, let the user play, die, and input their high score. +*/ + +// The "GameScreen" class. +import java.awt.*; +import hsa.Console; + +public class GameScreen +{ + + //variable declaration + private Console c; // The output console + private int score; //this varibale holds the score of the game. + private Enemy[] enemies; //an array of enemies declared + private boolean levelEnd = false; //this boolean holds wether the level is finished or not + + //this is the constructor the method. + public GameScreen (Console c) + { + this.c = c; + levels (); + } + + + //this method will draw the enemies, the player, and the clock. + private void levels () + { + Level lev = new Level (c, 1, false); + } + + + //this method will display the gameover screen + private void gameover () + { + + } + + + //this method will record the curretn scroe and display it on the top of the screen. + private void setScore () + { + + } + + + //this method will return the curretn score + public int getScore () + { + return score; + } + + + //this method will ask the high scoring user for their name + private void inputName () + { + + } + + + //this method will save the highscore and the user's information to a file + private void saveHighscore () + { + + } +} // GameScreen class + + diff --git a/Level.java b/Level.java index e258c70..f0949ee 100644 --- a/Level.java +++ b/Level.java @@ -1,7 +1,7 @@ /* Name Yujin Bae teacher: Mr. Guglielmi -Date:L november, 6, 2020 +Date: november, 6, 2020 description: This is the level class of the final project space invaders game. this class will draw all the enemies and the player, and let the user play a level. */ @@ -9,85 +9,66 @@ this class will draw all the enemies and the player, and let the user play a lev // The "Level" class. import java.awt.*; import hsa.Console; +import java.lang.*; public class Level { //variable declaration - private Console c; // The output console - private int speed; //the speed of the game - private int score; //this varibale holds the score of the game. - private Player p; - private Enemy[] enemies; + private Console c; // The output console + private boolean mul; //whether the game will be multiplayer or not + private int s; //the speed of the enemies + private Player p1; //first player object declared + private Player p2; //second player object declared + private boolean levelEnd = false; //this boolean holds whether the level is finished or not //this is the constructor of the method. - public Level (Console c, int s) + public Level (Console c, int s, boolean mul) { this.c = c; - speed = s; - gameScreen(); + this.s = s; + this.mul = mul; + joining (); } - //this method will draw the enemies, the player, and the clock. - private void gameScreen () + //this method will draw the players with the player number provided by the parameter + private void drawPlayers () { - //draws the enemies!! - enemies = new Enemy [30]; //create an array of enemeis - for (int i = 0 ; i >= 30 ; i++) + if (mul == true) { - if (i <= 10) //the first row of enemies - { - enemies [i] = new Enemy (c, i * 25, 25); - } - else if (i <= 20) //the second row - { - enemies [i] = new Enemy (c, i % 10 * 25, 50); - } - else //the third row - { - enemies [i] = new Enemy (c, i % 10 * 25, 75); - } - - } //enemy drawing for loop's end - - //draws the player - p = new Player (c); - - } - - - //this method will display the gameover screen - private void gameover () - { - - } - - - //this method will record the curretn scroe and display it on the top of the screen. - private void setScore () - { + p1 = new Player (c, true); + p1.start (); + p2 = new Player (c, false); + p2.start (); + } + else + { + p1 = new Player (c, true); + p1.start (); + } } - //this method will return the curretn score - public int getScore () + public void joining () { - return score; - } - + //draws the black background + c.setColor (Color.black); + c.fillRect (0, 0, 500, 500); - //this method will ask the high scoring user fro their name - private void getName () - { + drawPlayers (); + if (mul) + { + AnimateEnemies a = new AnimateEnemies (c, s, p1, p2); + a.start (); + } + else + { + AnimateEnemies a = new AnimateEnemies (c, s, p1); + a.start (); + } } +} // Level class - //this method will save the highscore and the user's information to a file - private void saveHighscore () - { - - } - -} // Level class diff --git a/MainMenu.java b/MainMenu.java index c078390..13b4a12 100644 --- a/MainMenu.java +++ b/MainMenu.java @@ -10,17 +10,15 @@ public class MainMenu //this method will create the main menu private MainMenu () { - c = new Console (24, 60); - + c = new Console (25, 63); } - //the main method o fthe whole program + //the main method of the whole program public static void main (String[] args) { MainMenu m = new MainMenu (); - - Level lev1 = new Level (c, 1); - // Place your program here. 'c' is the output console + GameScreen g = new GameScreen (c); + } // main method } // MainMenu class diff --git a/Missile.java b/Missile.java index a475ddb..b00937d 100644 --- a/Missile.java +++ b/Missile.java @@ -3,16 +3,95 @@ teacher: Mr. Guglielmi Date: november, 6, 2020 description: This is the missile class of the final project space invaders game. -this class will draw all the enemies and the player, and let the user play a level. +this class create the missile object to be shot by the player. */ // The "Missile" class. import java.awt.*; import hsa.Console; -public class Missile +public class Missile extends Thread { - Console c; // The output console + //variable declaration + private Console c; // The output console + private int x = 0; //the x coordinate of the missile + private int y; //the y coordinate of the missile + private boolean go = true; //whether the missile should continue or not + private boolean load = true; //whether a missile is loaded or not + //this is the constructor of the missile class + public Missile (Console c) + { + this.c = c; + } + + + //this method will draw a missile's graphics + private void drawMissile () + { + y = 450; + //the missile is fired! + while (go) + { + c.setColor (Color.white); + c.fillRect (x, y, 4, 20); + c.setColor (Color.black); + c.fillRect (x, y + 20, 4, 2); + //if the missile does not hit as enemy or goes off screen, it continues ascending + if (y >= -20 ) + { + y--; + load = false; + } + else + { + load = true; + //go = false; + } + + try + { + sleep (10); + } + catch (Exception e) + { + } + } //while(go) loop end + } //drawMissile method end + + + //this method sets the missile's x coordinate. + //takes in the player's x coordinate as an input + public void setMissileX (int x) + { + this.x = x + 23; //+23 to make the missile com form the missile of the cannon + } + + + //this method will return the x location of the missile + public int getMissileX () + { + return x; + } + + + //this method will return the y location of the missile + public int getMissileY () + { + return y; + } + + //this method will return the load information of the missile + public boolean getLoad () + { + return load; + } + + + //this method runs the animation of the missile + public void run () + { + drawMissile (); + } } // Missile class diff --git a/Player.java b/Player.java index 834cc1d..303c656 100644 --- a/Player.java +++ b/Player.java @@ -9,53 +9,97 @@ import java.awt.*; import hsa.Console; -public class Player +public class Player extends Thread { //variable declaration section - private Console c; // The output console - private int x; //the x coordinate of the player + private Console c; // The output console + private int x = 225; //the x coordinate of the player + private Missile m1; //declares a new missile 1 object + private Missile m2; //declares a new missile 2 object + private boolean p1; //whether this is player one or player two + boolean go = false; //whether the missile should go or not //this is the constrcutor method that will create a player object //The console is passes to display output - public Player (Console c) + public Player (Console c, boolean p1) { - this.c = c; - drawPlayer (); - input (); + this.c = c; //parameter passing + this.p1 = p1; //parameter passing + m1 = new Missile (c); //initialize a missile for player 1 + m2 = new Missile (c); //initialize a mmissile for player 2 } - //this method draws the graphics of the player - private void drawPlayer () - { - c.setColor (new Color (4, 245, 4)); //sets the colour to green - c.fillRect (x + 23, 450, 4, 3); //the top small square - c.fillRect (x + 21, 453, 8, 5); //the top middle square - c.fillRect (x + 4, 458, 42, 4); //the third layer rectangle - c.fillRect (x, 462, 50, 12); //the bottom huge reectangle - } - - - //this method will take input from the user + //this method will take input from the user and move the cannon or shooot a missile private void input () { - char wasd; //this char wll hold the user's input - while (true) + if (p1) { + char wasd; //this char wll hold the user's input wasd = c.getChar (); if (wasd == 'a') { - x -= 2; + if (x > 5) + x -= 2; } else if (wasd == 'd') { - x += 2; + if (x < 445) + x += 2; } else if (wasd == 'w' || wasd == 's') { + if (m1.getLoad ()) + { + m1.setMissileX (x); + m1.start (); + } + } + } + if (!p1) + { + char ijkl; //this char wll hold the user's input + ijkl = c.getChar (); + if (ijkl == 'j') + { + if (x > 5) + + x -= 2; + } + else if (ijkl == 'l') + { + if (x < 445) + + x += 2; } + else if ((ijkl == 'i' || ijkl == 'k')) + { + if (m2.getLoad ()) + { + m2.setMissileX (x); + m2.start (); + } + } + } + } + + + //this method draws the graphics of the player + private void drawPlayer () + { + while (true) + { + + c.setColor (Color.black); + c.fillRect (x - 2, 448, 54, 29); + c.setColor (new Color (4, 245, 4)); //sets the colour to green + c.fillRect (x + 23, 450, 4, 3); //the top small square + c.fillRect (x + 21, 453, 8, 5); //the top middle square + c.fillRect (x + 4, 458, 42, 4); //the third layer rectangle + c.fillRect (x, 462, 50, 12); //the bottom huge reectangle + input (); } } @@ -65,4 +109,104 @@ public int getPlayerX () { return x; } + + + //this method will set the missile's x coordinate + private int setMissileX () + { + if (p1) + { + if (m1 != null) + { + return m1.getMissileX (); + } + else + { + return 0; + } + } + else + { + if (m2 != null) + { + return m2.getMissileX (); + } + else + { + return 0; + } + } + } + + + //this method will return the x coordinate of player's missile + public int getMissileX () + { + return setMissileX (); + } + + + //this method will set the y coordinate of the player's misssile + private int setMissileY () + { + if (p1) + { + if (m1 != null) + { + return m1.getMissileY (); + } + else + { + return 0; + } + } + else + { + if (m2 != null) + { + return m2.getMissileY (); + } + else + { + return 0; + } + } + } + + + //this method returns the y coordinate of player's missile + public int getMissileY () + { + return setMissileY (); + } + + + //this method will set whether the missile should continue to go or not + public void setEnemyHit (boolean go) + { + this.go = go; + } + + + //this method will return whether the missile has hit an enemy or not + public boolean getEnemyHit () + { + if (go == true) + { + return go; + } + else + { + return false; + } + } + + + //this method is from thread class. it runs the player's animation + public void run () + { + drawPlayer (); + } } // Player class + +