-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
213 lines (200 loc) · 4.5 KB
/
sketch.js
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
let timer = 0;
let button;
canvasSize = [400, 450];
gameSize = [400, 400];
gameOffset = [0, 50];
var gridX = 16;
var gridY = 16;
var sizing = gameSize[0] / gridX; // the amount of pixels per tile
var refresh = 0.25 // in seconds
const snake = [3, 2, 1]; // stores the position of each part of the snake
var apple = 25;
var appleColor = [255, 0, 0];
var snakeColor = [0, 0, 255];
var right = [1, 0];
var left = [-1, 0];
var up = [0, -1];
var down = [0, 1];
var direction = right // starts as right
var quickDraw = false;
var queue = 0;
var score = 0;
var prevValue;
var gameLoop = true;
function coordinateToPoint(x, y)
{
return (y * gridX) + x;
}
function pointToCoordinate(i)
{
if (i == 0)
{
return [0, 0];
}
else
{
var x = i % gridX;
var y = (i - x) / gridY;
return [x, y];
}
}
function toPixels(x, y = -1)
{
var px, py;
if (y != -1)
{
px = x * sizing;
py = y * sizing;
}
else
{
var fCoords = pointToCoordinate(x);
px = fCoords[0] * sizing;
py = fCoords[1] * sizing;
}
return [px + gameOffset[0], py + gameOffset[1]];
}
function makeApple()
{
var going = true
var newApple;
while (going)
{
going = false;
newApple = Math.floor(Math.random() * ((gridX * gridY) - 1))
if (snake.includes(newApple))
{
going = true;
}
}
apple = newApple;
}
function addSnake()
{
if (queue > 0)
{
snake[snake.length] = prevValue;
queue--;
}
}
function moveSnake()
{
var pureDirection = coordinateToPoint(direction[0], direction[1]);
var snakeHead = pointToCoordinate(snake[0]);
var newSnakeHead = [snakeHead[0]+direction[0], snakeHead[1]+direction[1]];
prevValue = snake[0];
if (newSnakeHead[0] >= 0 && newSnakeHead[0] < gridX && newSnakeHead[1] >= 0 && newSnakeHead[1] < gridY && !(snake.includes(snake[0] + pureDirection)))
{
// possible, now continue
if (snake[0] + pureDirection == apple)
{
queue++;
score++;
makeApple();
addSnake();
}
snake[0] = snake[0] + pureDirection;
for (let i = 1; i < snake.length; i++)
{
var oldValue = snake[i];
snake[i] = prevValue;
prevValue = oldValue;
}
}
else
{
gameLoop = false;
}
}
function setup()
{
createCanvas(canvasSize[0], canvasSize[1]);
//noStroke();
}
function mousePressed()
{
if (mouseX > 0 && mouseX < canvasSize[0] && mouseY > 0 && mouseY < canvasSize[1] && !fullscreen())
{
fullscreen(true);
}
}
function keyPressed()
{
if (keyCode === ESCAPE && fullscreen())
{
fullscreen(false);
}
if (keyCode === LEFT_ARROW && direction != right)
{
direction = left;
quickDraw = true;
}
if (keyCode === RIGHT_ARROW && direction != left)
{
direction = right;
quickDraw = true;
}
if (keyCode === DOWN_ARROW && direction != up)
{
direction = down;
quickDraw = true;
}
if (keyCode === UP_ARROW && direction != down)
{
direction = up;
quickDraw = true;
}
if (key == ' ' && gameLoop == false)
{
snake.length = 0;
snake[0] = 3; snake[1] = 2; snake[2] = 1;
score = 0;
queue = 0;
makeApple();
direction = right;
gameLoop = true;
}
}
function draw()
{
// DRAW GUI
background(color(0, 255, 150));
fill(color(255, 255, 255));
rect(gameOffset[0], gameOffset[1], gameSize[0], gameSize[1]);
textSize(40); textStyle(ITALIC); textAlign(LEFT);
var scoreToDisplay = "Score: " + score;
text(scoreToDisplay, 8, 38);
// DRAW SNAKE
for (let i = 0; i < snake.length; i++)
{
var pCoords = toPixels(snake[i]);
px = pCoords[0]; py = pCoords[1]
fill(color(snakeColor));
square(px, py, sizing);
}
// DRAW APPLE
appleCoords = toPixels(apple);
apx = appleCoords[0]; apy = appleCoords[1];
fill(color(appleColor));
circle(apx + (sizing / 2), apy + (sizing / 2), sizing)
// GAME LOOP
if (gameLoop == true && (millis() >= (refresh * 1000) + timer || quickDraw == true))
{
quickDraw = false;
timer = millis();
moveSnake();
addSnake();
}
// IF GAME IS OVER
else if (gameLoop == false)
{
squareColor = color(64, 64, 64);
squareColor.setAlpha(200);
fill(squareColor);
square(0, 0, 1000);
fill(0, 0, 0);
textSize(32); textStyle(BOLD); textAlign(CENTER);
var textToDisplay = "Your score was: " + score + "\nPress space to play again.";
text(textToDisplay, canvasSize[0] * 0.5, canvasSize[1] * 0.5);
}
}