-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridSnake.html
More file actions
239 lines (228 loc) · 7.88 KB
/
gridSnake.html
File metadata and controls
239 lines (228 loc) · 7.88 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
229
230
231
232
233
234
235
236
237
238
239
<html>
<head>
<script src="p5/p5.min.js"></script>
<script>
var gridX = [];
var gridY = [];
var arrX = [];
var arrY = [];
var gridSize = 12;
var cellCountX = 50;
var cellCountY = 50;
var w = (gridSize * cellCountX) + 1;
var h = (gridSize * cellCountY) + 1;
var i = 0;
var p = 0;
var n = 0;
var magicalCenterer = Math.ceil(gridSize / 4);
var snakeX = 1;
var snakeY = 1;
var snakeDirection = 2; //0 left, 1 up, 2 right, 3 down
var snakeSize = gridSize / 2;
var snakeBodyAmt = 1;
var snakeBodyX = [1];
var snakeBodyY = [1];
var trailX = [];
var trailY = [];
var moveInt = setInterval(snakeInterval, 80);
var eatX = [];
var eatY = [];
var eatSize = gridSize / 2;
var eatAmt = 3;
var actSwitch = false;
var gameOver = false;
var points = 0;
var eats = new eats();
var snake = new snake();
function setup() {
createCanvas(w, h);
}
function draw() {
background(220);
for (i; i < h - 1; i += gridSize) { //setup position of grid
gridY.push(i);
for (var ii = 0; ii < w; ii += gridSize) {
gridX.push(ii);
}
}
for (p; p < cellCountX; p++) { //coordinates X
arrX.push(gridX[p]);
}
for (n; n < cellCountY; n++) { //coordinates Y
arrY.push(gridY[n]);
}
if (trailX.length > snakeBodyAmt + 1) {
trailX.pop();
}
if (trailY.length > snakeBodyAmt + 1) {
trailY.pop();
}
if (actSwitch === true && snakeDirection !== -1) {
document.getElementById('points').style.visibility = "visible";
eats.activate();
snake.move();
snake.head();
snake.body();
} else if (actSwitch === false && gameOver === true) {
document.getElementById('gameOverTitle').style.visibility = "visible";
document.getElementById('gameOverBttn').style.visibility = "visible";
}
for (var y = 0; y < cellCountY; y++) { //show grid
for (var x = 0; x < cellCountX; x++) {
stroke(200);
strokeWeight(1);
noFill();
rect(arrX[x], arrY[y], gridSize, gridSize);
}
}
document.getElementById('points').style.left = w + 25;
document.getElementById('points').innerHTML = "Points: " + points;
}
function start() {
actSwitch = true;
document.getElementById("startTitle").style.visibility = "hidden";
document.getElementById("startBttn").style.visibility = "hidden";
}
function restart() {
location.href = "gridSnake.html";
}
function snake() {
this.move = function () {
this.x = arrX[snakeX] + magicalCenterer;
this.y = arrY[snakeY] + magicalCenterer;
if (snakeX >= 0 || snakeX <= cellCountX) {
if (keyIsDown(65) && snakeDirection !== 2) { //left
snakeDirection = 0;
}
if (keyIsDown(68) && snakeDirection !== 0) { //right
snakeDirection = 2;
}
}
if (snakeY >= 0 && snakeY <= cellCountY) {
if (keyIsDown(87) && snakeDirection !== 3) { //up
snakeDirection = 1;
}
if (keyIsDown(83) && snakeDirection !== 1) { //down
snakeDirection = 3;
}
}
if (snakeY < 0 || snakeY > cellCountY || snakeX < 0 || snakeX > cellCountX) {
snakeDirection = -1;
actSwitch = false;
gameOver = true;
}
}
this.head = function () {
stroke(0);
strokeWeight(1);
fill(20, 255, 20);
rect(this.x, this.y, snakeSize, snakeSize);
};
this.body = function () {
stroke(0);
strokeWeight(1);
fill(50, 150, 50);
for (var num = 0; num < snakeBodyAmt; num++) {
var x = arrX[snakeBodyX[num]] + magicalCenterer;
var y = arrY[snakeBodyY[num]] + magicalCenterer;
if (snake.x === x && snake.y === y && num > 0) {
actSwitch = false;
gameOver = true;
}
rect(x, y, snakeSize, snakeSize);
}
};
}
function eats() {
this.activate = function() {
if (eatX.length < eatAmt) {
var x = Math.floor(Math.random() * cellCountX);
eatX.push(x);
}
if (eatY.length < eatAmt) {
var y = Math.floor(Math.random() * cellCountY);
eatY.push(y);
}
for (var l = 0; l < eatAmt; l++) {
this.x = arrX[eatX[l]] + magicalCenterer;
this.y = arrY[eatY[l]] + magicalCenterer;
if (this.x === snake.x && this.y === snake.y) {
eatX.splice(l, 1);
eatY.splice(l, 1);
points++;
snakeBodyAmt++;
} else {
stroke(0);
strokeWeight(1);
fill(255,255,20);
ellipseMode(CORNER);
ellipse(this.x, this.y, eatSize);
ellipseMode(CENTER);
}
}
};
}
function snakeInterval() {
if (snakeDirection === 0) { //left
snakeX--;
}
if (snakeDirection === 1) { //up
snakeY--;
}
if (snakeDirection === 2) { //right
snakeX++;
}
if (snakeDirection === 3) { //down
snakeY++;
}
for (var iv = 0; iv < snakeBodyAmt; iv++) {
if (iv === 0) {
trailX.unshift(snakeX);
trailY.unshift(snakeY);
}
snakeBodyX[iv] = trailX[iv + 1];
snakeBodyY[iv] = trailY[iv + 1];
}
}
</script>
<style>
body {
overflow: hidden;
}
canvas {
position: absolute;
left: 20px;
top: 20px;
z-index: -1;
}
#points {
position: absolute;
user-select: none;
visibility: hidden;
}
.title {
position: absolute;
left: 32px;
top: 30px;
}
.bttnS {
position: absolute;
left: 32px;
top: 93px
}
#gameOverTitle {
visibility: hidden;
}
#gameOverBttn {
visibility: hidden;
}
</style>
</head>
<body>
<h1 id="points"></h1>
<h1 class="title" id="startTitle">Grid Snake</h1>
<button class="bttnS" onclick="start()" id="startBttn">Start Game</button>
<h1 class="title" id="gameOverTitle">You hit your head</h1>
<button class="bttnS" onclick="restart()" id="gameOverBttn">Restart Game</button>
</body>
</html>