-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasketballWorld.java
More file actions
407 lines (302 loc) · 12 KB
/
BasketballWorld.java
File metadata and controls
407 lines (302 loc) · 12 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
public class BasketballWorld extends World
{
private Basketball ball;
private Basket basket;
private Backboard backboard;
private Hand hand;
private int score = 0;
private ArrayList<Boundary> boundaries = new ArrayList<Boundary>();
private int timeLeft = 120;
private long lastTime = System.currentTimeMillis();
private boolean gameOver = false;
private boolean endGameDisplayed = false;
private Arrow arrow;
private GreenfootSound timeEndSound;
private boolean soundEnabled = true;
private boolean cpuMode = false;
private String difficulty = "medium";
private int cpuReactionDelay = 0;
private boolean mousePressed = false;
private int startX, startY;
private long pressStartTime;
private boolean showingInstructions = true;
private int instructionTimer = 120;
private GreenfootImage instructionsImage = new GreenfootImage("basketball_instructions.png");
public BasketballWorld()
{
this(false, "medium");
}
public BasketballWorld(boolean isCPU, String diff)
{
super(1100, 600, 1);
this.cpuMode = isCPU;
this.difficulty = diff;
showInstructionsScreen();
}
private void showInstructionsScreen() {
instructionsImage.scale(1100, 600);
setBackground(instructionsImage);
showingInstructions = true;
instructionTimer = 120;
}
private void prepareGame() {
GreenfootImage bg = new GreenfootImage("images/bg.png");
bg.scale(1100, 600);
setBackground(bg);
ball = new Basketball();
addObject(ball, 100, 500);
hand = new Hand(cpuMode, difficulty);
addObject(hand, 400, 550);
backboard = new Backboard();
addObject(backboard, 1080, 200);
basket = new Basket();
addObject(basket, 1035, 251);
backboard.setBasket(basket);
basket.setBackboard(backboard);
arrow = new Arrow();
addObject(arrow, ball.getX(), ball.getY());
arrow.setVisible(false);
try {
timeEndSound = new GreenfootSound("sounds/time_end.wav");
} catch (Throwable t) {
System.out.println("Could not load time end sound: " + t.getMessage());
soundEnabled = false;
timeEndSound = null;
}
}
public void act()
{
if (showingInstructions) {
instructionTimer--;
if (instructionTimer <= 0) {
showingInstructions = false;
prepareGame();
}
return;
}
if (!gameOver)
{
updateTime();
handleMouseInput();
updateUI();
}
if (Greenfoot.isKeyDown("r"))
{
Greenfoot.setWorld(new BasketballWorld(cpuMode, difficulty));
}
if (Greenfoot.isKeyDown("escape"))
{
Greenfoot.setWorld(new MenuWorld());
}
}
private void updateTime()
{
long currentTime = System.currentTimeMillis();
if (currentTime - lastTime >= 1000)
{
timeLeft--;
lastTime = currentTime;
if (timeLeft <= 0)
{
gameOver = true;
if (soundEnabled && timeEndSound != null)
{
try {
timeEndSound.play();
} catch (Throwable t) {
soundEnabled = false;
}
}
}
}
}
private void drawOutlinedText(String text, int centerX, int y, Color mainColor, Color outlineColor, int size)
{
GreenfootImage img = new GreenfootImage(text, size, mainColor, new Color(0, 0, 0, 0));
GreenfootImage outline = new GreenfootImage(text, size, outlineColor, new Color(0, 0, 0, 0));
GreenfootImage combined = new GreenfootImage(img.getWidth() + 4, img.getHeight() + 4);
for (int dx = -2; dx <= 2; dx++)
for (int dy = -2; dy <= 2; dy++)
combined.drawImage(outline, dx + 2, dy + 2);
combined.drawImage(img, 2, 2);
int x = centerX - combined.getWidth() / 2;
getBackground().drawImage(combined, x, y);
}
private void handleMouseInput()
{
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null)
{
if (Greenfoot.mousePressed(null))
{
mousePressed = true;
startX = mouse.getX();
startY = mouse.getY();
pressStartTime = System.currentTimeMillis();
arrow.setVisible(true);
}
if (mousePressed)
{
int deltaX = mouse.getX() - startX;
int deltaY = mouse.getY() - startY;
double angle = Math.atan2(deltaY, deltaX);
double power = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
arrow.setRotation((int) Math.toDegrees(angle));
arrow.setPower((int) power);
arrow.setLocation(ball.getX(), ball.getY());
}
if (Greenfoot.mouseClicked(null) && mousePressed)
{
mousePressed = false;
arrow.setVisible(false);
int deltaX = mouse.getX() - startX;
int deltaY = mouse.getY() - startY;
double power = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
power = Math.min(power, 200);
ball.shoot(deltaX, deltaY, power);
}
}
}
private void updateUI()
{
if (gameOver && !endGameDisplayed) {
GreenfootImage bg = new GreenfootImage("images/bg.png");
bg.scale(1100, 600);
setBackground(bg);
drawOutlinedText("GAME OVER", 550, 150, Color.YELLOW, Color.BLACK, 60);
drawOutlinedText("Final Score: " + score, 550, 250, Color.WHITE, Color.BLACK, 50);
drawOutlinedText("Press R to reset or ESC to return to menu", 550, 350, Color.WHITE, Color.BLACK, 30);
endGameDisplayed = true;
return;
}
if (gameOver) return;
GreenfootImage bg = new GreenfootImage("images/bg.png");
bg.scale(1100, 600);
setBackground(bg);
drawGameText("Score: ", score, 40, 20, Color.WHITE, Color.BLACK);
drawGameText("Time: ", timeLeft, 950, 20, Color.RED, Color.BLACK);
if (cpuMode) {
drawGameText("Difficulty: " + difficulty.toUpperCase(), "", 40, 570, Color.WHITE, Color.BLACK);
}
}
public void addScore()
{
score += 2;
basket.moveToRandomLocation();
if (score == 10 || (score > 10 && score % 10 == 0))
{
addBoundary();
}
moveAllBoundaries();
}
private void addBoundary()
{
int attempts = 0;
int maxAttempts = 50;
while (attempts < maxAttempts)
{
int basketX = basket.getX();
int basketY = basket.getY();
int offsetX = Greenfoot.getRandomNumber(200) - 100;
int offsetY = Greenfoot.getRandomNumber(150) - 75;
int newX = Math.max(50, Math.min(getWidth() - 50, basketX + offsetX));
int newY = Math.max(50, Math.min(getHeight() - 100, basketY + offsetY));
if (isValidBoundaryPosition(newX, newY, null))
{
Boundary newBoundary = new Boundary();
addObject(newBoundary, newX, newY);
boundaries.add(newBoundary);
break;
}
attempts++;
}
}
private void moveAllBoundaries()
{
for (Boundary boundary : boundaries)
{
moveBoundaryToNewPosition(boundary);
}
}
private void moveBoundaryToNewPosition(Boundary boundary)
{
int attempts = 0;
int maxAttempts = 50;
while (attempts < maxAttempts)
{
int basketX = basket.getX();
int basketY = basket.getY();
int offsetX = Greenfoot.getRandomNumber(200) - 100;
int offsetY = Greenfoot.getRandomNumber(150) - 75;
int newX = Math.max(50, Math.min(getWidth() - 50, basketX + offsetX));
int newY = Math.max(50, Math.min(getHeight() - 100, basketY + offsetY));
if (isValidBoundaryPosition(newX, newY, boundary))
{
boundary.setLocation(newX, newY);
break;
}
attempts++;
}
}
private boolean isValidBoundaryPosition(int x, int y, Boundary excludeBoundary)
{
double distanceFromBasket = Math.sqrt((x - basket.getX()) * (x - basket.getX()) +
(y - basket.getY()) * (y - basket.getY()));
if (distanceFromBasket < 100)
{
return false;
}
double distanceFromBackboard = Math.sqrt((x - backboard.getX()) * (x - backboard.getX()) +
(y - backboard.getY()) * (y - backboard.getY()));
if (distanceFromBackboard < 100)
{
return false;
}
for (Boundary existingBoundary : boundaries)
{
if (existingBoundary != excludeBoundary)
{
double distance = Math.sqrt((x - existingBoundary.getX()) * (x - existingBoundary.getX()) +
(y - existingBoundary.getY()) * (y - existingBoundary.getY()));
if (distance < 120)
{
return false;
}
}
}
return true;
}
public boolean isGameOver()
{
return gameOver;
}
private void drawGameText(String label, int value, int x, int y, Color mainColor, Color outlineColor)
{
String text = label + value;
Font font = new Font("Arial", true, false, 28);
GreenfootImage img = new GreenfootImage(text, 28, mainColor, new Color(0, 0, 0, 0));
GreenfootImage outline = new GreenfootImage(text, 28, outlineColor, new Color(0, 0, 0, 0));
GreenfootImage combined = new GreenfootImage(img.getWidth() + 4, img.getHeight() + 4);
for (int dx = -2; dx <= 2; dx++)
for (int dy = -2; dy <= 2; dy++)
combined.drawImage(outline, dx + 2, dy + 2);
combined.drawImage(img, 2, 2);
getBackground().drawImage(combined, x, y);
}
private void drawGameText(String label, String value, int x, int y, Color mainColor, Color outlineColor)
{
String text = label + value;
Font font = new Font("Arial", true, false, 20);
GreenfootImage img = new GreenfootImage(text, 20, mainColor, new Color(0, 0, 0, 0));
GreenfootImage outline = new GreenfootImage(text, 20, outlineColor, new Color(0, 0, 0, 0));
GreenfootImage combined = new GreenfootImage(img.getWidth() + 4, img.getHeight() + 4);
for (int dx = -2; dx <= 2; dx++)
for (int dy = -2; dy <= 2; dy++)
combined.drawImage(outline, dx + 2, dy + 2);
combined.drawImage(img, 2, 2);
getBackground().drawImage(combined, x, y);
}
}