-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.java
More file actions
99 lines (88 loc) · 2.19 KB
/
window.java
File metadata and controls
99 lines (88 loc) · 2.19 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
//Jeremy Okeyo
//Used to setup the game window, when play is selected
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Random;
public class window extends Application{
Random rand = new Random();
public static final int winSize = 16;
private Group board;
private Scene game;
private Canvas canvas;
private GraphicsContext gc;
public static final double size = 19.53;
private game gme;
//Sets up the game window
public void setWindow() {
board = new Group();
game = new Scene(board, 312.5, 312.5, Color.BLACK);
canvas = new Canvas(312.5, 312.5);
gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK); //Change to Black after test
board.getChildren().add(canvas);
}
//Adds the food to the specific cell
public void popFood() {
//Used to populate board with food
int foodCol = rand.nextInt(winSize);
int foodRow = rand.nextInt(winSize);
gme.setFood(foodCol, foodRow);
}
//Sets the board to its default starting point
private void setBoard() {
gc.setFill(Color.BLACK);
gc.fillRect(0,0,312.5, 312.5);
}
//Initializes the controls
private void initializeControls() {
//Sets up Controls
game.setOnKeyPressed((e) -> {
gme.validKey(e);
}
);
new AnimationTimer() {
boolean gameOvr = false;
long lastTick = 0;
@Override
public void handle (long now) {
if (lastTick == 0) {
lastTick = now;
setBoard();
gameOvr = gme.gameLoop(gc);
return;
}
if (now - lastTick > 100000000) {
setBoard();
lastTick = now;
gameOvr = gme.gameLoop(gc);
}
if (gameOvr) {
this.stop();
}
}
}.start();
}
@Override
public void start(Stage stage) throws IOException {
//OnMousePressed
gme = new game();
setWindow();
popFood();
stage.setScene(game);
initializeControls();
stage.setTitle("Snakes");
stage.setScene(game);
stage.sizeToScene();
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}