Repository navigation
Expand file tree
/
Copy pathGamePane.java
More file actions
276 lines (249 loc) 路 11 KB
/
Copy pathGamePane.java
File metadata and controls
276 lines (249 loc) 路 11 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
package org.example.gamedemo;
import javafx.animation.*;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.util.Duration;
public class GamePane extends Pane {
private static final int CELL_SIZE = 40;
private static final int BOARD_WIDTH = 760;
private static final int BOARD_HEIGHT = 760;
private final PacMan pacman ;
private final Stage stage ;
private final Scene mainScene;
private final Scene gameScene;
private final GameSounds sound ;
private final Ghost[] ghosts;
private Timeline positionChecker;
private boolean death = false; // To Know Pacman is still alive or die
public GamePane(int mapNumber, GameSounds sound , int level, int pacmanGifNum, Stage stage, Scene mainScene ) {
this.sound = sound;
this.stage = stage;
this.mainScene = mainScene;
MazeView mazeView = new MazeView(new Maze(BOARD_WIDTH, BOARD_HEIGHT, CELL_SIZE, mapNumber));
pacman = new PacMan(1, 1, mazeView, pacmanGifNum);
ghosts = createGhosts(mazeView, level);
getChildren().addAll(createBackground(mapNumber), mazeView);
gameScene = new Scene(this, BOARD_WIDTH, BOARD_HEIGHT);
movePacman();
checkLife();
}
// Returning ghosts (Array) created based on level
public Ghost[] createGhosts(MazeView mazeView, int level) {
Ghost[] ghosts = new Ghost[4];
switch (level) {
case 1, 2, 3:
ghosts[0] = new Ghost(17, 17, mazeView, level);
ghosts[0].setMode(1);
ghosts[1] = new Ghost(17, 1, mazeView, level);
ghosts[1].setMode(2);
ghosts[2] = new Ghost(1, 17, mazeView, level);
ghosts[2].setMode(3);
ghosts[3] = new Ghost(9, 6, mazeView, level);
break;
case 4,5:
ghosts[0] = new Ghost(17, 17, mazeView, 3);
ghosts[0].setMode(1);
ghosts[1] = new Ghost(17, 1, mazeView, 3);
ghosts[1].setMode(2);
ghosts[2] = new Ghost(1, 17, mazeView, 3);
ghosts[2].setMode(3);
ghosts[3] = new Ghost(9, 6, mazeView, level);
break;
}
return ghosts;
}
// Responsible for moving the pacman (Timeline)
public void movePacman() {
gameScene.setOnKeyPressed(event -> {
switch (event.getCode()) {
case RIGHT:
pacman.setDirection(Direction.RIGHT); //to set last press right
break;
case DOWN:
pacman.setDirection(Direction.DOWN); //to set last press down
break;
case UP:
pacman.setDirection(Direction.UP); //to set last press up
break;
case LEFT:
pacman.setDirection(Direction.LEFT); //to set last press left
break;
}
});
pacman.getAutoMovement().setCycleCount(-1);
pacman.getAutoMovement().play();
}
//-------------Helper Methods---------------//
private ImageView createBackground(int mazeNum) {
String[] mazes = new String[]{"GameBackground1.gif", "GameBackground2.gif", "GameBackground3.gif"};
ImageView background = new ImageView(mazes[mazeNum - 1]);
background.setFitWidth(BOARD_WIDTH);
background.setFitHeight(BOARD_HEIGHT);
return background;
}
private void checkLife() {
death = false;
sound.deathSound.stop();
sound.winSound.stop();
positionChecker = new Timeline(new KeyFrame(Duration.millis(40), e -> {
for (Ghost ghost : ghosts) {
if (ghost.getCurrentColumn() == pacman.getCurrentColumn() && ghost.getCurrentRow() == pacman.getCurrentRow()) {
death = true;
ghost.stopGhostMovement();
}
}
if (death || pacman.getMazeView().getMaze().isFinishedMap()) {
startEndScene(death);
}
}));
positionChecker.setCycleCount(-1);
positionChecker.play();
}
// Viewing Ending Screens [Win - Game Over]
private void startEndScene(boolean isDeath) {
positionChecker.stop();
ImageView[] endingImgs = {new ImageView("GameoverScreen.gif"), new ImageView("WinScreen.jpg")};
ScaleTransition[] imgsTransition = new ScaleTransition[2];
for (int i = 0; i < 2; i++) {
// Setting Image Properties
endingImgs[i].setFitHeight(5);
endingImgs[i].setFitWidth(5);
endingImgs[i].setPreserveRatio(true);
endingImgs[i].setLayoutX(380);
endingImgs[i].setLayoutY(380);
// Setting Image's Transition Effect
imgsTransition[i] = new ScaleTransition();
imgsTransition[i].setNode(endingImgs[i]);
imgsTransition[i].setDuration(Duration.seconds(.5));
imgsTransition[i].setToX(153);
imgsTransition[i].setToY(153);
this.getChildren().add(endingImgs[i]);
endingImgs[i].setVisible(false);
}
FadeTransition[] txtsFadeTransition = new FadeTransition[5];
StrokeTransition[] txtsStrokeTransition = new StrokeTransition[5];
Text[] endGameTexts = initializeEndGameTexts(txtsStrokeTransition, txtsFadeTransition);
if (isDeath) {
handleGameOver(endingImgs, endGameTexts, txtsFadeTransition, txtsStrokeTransition, imgsTransition);
} else {
handleGameWin(endingImgs, endGameTexts, txtsFadeTransition, txtsStrokeTransition, imgsTransition);
}
}
// Setting Ending Texts [Press ESC - Press Enter - You Won] and Their Effects
private Text[] initializeEndGameTexts(StrokeTransition[] txtsStrokeTransition, FadeTransition[] txtsFadeTransition) {
Text[] endGameTexts = new Text[5];
endGameTexts[0] = new Text("Press 'Enter' For Main Menu");
endGameTexts[0].setLayoutX(240);
endGameTexts[0].setLayoutY(630);
endGameTexts[1] = new Text("Press 'ESC' To Exit");
endGameTexts[1].setLayoutX(291);
endGameTexts[1].setLayoutY(690);
endGameTexts[2] = new Text("You Won");
endGameTexts[2].setLayoutX(306);
endGameTexts[2].setLayoutY(100);
endGameTexts[3] = new Text("Score: ");
endGameTexts[3].setLayoutX(10);
endGameTexts[3].setLayoutY(30);
endGameTexts[4] = new Text("Previous Score: ");
endGameTexts[4].setLayoutX(10);
endGameTexts[4].setLayoutY(60);
for (int i = 0; i < 5; i++) {
// Setting Text Properties
endGameTexts[i].setVisible(false);
endGameTexts[i].setStyle("-fx-background-color: transparent;");
endGameTexts[i].setFont(Font.font("Arial", FontWeight.BOLD, 24));
endGameTexts[i].setFill(Color.CORNSILK);
endGameTexts[i].setStroke(Color.WHITE);
endGameTexts[i].setStrokeWidth(1);
// Setting Fading Transition
txtsFadeTransition[i] = new FadeTransition(Duration.seconds(1), endGameTexts[i]);
txtsFadeTransition[i].setToValue(0.1);
txtsFadeTransition[i].setCycleCount(-1);
txtsFadeTransition[i].setAutoReverse(true);
// Setting Stroke transition
txtsStrokeTransition[i] = new StrokeTransition(Duration.seconds(1), endGameTexts[i]);
txtsStrokeTransition[i].setFromValue(Color.WHITE);
txtsStrokeTransition[i].setToValue(Color.TRANSPARENT);
txtsStrokeTransition[i].setAutoReverse(true);
txtsStrokeTransition[i].setCycleCount(-1);
// Attaching Txt To The Pane
this.getChildren().add(endGameTexts[i]);
}
// Over Edit For "Your Win" Text.
endGameTexts[2].setFont(Font.font("Arial", FontWeight.BOLD, 40));
endGameTexts[2].setFill(Color.GOLD);
txtsStrokeTransition[2].setFromValue(Color.LIGHTGOLDENRODYELLOW);
return endGameTexts;
}
// Setting Winning Screen With All Effects Related.
private void handleGameWin(ImageView[] endingImgs, Text[] endGameTexts, FadeTransition[] txtsFadeTransition,
StrokeTransition[] txtsStrokeTransition, ScaleTransition[] imgsTransition) {
pacman.getAutoMovement().stop();
pacman.getChildren().remove(pacman.getGif());
this.getChildren().remove(pacman);
// Win
endingImgs[1].setVisible(true);
imgsTransition[1].play();
imgsTransition[1].setOnFinished(event1 -> {
endGameTexts[3].setText(endGameTexts[3].getText()+ pacman.getScore());
endGameTexts[4].setText(endGameTexts[4].getText()+ pacman.getPreviousScore());
for(int i = 0; i < 5; i++){
endGameTexts[i].setVisible(true);
txtsFadeTransition[i].play();
txtsStrokeTransition[i].play();
}
gameScene.setOnKeyPressed(keyEvent -> {
switch (keyEvent.getCode()) {
case ENTER -> {
PacMan.setPreviousScore(pacman.getScore());
stage.setScene(mainScene);
sound.winSound.stop();
}
case ESCAPE -> stage.close();
}
});
});
positionChecker.stop();
sound.winSound.play();
}
// Setting Game Over Screen With All Effects Related.
private void handleGameOver( ImageView[] endingImgs, Text[] endGameTexts, FadeTransition[] txtsFadeTransition,
StrokeTransition[] txtsStrokeTransition, ScaleTransition[] imgsTransition) {
sound.deathSound.play();
death = false;
pacman.getAutoMovement().stop();
pacman.getChildren().remove(pacman.getGif());
this.getChildren().remove(pacman);
// Game Over
endingImgs[0].setVisible(true);
imgsTransition[0].play();
imgsTransition[0].setOnFinished(event1 -> {
endGameTexts[3].setText(endGameTexts[3].getText()+ pacman.getScore());
endGameTexts[4].setText(endGameTexts[4].getText()+ pacman.getPreviousScore());
for(int i = 0; i < 5; i++){
if(i != 2) // i = 2 -> "You Won"
{
endGameTexts[i].setVisible(true);
txtsFadeTransition[i].play();
txtsStrokeTransition[i].play();
}
}
gameScene.setOnKeyPressed(keyEvent -> {
switch (keyEvent.getCode()) {
case ENTER -> {
PacMan.setPreviousScore(pacman.getScore());
stage.setScene(mainScene);
sound.deathSound.stop();
}
case ESCAPE -> {
stage.close();
}
}
});
});
}
}