-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
160 lines (144 loc) · 5.01 KB
/
Main.java
File metadata and controls
160 lines (144 loc) · 5.01 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
package snake;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author Jirka
*/
public class Main extends Application {
private final Drawing drw = new Drawing();
private final Game gm = new Game();
@Override
public void start(Stage primaryStage) {
//Základní nastavení
StackPane root = new StackPane();
Scene scene = new Scene(root, 500, 620);
primaryStage.setTitle("Snake");
Canvas canvas = new Canvas(scene.getWidth(), scene.getHeight());
final GraphicsContext gc = canvas.getGraphicsContext2D();
drw.draw(gc, canvas.getWidth(), canvas.getHeight());
//nastavení timeru
Timeline timer = new Timeline(new KeyFrame(Duration.millis(1000), (ActionEvent event) -> {
if (!gm.getGameOver()){
gm.setTime();
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
Timeline timer5 = new Timeline(new KeyFrame(Duration.millis(30), (ActionEvent event) -> {
if (gm.getSnake().size() >= 24){
gm.next();
drw.draw(gc, canvas.getWidth(), canvas.getHeight());
gm.setLevel(5);
}
}));
timer5.setCycleCount(Timeline.INDEFINITE);
timer5.play();
Timeline timer4 = new Timeline(new KeyFrame(Duration.millis(50), (ActionEvent event) -> {
if (gm.getSnake().size() < 24 && gm.getSnake().size() >= 19){
gm.next();
drw.draw(gc, canvas.getWidth(), canvas.getHeight());
gm.setLevel(4);
}
}));
timer4.setCycleCount(Timeline.INDEFINITE);
timer4.play();
Timeline timer3 = new Timeline(new KeyFrame(Duration.millis(100), (ActionEvent event) -> {
if (gm.getSnake().size() < 19 && gm.getSnake().size() >= 14){
gm.next();
drw.draw(gc, canvas.getWidth(), canvas.getHeight());
gm.setLevel(3);
}
}));
timer3.setCycleCount(Timeline.INDEFINITE);
timer3.play();
Timeline timer2 = new Timeline(new KeyFrame(Duration.millis(150), (ActionEvent event) -> {
if (gm.getSnake().size() < 14 && gm.getSnake().size() >= 9){
gm.next();
drw.draw(gc, canvas.getWidth(), canvas.getHeight());
gm.setLevel(2);
}
}));
timer2.setCycleCount(Timeline.INDEFINITE);
timer2.play();
Timeline timer1 = new Timeline(new KeyFrame(Duration.millis(200), (ActionEvent event) -> {
if (gm.getSnake().size() < 9){
gm.next();
drw.draw(gc, canvas.getWidth(), canvas.getHeight());
gm.setLevel(1);
}
}));
timer1.setCycleCount(Timeline.INDEFINITE);
timer1.play();
//čtení kláves
scene.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent e) -> {
switch (e.getCode()) {
case W:
gm.setKey(0);
break;
case S:
gm.setKey(1);
break;
case A:
gm.setKey(2);
break;
case D:
gm.setKey(3);
break;
case NUMPAD8:
gm.setKey(0);
break;
case NUMPAD2:
gm.setKey(1);
break;
case NUMPAD4:
gm.setKey(2);
break;
case NUMPAD6:
gm.setKey(3);
break;
case UP:
gm.setKey(0);
break;
case DOWN:
gm.setKey(1);
break;
case LEFT:
gm.setKey(2);
break;
case RIGHT:
gm.setKey(3);
break;
case F5:
if (gm.getGameOver()){
gm.reset();
}
break;
case ESCAPE:
primaryStage.close();
break;
default:
break;
}
});
//vykrslení plochy
root.getChildren().add(canvas);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}