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
33 changes: 32 additions & 1 deletion src/main/java/org/ergasia/javaspacegame/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class Panel extends JPanel implements ActionListener, KeyListener {
private boolean endGame = false;
/*Image of the background */
private Image bg;
private int frameCounter = 0;



/**
Expand Down Expand Up @@ -102,6 +104,7 @@ public Panel() {
}
for(int i=0; i<numUfo; i++){
ufos.add(new Ufo()); //Creating the Ufo Objects

}

timer = new Timer(16, this);// ~= 60fps.
Expand Down Expand Up @@ -211,7 +214,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,6 +249,32 @@ 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.
}
Expand Down
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