-
Notifications
You must be signed in to change notification settings - Fork 3
/
Game.java
175 lines (153 loc) · 4.17 KB
/
Game.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
package com.danilchican.pacman;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
/**
* The main class of game
*
* @author Vlad Danilchik
* @version 1.0
*/
public class Game extends Application {
/**
* The method return booleand of keycode press
*
* @param key keycode
* @return boolean key pressed
* @see Game#isPressed(KeyCode)
*/
private boolean isPressed(KeyCode key) {
return Constants.keys.getOrDefault(key, false);
}
/**
* Method update() by timer
*
* @see Game#update()
*/
private void update() {
if (Constants.startGame) {
Constants.save.saveMove(3);
if (isPressed(KeyCode.RIGHT)) {
Constants.player.setScaleX(1);
GamePlay.moveX(3);
}
if (isPressed(KeyCode.LEFT)) {
Constants.player.setScaleX(-1);
GamePlay.moveX(-3);
}
if (isPressed(KeyCode.UP)) {
GamePlay.moveY(-3);
}
if (isPressed(KeyCode.DOWN)) {
GamePlay.moveY(3);
}
Constants.save.saveMoves(Constants.player.getTranslateX(), Constants.player.getTranslateY());
if (Constants.enemies.isEmpty()) {
return;
} else {
Constants.save.saveMove(4);
for (Enemy enemy : Constants.enemies) {
enemy.move();
}
}
if ((Constants.CurrentCountBonuses == Constants.countFoodLevel)
&& Constants.player.isAlive()) {
Constants.startGame = false;
RepresenterScreen.finishScreen();
}
} else if (Constants.startBoot) {
Constants.boot.move();
if (Constants.enemies.isEmpty()) {
return;
} else {
Constants.save.saveMove(4);
for (Enemy enemy : Constants.enemies) {
enemy.move();
}
}
if (!(Constants.boot.isAlive())) {
MapGenerator.clearGameScene();
Constants.save.closeOutputStream();
}
if ((Constants.CurrentCountBonuses == Constants.countFoodLevel) && Constants.boot.isAlive()) {
Constants.save.closeOutputStream();
RepresenterScreen.finishScreen();
MapGenerator.clearGameScene();
if (GamePlay.nextLevel(Constants.currentLevel++)) {
GamePlay.startGame(Constants.currentLevel++);
} else {
Constants.currentLevel = 1;
}
}
} else if (Constants.startReplay) {
int temp;
temp = Constants.save.getIntFromFile();
switch (temp) {
case 3:
GamePlay.moveReplay();
break;
case 4:
for (Enemy enemy : Constants.enemies) {
enemy.moveReplay();
}
break;
}
if (!(Constants.player.isAlive())) {
MapGenerator.clearGameScene();
Constants.save.closeOutputStream();
}
if ((Constants.CurrentCountBonuses == Constants.countFoodLevel)
&& Constants.player.isAlive()) {
Constants.save.closeOutputStream();
RepresenterScreen.finishScreen();
MapGenerator.clearGameScene();
if (GamePlay.nextLevel(++Constants.currentLevel)) {
GamePlay.startGame(Constants.currentLevel);
} else {
Constants.currentLevel = 1;
}
}
}
}
/**
* The submain method of the game
*
* @param stage main stage
* @see Game#start(Stage)
*/
public void start(Stage stage) {
try {
Constants.main_scene = new Scene(MenuBox.drawMenu());
MenuBox.drawSubMenuLevels();
MenuBox.setMenuOptions();
GamePlay.setLevelsCount();
stage.setTitle("Pacman");
stage.setMaxWidth(1000);
stage.setMaxHeight(600);
stage.setMinWidth(1000);
stage.setMinHeight(600);
stage.setScene(Constants.main_scene);
stage.show();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
update();
}
};
timer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The main game method
*
* @param args
* @see Game#main(String[])
*/
public static void main(String[] args) {
launch(args);
}
}