-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.pde
175 lines (141 loc) · 3.05 KB
/
snake.pde
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
import java.util.*;
void setup () {
// Prelim
size(500, 550);
stroke(255);
strokeWeight(2);
ellipseMode(CENTER);
// Game
rectMode(CORNER);
frameRate(120);
// Init speed setting functionality
cycleSpeed();
}
void draw () {
background(100);
scoreboard();
speedButton.draw();
// --- VARIABLE UI ---
if (snakeDied || playing) {
drawSnake();
drawFood();
}
if (!playing) {
if (viewBoard) {
viewBoardExitButton.draw();
} else {
playButton.draw();
if (snakeDied) {
viewBoardButton.draw();
}
}
}
// --- GAME ---
if (playing) {
moveSnake();
}
}
void keyPressed () {
switch (keyCode) {
case UP:
snake.setDirection('U');
break;
case DOWN:
snake.setDirection('D');
break;
case LEFT:
snake.setDirection('L');
break;
case RIGHT:
snake.setDirection('R');
break;
}
}
void mousePressed () {
// If play button is pressed
if (!playing && playButton.mouseIsOver()) {
snake = new Snake();
newFood();
score = 0;
playing = true;
}
if (!playing && speedButton.mouseIsOver()) {
cycleSpeed();
score = 0;
}
if (!playing && snakeDied) {
if (!viewBoard && viewBoardButton.mouseIsOver()) {
viewBoard = true;
} else if (viewBoard && viewBoardExitButton.mouseIsOver()) {
viewBoard = false;
}
}
}
// ---- UI ELEMENTS ----
void scoreboard () {
// Box
fill(70);
strokeWeight(0);
rect(0, 0, 500, SCORE_HEIGHT);
fill(255);
textSize(23);
// Score text
textAlign(RIGHT, CENTER);
text("Score: " + score.toString(), 490, SCORE_HEIGHT/2);
// High score text
textAlign(CENTER, CENTER);
text("Best: " + highScore.toString(), 250, SCORE_HEIGHT/2);
strokeWeight(2); // Reset to original
}
void drawSnake () {
for (SnakePoint p : snake.getBody()) {
fill(p.getColor());
rect(p.getXCoord(), p.getYCoord(), SEG_SIZE, SEG_SIZE);
}
}
void drawFood () {
fill(food.getColor());
rect(food.getXCoord(), food.getYCoord(), SEG_SIZE, SEG_SIZE);
}
void moveSnake () {
// Only move at certain intervals, but keep framerate high
// to lessen input latency
if (frameCount % speed == 0) {
if (speedText == "Neural") {
playing = NAI.processInput();
} else if (speedText == "Brute") {
playing = BAI.processInput();
} else {
playing = snake.moveAuto();
}
if (snake.eating(food)) {
newFood();
snake.addPoints(GROW_AMT);
score++;
if (score > highScore) { highScore = score; }
}
if (!playing) {
snakeDied = true;
}
}
}
// ---- UTIL FUNCTIONS ----
// TODO: Modify and add view board button
Boolean mouseOverPlay () {
float disX = 250 - mouseX;
float disY = 250 + SCORE_HEIGHT - mouseY;
if (sqrt(sq(disX) + sq(disY)) < PLAY_BUTTON_DIAM/2) {
return true;
} else {
return false;
}
}
Boolean mouseOverSpeed() {
int w = 100;
int h = SCORE_HEIGHT/2+10;
int x1 = 10;
int y1 = 10;
int x2 = w + x1;
int y2 = h + y1;
return (mouseX > x1 && mouseX < x2 && mouseY > y1 && mouseY < y2);
}