-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flower.java
267 lines (261 loc) · 10.8 KB
/
Flower.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import javax.swing.*;
import java.awt.event.*;
/**
* <PRE> The basic Flower that the Player collects to get points in the game Rainbow plants.
* It traverses the screen so that the Player can "collect" it with their basket.
* The direction that the Flower is going on the screen must match the direction of the basket of the
* Player. When a Flower of a colour matching the colour of the user's basket is collected, the user gains points.
* Otherwise, they lose points. If a Flower is collected or traverses the whole game screen without being collected.
* it is then removed from play.
* Variable Dictionary:
* t Timer helps the animation of the Flower across the screen.
* @author Eric Tu and Sejin Kwon
* @version 6
* @since June 13, 2012
*/
public class Flower extends Moveable implements ActionListener
{
private Timer t;
/**
* <PRE>Constructs a Flower object that starts at x on the x axis and y on the y axis,
* comes in from direction dir, is of the colour col and is represented by an image that
* depends on the parameter int col.
* Ths first if detects if the colour of the Flower is blue.
* The first else if detects if the colour of the Flower is green.
* The second else if detects if the colour of the Flower is red.
* The else detects if the colour of the Flower is yellow.
* @param x int initial x axis position
* @param y int initial y axis position
* @param dir int direction that the Flower comes in on the screen
* @param col int colour of the Flower </PRE>
*/
public Flower (int x, int y, int col , int dir)
{
super (x, y, col, dir);
if (col == 0)
setIcon (new ImageIcon ("BlueFlower.png"));
else if (col == 1)
setIcon (new ImageIcon ("GreenFlower.png"));
else if (col == 2)
setIcon (new ImageIcon ("RedFlower.png"));
else
setIcon (new ImageIcon ("YellowFlower.png"));
t = new Timer (1, this);
t.start ();
}
/**
* (Re)Starts the movement of the Flower (for when the game is started or paused and then resumed).
*/
public void resume ()
{
t.start ();
}
/**
* Stops the movement of the Flower (for when the game is paused).
*/
public void pause ()
{
t.stop ();
}
/**
* Removes the Flower from the screen after it has been collected or has moved off the screen.
* Precondition: this Flower is out of play.
* Postcondition: this Flower has been removed.
*/
public void removeSelf ()
{
pause ();
FrameApp.g.remove(this);
}
/**
* A defined actionPerformed method for the ActionListener interface that helps
* animate the Flower and deal with if the Flower has hit a Player.
* The first if checks if the Flower has hit a Player.
* The second if checks if the colour of the Flower matches that of the Player and adds 50 to the Player's score.
* The first else is for when the colour of the Flower does not match that of the Player and it subtracts 10 from the Player's score.
* The third if structure determines if the direction of the Flower is going from left to right.
* The fourth if structure checks if the Flower is still on the screen and if this is true, it moves the Flower one pixel to the right.
* The second else is for when the Flower isn't on the screen. If this is true, the Flower is removed from play.
* The fifth if structure determines if the direction of the Flower is going from up to down.
* The sixth if structure checks if the Flower is still on the screen and if this is true, it moves the Flower one pixel down.
* The third else is for when the Flower isn't on the screen. If this is true, the Flower is removed from play.
* The seventh if structure determines if the direction of the Flower is going from right to left.
* The eigth if structure checks if the Flower is still on the screen and if this is true, it moves the Flower one pixel left.
* The fourth else is for when the Flower isn't on the screen. If this is true, the Flower is removed from play.
* The ninth if structure determines if the direction of the Flower is going from down to up.
* The tenth if structure checks if the Flower is still on the screen and if this is true, it moves the Flower one pixel up.
* The fifth else is for when the Flower isn't on the screen. If this is true, the Flower is removed from play.
* @param e ActionEvent passes in data of the Action that was performed to trigger the method.
*/
public void actionPerformed (ActionEvent e)
{
if (collDetect ())
{
if (getColor () == FrameApp.g.pl.getColor ())
{
Player.setScore(50);
}
else
{
Player.setScore (10);
}
removeSelf ();
}
if (getDirection () == 0)
{
if (getXPos () < 601)
{
incrXPos ();
}
else
removeSelf ();
}
else if (getDirection () == 1)
{
if (getYPos () < 601)
{
incrYPos ();
}
else
removeSelf ();
}
else if (getDirection () == 2)
{
if (getXPos () > -31)
{
decrXPos ();
}
else
removeSelf ();
}
else
{
if (getDirection () == 3)
{
if (getYPos () > -31)
{
decrYPos ();
}
else
removeSelf ();
}
}
}
/**
* Detects if the Flower hits a Player.
* The Flower is only validly caught if the direction of the Flower is opposite to that of the Player it hit and
* if the Flower hits only the Player's basket.
* The if structures are used to check if the Flower has hit a Player at given points on the Flower.
* If the Flower hits a player validly, this method returns true. Otherwise, it returns false.
* This method is called every time the Flower moves.
* Variable Dictionary:
* pl Player variable used to store the Player that the Flower has hit.
* isThere boolean stores if the Flower has hit just the Player's basket.
*/
public boolean collDetect ()
{
Player pl = null;
try
{
if (getDirection () == 0)
{
if (FrameApp.g.getComponentAt (getXPos () + 31, getYPos ()) instanceof Player || FrameApp.g.getComponentAt (getXPos () + 31, getYPos () - 15) instanceof Player || FrameApp.g.getComponentAt (getXPos () + 31, getYPos () - 30) instanceof Player )
{
if (FrameApp.g.getComponentAt (getXPos () + 31, getYPos ()) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 31, getYPos ()));
else if (FrameApp.g.getComponentAt (getXPos () + 31, getYPos () - 15) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 31, getYPos () - 15));
else
if (FrameApp.g.getComponentAt (getXPos () + 31, getYPos () - 30) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 31, getYPos () - 30));
for (int x = 0; x < 112; x += 4)
{
if (FrameApp.g.getComponentAt (pl.getXPos () - 1, pl.getYPos () + x) instanceof Flower)
{
if (pl.getDirection () == 2)
{
return true;
}
}
}
}
}
else if (getDirection () == 1)
{
if (FrameApp.g.getComponentAt (getXPos (), getYPos () + 31) instanceof Player || FrameApp.g.getComponentAt (getXPos () + 15, getYPos () + 31) instanceof Player || FrameApp.g.getComponentAt (getXPos () + 30, getYPos () + 31) instanceof Player)
{
if (FrameApp.g.getComponentAt (getXPos (), getYPos () + 31) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos (), getYPos ()+ 31));
else if (FrameApp.g.getComponentAt (getXPos () + 15, getYPos () + 31) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 15, getYPos () + 31));
else
if (FrameApp.g.getComponentAt (getXPos () + 30, getYPos () + 31) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 30, getYPos () + 31));
for (int x = 0; x < 112; x += 4)
{
if (FrameApp.g.getComponentAt (pl.getXPos () + x, pl.getYPos () - 1) instanceof Flower)
{
if (pl.getDirection () == 3)
{
return true;
}
}
}
}
}
else if (getDirection () == 2)
{
if (FrameApp.g.getComponentAt (getXPos () - 1, getYPos ()) instanceof Player || FrameApp.g.getComponentAt (getXPos () - 1, getYPos () + 15) instanceof Player || FrameApp.g.getComponentAt (getXPos () - 1, getYPos () + 30) instanceof Player)
{
if (FrameApp.g.getComponentAt (getXPos () - 1, getYPos ()) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () - 1, getYPos ()));
else if (FrameApp.g.getComponentAt (getXPos () - 1, getYPos () + 15) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () - 1, getYPos () + 15));
else
if (FrameApp.g.getComponentAt (getXPos () - 1, getYPos () + 30) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () - 1, getYPos () + 30));
for (int x = 0; x < 112; x += 4)
{
if (FrameApp.g.getComponentAt (pl.getXPos () + 113, pl.getYPos () + x) instanceof Flower)
{
if (pl.getDirection () == 0)
{
return true;
}
}
}
}
}
else
{
if (getDirection () == 3)
{
if (FrameApp.g.getComponentAt (getXPos (), getYPos () - 1) instanceof Player || FrameApp.g.getComponentAt (getXPos () + 15, getYPos () - 1) instanceof Player || FrameApp.g.getComponentAt (getXPos () + 30, getYPos () - 1) instanceof Player)
{
if (FrameApp.g.getComponentAt (getXPos (), getYPos () - 1) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos (), getYPos () - 1));
else if (FrameApp.g.getComponentAt (getXPos () + 15, getYPos () - 3) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 15, getYPos () - 3));
else
if (FrameApp.g.getComponentAt (getXPos () + 30, getYPos () - 1) instanceof Player)
pl = (Player) (FrameApp.g.getComponentAt (getXPos () + 30, getYPos () - 1));
for (int x = 0; x < 112; x += 4)
{
if (FrameApp.g.getComponentAt (pl.getXPos () + x, pl.getYPos () + 113) instanceof Flower)
{
if (pl.getDirection () == 1)
{
return true;
}
}
}
}
}
}
}
catch (NullPointerException e)
{
}
return false;
}
}