-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.java
More file actions
161 lines (146 loc) · 3.77 KB
/
game.java
File metadata and controls
161 lines (146 loc) · 3.77 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
//Jeremy Okeyo
//Keeps track of snake on
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.Random;
public class game {
private Random rand = new Random();
private ArrayList<Cell> snake = new ArrayList<>();
private int level;
private int length;
private int y = 2;
private int x = 12;
private Cell food;
private int winSize = window.winSize;
private double cellSize = window.size;
private boolean gameOver = false;
public boolean pause = false;
//Creates a new game
public game() {
food = new Cell();
setFood();
length = 3;
for (int i = 0; i < length; i++) {
snake.add(new Cell(x + i, y,'l'));
}
}
//Used to set new food location
public void setFood () {
food.setLoc(rand.nextInt(winSize),rand.nextInt(winSize));
}
//Used to set food location from load file
public void setFood (int foodCol, int foodRow) {
food.setLoc(foodCol, foodRow);
}
public int getLength() {
return length;
}
private boolean grow() {
Cell last = snake.get(length - 1);
switch (last.getDir()) {
case 'u':
snake.add(new Cell(last.getX(),last.getY() - 1,
last.getDir()));
break;
case 'd':
snake.add(new Cell(last.getX(),last.getY() + 1,
last.getDir()));
break;
case 'l':
snake.add(new Cell(last.getX() + 1,last.getY(),
last.getDir()));
break;
case 'r':
snake.add(new Cell(last.getX() - 1,last.getY(),
last.getDir()));
break;
}
snake.get(length).setLoc(outOfBounds(snake.get(length).getX()),
outOfBounds(snake.get(length).getY()));
length += 1;
return true;
}
public boolean validKey(KeyEvent e) {
if (e.getCode() == KeyCode.ESCAPE) {
if (!pause)
pause = true;
else
pause = false;
}
switch (snake.get(0).getDir()) {
case 'u':
case 'd':
if (e.getCode() == KeyCode.LEFT){
snake.get(0).setDir('l');
}
if (e.getCode() == KeyCode.RIGHT){
snake.get(0).setDir('r');
}
break;
case 'l':
case 'r':
if (e.getCode() == KeyCode.DOWN){
snake.get(0).setDir('d');
}
if (e.getCode() == KeyCode.UP){
snake.get(0).setDir('u');
}
break;
}
return true;
}
public boolean gameLoop(GraphicsContext gc) {
gc.setFill(Color.RED);
printToScreen_F(gc, food);
if (food.equals(snake.get(0))) {
setFood();
grow();
}
gc.setFill(Color.WHITE);
for (int i = length - 1; i > 0; i--) {
snake.get(i).setLoc(snake.get(i - 1).getX(), snake.get(i - 1).getY());
printToScreen_S(gc, snake.get(i));
}
slither();
snake.get(0).setLoc(outOfBounds(snake.get(0).getX()), outOfBounds(snake.get(0).getY()));
gc.setFill(Color.RED);
printToScreen_S(gc, snake.get(0));
for (int i = 1; i < length; i++) {
if (snake.get(0).equals(snake.get(i))) {
int score = length * 100;
gameOver = true;
gc.fillRect(140, 140, 100, 75);
gc.setFill(Color.WHITE);
gc.fillText("Game Over!", 152, 152, 50);
gc.fillText("Score:", 172, 180, 30);
gc.fillText(String.valueOf(score), 220, 180, 30);
return gameOver;
}
}
return gameOver;
}
//Checks whether snake is out of bounds
public static int outOfBounds(int X) {
if (X == -1)
return 15;
else if (X == 16)
return 0;
else
return X;
}
//Prints the snake Cell to screen;
private void printToScreen_S(GraphicsContext gc, Cell cell) {
gc.fillRect(cell.getX() * cellSize + 1, cell.getY() * cellSize + 1, cellSize - 2, cellSize - 2);
}
//Prints the food Cell to screen;
private void printToScreen_F(GraphicsContext gc, Cell food) {
gc.fillOval(food.getX() * cellSize, food.getY() * cellSize, cellSize, cellSize);
}
//Moves head depending on the button pressed
private void slither() {
snake.get(0).move(snake.get(0).getDir());
}
}