-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
272 lines (267 loc) · 8.61 KB
/
index.html
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<html>
<head>
<title>snake</title>
<script>
let MODE_CLASSIC = 1;
let MODE_TWIST = 2;
let SCREEN_INTRO = 1;
let SCREEN_GAME = 2;
function Color(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
this.toString = function () {
return "rgb(" + this.r + "," + this.g + "," + this.b + ")";
}
}
function randInt(min, max) {
return (Math.random() * (max-min) + min) | 0;
}
function randomColor() {
return new Color(randInt(0,255), randInt(0,255), randInt(0,255));
}
function randomPastelColor() {
return new Color(randInt(200,255), randInt(200,255), randInt(200,255));
}
function Position(x,y) {
this.x = x;
this.y = y;
}
var LEFT = new Position(-1, 0);
var RIGHT = new Position(1, 0);
var UP = new Position(0, -1);
var DOWN = new Position(0, 1);
function directionToString(dir) {
if (positionEqual(dir, LEFT)) {
return "left";
} else if (positionEqual(dir, RIGHT)) {
return "right";
} else if (positionEqual(dir, UP)) {
return "up";
} else if (positionEqual(dir, DOWN)) {
return "down";
}
}
function Snake(state) {
this.positions = [new Position(state.boardWidth/2, state.boardHeight/2)];
this.positions.push(new Position(this.positions[0].x, this.positions[0].y + 1));
}
function oppositeDirection(dir) {
if (positionEqual(dir, LEFT)) {
return RIGHT;
} else if (positionEqual(dir, RIGHT)) {
return LEFT;
} else if (positionEqual(dir, UP)) {
return DOWN;
} else if (positionEqual(dir, DOWN)) {
return UP;
}
return LEFT;
}
function positionAdd(a, b) {
return new Position(a.x + b.x, a.y + b.y);
}
function positionEqual(a, b) {
return a.x == b.x && a.y == b.y;
}
function isOccupied(state, position) {
var snakePos = state.snake.positions;
for (var i = 0; i < snakePos.length; i++) {
if (positionEqual(snakePos[i], position)) {
return true;
}
}
if (positionEqual(state.fruit, position)) {
return true;
}
return false;
}
function randomPosition(state, position) {
return new Position(randInt(0, state.boardWidth), randInt(0, state.boardHeight));
}
function randomEmptyPosition(state) {
var pos = randomPosition(state);
while(isOccupied(state, pos)) {
pos = randomPosition(state);
}
return pos;
}
function setState(state) {
var CANVAS_WIDTH = 480;
var CANVAS_HEIGHT = CANVAS_WIDTH;
var BOARD_WIDTH = 40;
var BOARD_HEIGHT = 40;
state.canvasWidth = CANVAS_WIDTH;
state.canvasHeight = CANVAS_HEIGHT;
state.boardWidth = BOARD_WIDTH;
state.boardHeight = BOARD_HEIGHT;
state.squareSize = CANVAS_WIDTH/BOARD_WIDTH;
state.timeOfLastStep = 0;
state.lost = false;
state.screen = SCREEN_INTRO;
state.mode = MODE_TWIST;
state.backgroundColor = randomPastelColor().toString();
state.snake = new Snake(state);
state.nextDirection = RIGHT;
state.fruit = state.snake.positions[0];
state.fruit = randomEmptyPosition(state);
console.log("createdState");
return state;
}
function drawAtPosition(ctx, state, pos) {
ctx.fillRect(pos.x * state.squareSize, pos.y * state.squareSize,
state.squareSize, state.squareSize);
}
function draw(ctx, state) {
ctx.clearRect(0, 0, state.canvasWidth, state.canvasHeight);
if (state.lost) {
ctx.fillStyle = "black";
ctx.font = "30px Consolas";
ctx.textAlign = "center";
ctx.fillText("Uh oh!",
state.canvasWidth/2,state.canvasHeight/2);
ctx.fillText("Press any key to play again.",
state.canvasWidth/2,state.canvasHeight/2 + 30);
return;
}
// background
ctx.fillStyle = state.backgroundColor;
ctx.fillRect(0, 0, state.canvasWidth, state.canvasHeight);
// fruit
ctx.fillStyle = "red";
drawAtPosition(ctx, state, state.fruit);
// snake
ctx.fillStyle = "black";
var pos = state.snake.positions;
for (var i = 0; i < pos.length; i++) {
drawAtPosition(ctx, state, pos[i]);
}
}
function lost(state) {
var pos = state.snake.positions;
var head = pos[0];
// self-collide
for (var i = 1; i < pos.length; i++) {
if (positionEqual(head, pos[i])) {
return true;
}
}
// out of bounds
if (head.x < 0 || head.x >= state.boardWidth ||
head.y < 0 || head.y >= state.boardHeight) {
return true;
}
return false;
}
function tryEatFruit(state) {
var pos = state.snake.positions;
let nextPos = positionAdd(pos[0], state.snake.direction);
if (positionEqual(nextPos, state.fruit)) {
// eat fruit
pos.unshift(nextPos);
// set up new fruit
state.fruit = randomEmptyPosition(state);
state.backgroundColor = randomPastelColor().toString();
return true;
}
return false;
}
function moveSnakeForward(state) {
var pos = state.snake.positions;
// takes tail and puts it in front of head
pos.pop();
let newHead = positionAdd(pos[0], state.snake.direction);
pos.unshift(newHead);
console.log("new head");
console.log(newHead);
if (lost(state)) {
state.lost = true;
}
}
function stepTwist(state) {
if (state.lost) {
return;
}
if (tryEatFruit(state)) {
var pos = state.snake.positions;
state.snake.positions.reverse();
state.snake.direction = oppositeDirection(state.snake.direction);
state.nextDirection = oppositeDirection(state.nextDirection);
let nextPos = positionAdd(pos[0], state.snake.direction);
for (let i = 0; i < pos.length; i++) {
if (positionEqual(pos[i], nextPos)) {
state.snake.direction = oppositeDirection(state.snake.direction);
state.nextDirection = oppositeDirection(state.nextDirection);
break;
}
}
}
moveSnakeForward(state);
}
function stepClassic(state) {
tryEatFruit(state);
moveSnakeForward(state);
}
function step(state) {
if (state.lost) {
return;
}
state.snake.direction = state.nextDirection;
if (state.mode == MODE_CLASSIC) {
stepClassic(state);
} else if (state.mode == MODE_TWIST) {
stepTwist(state);
}
}
function init() {
var canvas = document.getElementById('main_canvas');
var ctx = canvas.getContext('2d');
var state = {};
var state = setState(state);
console.log(state);
document.addEventListener('keydown', (event) => {
if (state.lost) {
setState(state);
}
const keyName = event.key;
if (keyName == "ArrowLeft") {
if (!positionEqual(state.snake.direction, RIGHT)) {
state.nextDirection = LEFT;
}
event.preventDefault();
} else if (keyName == "ArrowRight") {
if (!positionEqual(state.snake.direction, LEFT)) {
state.nextDirection = RIGHT;
}
event.preventDefault();
} else if (keyName == "ArrowUp") {
if (!positionEqual(state.snake.direction, DOWN)) {
state.nextDirection = UP;
}
event.preventDefault();
} else if (keyName == "ArrowDown") {
if (!positionEqual(state.snake.direction, UP)) {
state.nextDirection = DOWN;
}
event.preventDefault();
}
})
animate(ctx, state);
};
function animate(ctx, state) {
window.requestAnimationFrame(function (ts) {
if (ts - state.timeOfLastStep > 100) {
step(state);
state.timeOfLastStep = ts;
draw(ctx, state);
}
animate(ctx, state);
});
}
</script>
</head>
<body onload="init();">
<canvas id="main_canvas" width="500" height="500">
</canvas>
</body>
</html>