diff --git a/src/main/java/org/ergasia/javaspacegame/Ammo.java b/src/main/java/org/ergasia/javaspacegame/Ammo.java index 6655822..d4745ed 100644 --- a/src/main/java/org/ergasia/javaspacegame/Ammo.java +++ b/src/main/java/org/ergasia/javaspacegame/Ammo.java @@ -30,6 +30,7 @@ public class Ammo { private Image image; /*This boolean variable is used for the shot system*/ private boolean check = false; + private boolean updatedSpeed = false; /** * The Constructor. @@ -87,6 +88,19 @@ public void fire(){ dy = 5; } + public void setFirespeed() { + if (this.dy<=5) { + this.dy = this.dy * 3; + updatedSpeed=true; + } + + } + + + + public void resetSpeed() { + this.dy = 5; + } /** * if the player typed the right keyboard button * the dx value increases, so the object moves right. diff --git a/src/main/java/org/ergasia/javaspacegame/Panel.java b/src/main/java/org/ergasia/javaspacegame/Panel.java index 7a7ff93..adc42a4 100644 --- a/src/main/java/org/ergasia/javaspacegame/Panel.java +++ b/src/main/java/org/ergasia/javaspacegame/Panel.java @@ -75,6 +75,11 @@ public class Panel extends JPanel implements ActionListener, KeyListener { /*Image of the background */ private Image bg; + private PowerUpFuel fuel; + private int frameCounter = 0; + private int fuelEffectDuration = 300; + + /** *The Constructor with the Start-Game Settings. @@ -102,9 +107,11 @@ public Panel() { } for(int i=0; i 1000000) { + frameCounter = 0; + } + // 2. Check collisions and respond + - repaint();//Calls the paintComponent method. + repaint();//Calls the paintComponent method. } /** diff --git a/src/main/java/org/ergasia/javaspacegame/PowerUpFuel.java b/src/main/java/org/ergasia/javaspacegame/PowerUpFuel.java new file mode 100644 index 0000000..d745d15 --- /dev/null +++ b/src/main/java/org/ergasia/javaspacegame/PowerUpFuel.java @@ -0,0 +1,89 @@ +package org.ergasia.javaspacegame; + +import java.awt.Image; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Objects; +import java.util.Random; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; + +public class PowerUpFuel { + /*X dimension of the object */ + private int x; + /*Y dimension of the object */ + private int y; + /*The width of the object */ + private int width; + /*The height of the object */ + private int height; + private Image image; + /*In this variable is saved the total score */ + /*Random object */ + private Random rand; + private boolean fuelhit; + private boolean isActive=false; + + public PowerUpFuel() { + ImageIcon ii = null; + try { + ii = new ImageIcon(ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/gasolina.png")))); + } catch (IOException e) { + throw new RuntimeException(e); + } + image = ii.getImage(); + rand = new Random(); + + do{ + x = 40 + rand.nextInt(720); + y = 30 + rand.nextInt(440); + }while(x > 642 && y < 137); + + width = image.getWidth(null); + height = image.getHeight(null); + } + public Image getImage() { + return image; + } + public int getX() { + return x; + } + + public int getY() { + return y; + } + + + public void checkForCollision(ArrayList ammos) { + if (fuelhit) return; + + for(int i=0; i y && ammoY + ammoHeight < y + height){ + if(ammoX + ammoWidth > x && ammoX < x + width){ + image = null; + fuelhit = true; + boolean isActive = true; + + } + } + + } + + } + public boolean fuelCheck(){ + return fuelhit; + } + + + + public void changefuelhit() { + this.fuelhit = false; + ; + } +} diff --git a/src/main/java/org/ergasia/javaspacegame/Ufo.java b/src/main/java/org/ergasia/javaspacegame/Ufo.java index a61dbbf..c76f108 100644 --- a/src/main/java/org/ergasia/javaspacegame/Ufo.java +++ b/src/main/java/org/ergasia/javaspacegame/Ufo.java @@ -2,6 +2,7 @@ import java.awt.Image; import java.io.IOException; +import java.lang.reflect.Array; import java.util.Objects; import java.util.Random; import java.util.ArrayList; @@ -26,6 +27,7 @@ public class Ufo { private int height; /*Boolean variable that checks if an Ufo is crushed or not */ private boolean ufoCrushed = false; + private boolean ufoCollided = false; /*The image of the object */ private Image image; /*In this variable is saved the total score */ @@ -34,6 +36,8 @@ public class Ufo { private static int stageScore = 0; /*Random object */ private Random rand; + private boolean movingRight; + private int frameCounter = 0; /** * The Constructor. @@ -41,21 +45,59 @@ public class Ufo { */ public Ufo() { ImageIcon ii = null; + try { ii = new ImageIcon(ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/ufo.png")))); } catch (IOException e) { throw new RuntimeException(e); } image = ii.getImage(); - rand = new Random(); + rand = new Random(); - do{ + do { x = 40 + rand.nextInt(720); y = 30 + rand.nextInt(440); - }while(x > 642 && y < 137); + } while (x > 642 && y < 137); width = image.getWidth(null); - height = image.getHeight(null); + height = image.getHeight(null); + + int random = rand.nextInt(2); + if (random==0){ + movingRight=true; + } + else{ + movingRight=false; + } + } + + + public void reverseDirection(){ + movingRight= !movingRight; + } + public void move() { + x= getX(); + y= getY(); + + + int limitforx; + if (y<137){ + limitforx=612; + } + else{ + limitforx=720; + } + if (movingRight) { + x++; + if (x >= limitforx) { // Adjust this upper limit to match your right boundary + movingRight = false; + } + } else { + x--; + if (x <= 40) { // Adjust this lower limit to match your left boundary + movingRight = true; + } + } } /** @@ -95,6 +137,52 @@ private void checkForCollision(ArrayList ammos) { } + + public boolean collidesWith(Ufo other) { + return this.x < other.x + other.width && + this.x + this.width > other.x && + this.y < other.y + other.height && + this.y + this.height > other.y; + } + + public boolean isCollided() { + return ufoCollided; + } + + public void setCollided(boolean value) { + this.ufoCollided = value; + } + + public void setImage(Image img) { + this.image = img; + } + + public void checkForUfoCollisions(ArrayList ufos) { + for (int i = 0; i < ufos.size(); i++) { + Ufo u1 = ufos.get(i); + if (u1.isCollided()) continue; // skip destroyed UFOs + + for (int j = i + 1; j < ufos.size(); j++) { + Ufo u2 = ufos.get(j); + if (u2.isCollided()) continue; + + if (u1.collidesWith(u2)) { + // Example: destroy both or reverse directions + u1.setCollided(true); + u2.setCollided(true); + u1.reverseDirection(); + u2.reverseDirection(); + + } + } + } + } + + // 4. (Optional) repaint or redraw your game screen here + + + + /** * Get the ufo status. Crushed or no-crushed. * diff --git a/src/main/resources/gasolina.png b/src/main/resources/gasolina.png new file mode 100644 index 0000000..1a25a6d Binary files /dev/null and b/src/main/resources/gasolina.png differ