Skip to content

Commit eda794d

Browse files
committed
#1 Make UFOs go left and right but they should stay inside the viewport
1 parent a1aa142 commit eda794d

2 files changed

Lines changed: 124 additions & 5 deletions

File tree

src/main/java/org/ergasia/javaspacegame/Panel.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public class Panel extends JPanel implements ActionListener, KeyListener {
7474
private boolean endGame = false;
7575
/*Image of the background */
7676
private Image bg;
77+
private int frameCounter = 0;
78+
7779

7880

7981
/**
@@ -102,6 +104,7 @@ public Panel() {
102104
}
103105
for(int i=0; i<numUfo; i++){
104106
ufos.add(new Ufo()); //Creating the Ufo Objects
107+
105108
}
106109

107110
timer = new Timer(16, this);// ~= 60fps.
@@ -211,7 +214,9 @@ public void actionPerformed(ActionEvent e) {
211214
}
212215

213216
endTime = System.currentTimeMillis();
214-
217+
for (Ufo ufo : ufos) {
218+
ufo.move(); // Only x changes
219+
}
215220
/*
216221
* Object update methods.
217222
* Every Object in this game that is a paint component has an update
@@ -244,6 +249,32 @@ public void actionPerformed(ActionEvent e) {
244249
}
245250
}
246251

252+
for (Ufo u : ufos) {
253+
254+
u.checkForUfoCollisions(ufos);
255+
}
256+
for (Ufo u: ufos) {
257+
u.setCollided(false);
258+
}
259+
260+
261+
262+
frameCounter++;
263+
264+
// Every 10 frames, do something
265+
for (Ufo u : ufos) {
266+
if (frameCounter % 10 == 0) {
267+
u.checkForUfoCollisions(ufos);
268+
}
269+
}
270+
// Move UFOs, draw, etc.
271+
272+
// Reset counter if needed to prevent overflow
273+
if (frameCounter > 1000000) {
274+
frameCounter = 0;
275+
}
276+
// 2. Check collisions and respond
277+
247278

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

src/main/java/org/ergasia/javaspacegame/Ufo.java

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.awt.Image;
44
import java.io.IOException;
5+
import java.lang.reflect.Array;
56
import java.util.Objects;
67
import java.util.Random;
78
import java.util.ArrayList;
@@ -26,6 +27,7 @@ public class Ufo {
2627
private int height;
2728
/*Boolean variable that checks if an Ufo is crushed or not */
2829
private boolean ufoCrushed = false;
30+
private boolean ufoCollided = false;
2931
/*The image of the object */
3032
private Image image;
3133
/*In this variable is saved the total score */
@@ -34,28 +36,68 @@ public class Ufo {
3436
private static int stageScore = 0;
3537
/*Random object */
3638
private Random rand;
39+
private boolean movingRight;
40+
private int frameCounter = 0;
3741

3842
/**
3943
* The Constructor.
4044
* Sets the position and the image of the object.
4145
*/
4246
public Ufo() {
4347
ImageIcon ii = null;
48+
4449
try {
4550
ii = new ImageIcon(ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/ufo.png"))));
4651
} catch (IOException e) {
4752
throw new RuntimeException(e);
4853
}
4954
image = ii.getImage();
50-
rand = new Random();
55+
rand = new Random();
5156

52-
do{
57+
do {
5358
x = 40 + rand.nextInt(720);
5459
y = 30 + rand.nextInt(440);
55-
}while(x > 642 && y < 137);
60+
} while (x > 642 && y < 137);
5661

5762
width = image.getWidth(null);
58-
height = image.getHeight(null);
63+
height = image.getHeight(null);
64+
65+
int random = rand.nextInt(2);
66+
if (random==0){
67+
movingRight=true;
68+
}
69+
else{
70+
movingRight=false;
71+
}
72+
}
73+
74+
75+
public void reverseDirection(){
76+
movingRight= !movingRight;
77+
}
78+
public void move() {
79+
x= getX();
80+
y= getY();
81+
82+
83+
int limitforx;
84+
if (y<137){
85+
limitforx=612;
86+
}
87+
else{
88+
limitforx=720;
89+
}
90+
if (movingRight) {
91+
x++;
92+
if (x >= limitforx) { // Adjust this upper limit to match your right boundary
93+
movingRight = false;
94+
}
95+
} else {
96+
x--;
97+
if (x <= 40) { // Adjust this lower limit to match your left boundary
98+
movingRight = true;
99+
}
100+
}
59101
}
60102

61103
/**
@@ -95,6 +137,52 @@ private void checkForCollision(ArrayList<Ammo> ammos) {
95137

96138
}
97139

140+
141+
public boolean collidesWith(Ufo other) {
142+
return this.x < other.x + other.width &&
143+
this.x + this.width > other.x &&
144+
this.y < other.y + other.height &&
145+
this.y + this.height > other.y;
146+
}
147+
148+
public boolean isCollided() {
149+
return ufoCollided;
150+
}
151+
152+
public void setCollided(boolean value) {
153+
this.ufoCollided = value;
154+
}
155+
156+
public void setImage(Image img) {
157+
this.image = img;
158+
}
159+
160+
public void checkForUfoCollisions(ArrayList<Ufo> ufos) {
161+
for (int i = 0; i < ufos.size(); i++) {
162+
Ufo u1 = ufos.get(i);
163+
if (u1.isCollided()) continue; // skip destroyed UFOs
164+
165+
for (int j = i + 1; j < ufos.size(); j++) {
166+
Ufo u2 = ufos.get(j);
167+
if (u2.isCollided()) continue;
168+
169+
if (u1.collidesWith(u2)) {
170+
// Example: destroy both or reverse directions
171+
u1.setCollided(true);
172+
u2.setCollided(true);
173+
u1.reverseDirection();
174+
u2.reverseDirection();
175+
176+
}
177+
}
178+
}
179+
}
180+
181+
// 4. (Optional) repaint or redraw your game screen here
182+
183+
184+
185+
98186
/**
99187
* Get the ufo status. Crushed or no-crushed.
100188
*

0 commit comments

Comments
 (0)