forked from Gaspared/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
228 lines (193 loc) · 4.83 KB
/
Main.java
File metadata and controls
228 lines (193 loc) · 4.83 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
package application;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
public class Main extends Application {
// variable
static int speed = 5;
static int foodcolor = 0;
static int width = 20;
static int height = 20;
static int foodX = 0;
static int foodY = 0;
static int cornersize = 25;
static List<Corner> snake = new ArrayList<>();
static Dir direction = Dir.left;
static boolean gameOver = false;
static Random rand = new Random();
public enum Dir {
left, right, up, down
}
public static class Corner {
int x;
int y;
public Corner(int x, int y) {
this.x = x;
this.y = y;
}
}
public void start(Stage primaryStage) {
try {
newFood();
VBox root = new VBox();
Canvas c = new Canvas(width * cornersize, height * cornersize);
GraphicsContext gc = c.getGraphicsContext2D();
root.getChildren().add(c);
new AnimationTimer() {
long lastTick = 0;
public void handle(long now) {
if (lastTick == 0) {
lastTick = now;
tick(gc);
return;
}
if (now - lastTick > 1000000000 / speed) {
lastTick = now;
tick(gc);
}
}
}.start();
Scene scene = new Scene(root, width * cornersize, height * cornersize);
// control
scene.addEventFilter(KeyEvent.KEY_PRESSED, key -> {
if (key.getCode() == KeyCode.W) {
direction = Dir.up;
}
if (key.getCode() == KeyCode.A) {
direction = Dir.left;
}
if (key.getCode() == KeyCode.S) {
direction = Dir.down;
}
if (key.getCode() == KeyCode.D) {
direction = Dir.right;
}
});
// add start snake parts
snake.add(new Corner(width / 2, height / 2));
snake.add(new Corner(width / 2, height / 2));
snake.add(new Corner(width / 2, height / 2));
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("SNAKE GAME");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
// tick
public static void tick(GraphicsContext gc) {
if (gameOver) {
gc.setFill(Color.RED);
gc.setFont(new Font("", 50));
gc.fillText("GAME OVER", 100, 250);
return;
}
for (int i = snake.size() - 1; i >= 1; i--) {
snake.get(i).x = snake.get(i - 1).x;
snake.get(i).y = snake.get(i - 1).y;
}
switch (direction) {
case up:
snake.get(0).y--;
if (snake.get(0).y < 0) {
gameOver = true;
}
break;
case down:
snake.get(0).y++;
if (snake.get(0).y > height) {
gameOver = true;
}
break;
case left:
snake.get(0).x--;
if (snake.get(0).x < 0) {
gameOver = true;
}
break;
case right:
snake.get(0).x++;
if (snake.get(0).x > width) {
gameOver = true;
}
break;
}
// eat food
if (foodX == snake.get(0).x && foodY == snake.get(0).y) {
snake.add(new Corner(-1, -1));
newFood();
}
// self destroy
for (int i = 1; i < snake.size(); i++) {
if (snake.get(0).x == snake.get(i).x && snake.get(0).y == snake.get(i).y) {
gameOver = true;
}
}
// fill
// background
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, width * cornersize, height * cornersize);
// score
gc.setFill(Color.WHITE);
gc.setFont(new Font("", 30));
gc.fillText("Score: " + (speed - 6), 10, 30);
// random foodcolor
Color cc = Color.WHITE;
switch (foodcolor) {
case 0:
cc = Color.PURPLE;
break;
case 1:
cc = Color.LIGHTBLUE;
break;
case 2:
cc = Color.YELLOW;
break;
case 3:
cc = Color.PINK;
break;
case 4:
cc = Color.ORANGE;
break;
}
gc.setFill(cc);
gc.fillOval(foodX * cornersize, foodY * cornersize, cornersize, cornersize);
// snake
for (Corner c : snake) {
gc.setFill(Color.LIGHTGREEN);
gc.fillRect(c.x * cornersize, c.y * cornersize, cornersize - 1, cornersize - 1);
gc.setFill(Color.GREEN);
gc.fillRect(c.x * cornersize, c.y * cornersize, cornersize - 2, cornersize - 2);
}
}
// food
public static void newFood() {
start: while (true) {
foodX = rand.nextInt(width);
foodY = rand.nextInt(height);
for (Corner c : snake) {
if (c.x == foodX && c.y == foodY) {
continue start;
}
}
foodcolor = rand.nextInt(5);
speed++;
break;
}
}
public static void main(String[] args) {
launch(args);
}
}