-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameImpl.java
More file actions
243 lines (222 loc) · 8.3 KB
/
Copy pathGameImpl.java
File metadata and controls
243 lines (222 loc) · 8.3 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
import javafx.scene.layout.*;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.animation.AnimationTimer;
import javafx.scene.input.MouseEvent;
import javafx.event.*;
import java.util.*;
public class GameImpl extends Pane implements Game {
/*
*****************************************************************************************************************************************************
***** ENUMERATIONS **********************************************************************************************************************************
*****************************************************************************************************************************************************
*/
/**
* Defines different states of the game.
*/
public enum GameState
{
WON, LOST, ACTIVE, NEW
}
/*
*****************************************************************************************************************************************************
***** CONSTANTS *************************************************************************************************************************************
*****************************************************************************************************************************************************
*/
/**
* The width of the game board.
*/
public static final int WIDTH = 400;
/**
* The height of the game board.
*/
public static final int HEIGHT = 600;
/**
* The height of where the images start
*/
public static final int NEW_HEIGHT = HEIGHT / 2;
/*
*****************************************************************************************************************************************************
***** INSTANCE VARIABLES / CONSTRUCTOR **************************************************************************************************************
*****************************************************************************************************************************************************
*/
private Ball ball;
private Paddle paddle;
private Animal[] animals = new Animal[16];
private AudioClip backgroundMusic = null;
private int ballBounceCount = 0;
private int animalsRemaining = animals.length;
/**
* Constructs a new GameImpl.
*/
public GameImpl () {
setStyle("-fx-background-color: white;");
restartGame(GameState.ACTIVE);
}
/*
*****************************************************************************************************************************************************
***** PUBLIC METHODS ********************************************************************************************************************************
*****************************************************************************************************************************************************
*/
/**
* @return Returns the name of the game.
*/
public String getName () {
return "Zutopia";
}
/**
* @return Returns the object calling this method.
*/
public Pane getPane () {
return this;
}
/**
* Clears the board with whatever given state that has been passed in.
* @param state the state of the game currently
*/
private void restartGame (GameState state) {
getChildren().clear(); // remove all components from the game
// Create and add ball
ball = new Ball();
getChildren().add(ball.getCircle()); // Add the ball to the game board
// Create and add paddle
paddle = new Paddle();
getChildren().add(paddle.getRectangle()); // Add the paddle to the game board
//Creates a 4x4 board of images
Image duck = new Image("duck.jpg");
Image goat = new Image("goat.jpg");
Image horse = new Image("horse.jpg");
String duckSound = "quack.wav";
String horseSound = "whinny.wav";
String goatSound = "bleat.wav";
//FIRST ROW
animals[0] = new Animal(duck, duckSound, WIDTH/4 - 50, NEW_HEIGHT/12);
animals[1] = new Animal(goat, goatSound, WIDTH/4 + 35, NEW_HEIGHT/12);
animals[2] = new Animal(horse, horseSound, WIDTH/2 + 25, NEW_HEIGHT/12);
animals[3] = new Animal(duck, duckSound, (WIDTH/4)*3 + 25, NEW_HEIGHT/12);
//SECOND ROW
animals[4] = new Animal(goat, goatSound, WIDTH/4 - 50, NEW_HEIGHT/3);
animals[5] = new Animal(horse, horseSound, WIDTH/4 + 35, NEW_HEIGHT/3);
animals[6] = new Animal(duck, duckSound, WIDTH/2 + 25, NEW_HEIGHT/3);
animals[7] = new Animal(goat, goatSound, (WIDTH/4)*3 + 25, NEW_HEIGHT/3);
//THIRD ROW.
animals[8] = new Animal(horse, horseSound, WIDTH/4 - 50, NEW_HEIGHT/3 + 75);
animals[9] = new Animal(duck, duckSound, WIDTH/4 + 35, NEW_HEIGHT/3 + 75);
animals[10] = new Animal(goat, goatSound, WIDTH/2 + 25, NEW_HEIGHT/3 + 75);
animals[11] = new Animal(horse, horseSound, (WIDTH/4)*3 + 25, NEW_HEIGHT/3 + 75);
//FOURTH ROW.
animals[12] = new Animal(duck, duckSound, WIDTH/4 - 50, NEW_HEIGHT/2 + 100);
animals[13] = new Animal(goat, goatSound, WIDTH/4 + 35, NEW_HEIGHT/2 + 100);
animals[14] = new Animal(horse, horseSound, WIDTH/2 + 25, NEW_HEIGHT/2 + 100);
animals[15] = new Animal(duck, duckSound, (WIDTH/4)*3 + 25, NEW_HEIGHT/2 + 100);
for(int i = 0; i < animals.length; i++)
{
getChildren().add(animals[i].getSprite());
}
if(backgroundMusic != null)
{
backgroundMusic.stop();
}
backgroundMusic = new AudioClip(getClass().getClassLoader().getResource("Old MacDonald Had a Farm.wav").toString());
backgroundMusic.play();
// Add start message
final String message;
if (state == GameState.LOST) {
message = "Game Over\n";
} else if (state == GameState.WON) {
message = "You won!\n";
} else {
message = "";
}
final Label startLabel = new Label(message + "Click mouse to start");
startLabel.setLayoutX(WIDTH / 2 - 50);
startLabel.setLayoutY(HEIGHT / 2 + 100);
getChildren().add(startLabel);
// Add event handler to start the game
setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle (MouseEvent e) {
GameImpl.this.setOnMouseClicked(null);
// As soon as the mouse is clicked, remove the startLabel from the game board
getChildren().remove(startLabel);
run();
}
});
// Add another event handler to steer paddle...
setOnMouseMoved(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent event)
{
paddle.moveTo(event.getX(), event.getY());
}
});
}
/**
* Begins the game-play by creating and starting an AnimationTimer.
*/
public void run () {
// Instantiate and start an AnimationTimer to update the component of the game.
new AnimationTimer () {
private long lastNanoTime = -1;
public void handle (long currentNanoTime) {
if (lastNanoTime >= 0) { // Necessary for first clock-tick.
GameState state;
if ((state = runOneTimestep(currentNanoTime - lastNanoTime)) != GameState.ACTIVE) {
// Once the game is no longer ACTIVE, stop the AnimationTimer.
stop();
// Restart the game, with a message that depends on whether
// the user won or lost the game.
restartGame(state);
}
}
// Keep track of how much time actually transpired since the last clock-tick.
lastNanoTime = currentNanoTime;
}
}.start();
}
/**
* Updates the state of the game at each timestep. In particular, this method should
* move the ball, check if the ball collided with any of the animals, walls, or the paddle, etc.
* @param deltaNanoTime how much time (in nanoseconds) has transpired since the last update
* @return the current game state
*/
public GameState runOneTimestep (long deltaNanoTime) {
ball.updatePosition(deltaNanoTime);
if(ball.intersect(paddle))
{
ball.resolve_collision(paddle);
}
for(int i = 0; i < animals.length; i++)
{
if(animals[i] != null)
{
if(ball.intersect(animals[i]))
{
ball.resolve_collision(animals[i]);
getChildren().remove(animals[i].getSprite());
animals[i] = null;
animalsRemaining--;
if(animalsRemaining == 0)
{
animalsRemaining = animals.length;
ballBounceCount = 0;
return GameState.WON;
}
}
}
}
if (ball.getY() + Ball.BALL_RADIUS > HEIGHT) {
ballBounceCount++;
if (ballBounceCount == 5) {
ballBounceCount = 0;
animalsRemaining = animals.length;
return GameState.LOST;
}
}
return GameState.ACTIVE;
}
}