22
33import java .awt .Image ;
44import java .io .IOException ;
5+ import java .lang .reflect .Array ;
56import java .util .Objects ;
67import java .util .Random ;
78import 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