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
14 changes: 14 additions & 0 deletions src/main/java/org/ergasia/javaspacegame/Ammo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
83 changes: 80 additions & 3 deletions src/main/java/org/ergasia/javaspacegame/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -102,9 +107,11 @@ public Panel() {
}
for(int i=0; i<numUfo; i++){
ufos.add(new Ufo()); //Creating the Ufo Objects

}

timer = new Timer(16, this);// ~= 60fps.

timer = new Timer(16, this);// ~= 60fps.
timer.start();//Starting the thread.

}
Expand Down Expand Up @@ -187,6 +194,15 @@ private void doDrawing(Graphics g) {

g2d.drawImage(ship.getImage(), ship.getX(), ship.getY() - 25/*the height of the image*/, this);

if (fuel!= null && fuel.getImage() != null) {
if (!fuel.fuelCheck()){
g2d.drawImage(fuel.getImage(), fuel.getX(), fuel.getY(), this);
}
}
else{
//System.out.println("Fuel is "+frameCounter);

}
}

/**
Expand All @@ -197,7 +213,40 @@ private void doDrawing(Graphics g) {
*/
@Override
public void actionPerformed(ActionEvent e) {
frameCounter++;
if (fuel != null && !fuel.fuelCheck()) {
fuel.checkForCollision(ammos);

}

if (fuel != null && fuel.fuelCheck()){

// or 300 if using frames
// Set all ammo to be fast

for (Ammo ammo : ammos) {
ammo.setFirespeed();
}



fuelEffectDuration--;
System.out.println(fuelEffectDuration);
if (fuelEffectDuration <= 0) {
fuel.changefuelhit();// end the powerup

fuelEffectDuration = 300;
// Reset ammo speeds if needed
for (Ammo ammo : ammos) {
ammo.resetSpeed(); // You may need to make this method

}
}

}
if (frameCounter == 100) {
fuel= new PowerUpFuel();
}
//if stage is beaten this if statement is responsible for the setup for the next stage.
if(restart){
for(int i=0; i<numUfo; i++){
Expand All @@ -211,7 +260,9 @@ public void actionPerformed(ActionEvent e) {
}

endTime = System.currentTimeMillis();

for (Ufo ufo : ufos) {
ufo.move(); // Only x changes
}
/*
* Object update methods.
* Every Object in this game that is a paint component has an update
Expand Down Expand Up @@ -244,8 +295,34 @@ public void actionPerformed(ActionEvent e) {
}
}

for (Ufo u : ufos) {

u.checkForUfoCollisions(ufos);
}
for (Ufo u: ufos) {
u.setCollided(false);
}



frameCounter++;

// Every 10 frames, do something
for (Ufo u : ufos) {
if (frameCounter % 10 == 0) {
u.checkForUfoCollisions(ufos);
}
}
// Move UFOs, draw, etc.

// Reset counter if needed to prevent overflow
if (frameCounter > 1000000) {
frameCounter = 0;
}
// 2. Check collisions and respond


repaint();//Calls the paintComponent method.
repaint();//Calls the paintComponent method.
}

/**
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/org/ergasia/javaspacegame/PowerUpFuel.java
Original file line number Diff line number Diff line change
@@ -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<Ammo> ammos) {
if (fuelhit) return;

for(int i=0; i<ammos.size(); i++){

int ammoX = ammos.get(i).getX();
int ammoY = ammos.get(i).getY();
int ammoWidth = ammos.get(i).getWidth();
int ammoHeight = ammos.get(i).getHeight();

if(ammoY + ammoHeight > 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;
;
}
}
96 changes: 92 additions & 4 deletions src/main/java/org/ergasia/javaspacegame/Ufo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -34,28 +36,68 @@ public class Ufo {
private static int stageScore = 0;
/*Random object */
private Random rand;
private boolean movingRight;
private int frameCounter = 0;

/**
* The Constructor.
* Sets the position and the image of the object.
*/
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;
}
}
}

/**
Expand Down Expand Up @@ -95,6 +137,52 @@ private void checkForCollision(ArrayList<Ammo> 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<Ufo> 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.
*
Expand Down
Binary file added src/main/resources/gasolina.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.