-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
149 lines (133 loc) · 4.17 KB
/
snake.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
<html>
<head>
<script type="text/javascript">
var canvas;
var ctx;
var dimension = {w:25, h:25}; //dimension of boxes
var grid = {w:21, h:15}; //dimension of grid
var padding = 3; //padding between boxes on grid
var direction = {x:1, y:0};
var speed = 75; //ms
var snake = [{x:0,y:0},{x:1,y:0},{x:2,y:0,xv:1,yv:0}];
var foodColor = "red";
var snakeColor = "black";
var font = "50px serif";
var intervalId;
function turnGridOn(x, y) {
ctx.fillRect(padding+x*(dimension.w+padding), padding+y*(dimension.h+padding), dimension.w, dimension.h);
}
function turnGridOff(x, y) {
ctx.clearRect(padding+x*(dimension.w+padding), padding+y*(dimension.h+padding), dimension.w, dimension.h);
}
function clear() {
ctx.clearRect(padding,padding,canvas.width-2*padding,canvas.height-2*padding);
}
function pause() {
if(intervalId>0) {
clearInterval(intervalId);
intervalId = 0;
clear();
ctx.fillText("Paused",(canvas.width-150)/2,canvas.height/2);
} else if(intervalId==0) {
clear();
ctx.fillStyle = foodColor;
turnGridOn(foodPos.x,foodPos.y);
ctx.fillStyle = snakeColor;
for(let snakeBody of snake)
turnGridOn(snakeBody.x,snakeBody.y);
intervalId = setInterval(updateCanvas, speed);
}
}
function lose() {
alert(`You lost.\nScore: ${snake.length-3}`);
clearInterval(intervalId);
intervalId = -1;
reload();
}
function reload() {
clearInterval(intervalId);
clear();
snake = [{x:0,y:0},{x:1,y:0},{x:2,y:0,xv:1,yv:0}];
direction = {x:1, y:0};
placeFood();
ctx.fillStyle = foodColor;
turnGridOn(foodPos.x,foodPos.y);
ctx.fillStyle = snakeColor;
intervalId = setInterval(updateCanvas, speed);
}
var foodPos = {x:-1, y:-1};
function placeFood() {
let index = [];
for(let i=0; i<grid.w*grid.h; i++)
index[i]=i;
for(let snakeBody of snake)
index = index.filter(xy => xy != snakeBody.y*grid.w + snakeBody.x); //remove snake's body from list
let foodPosXY = index[Math.floor(Math.random()*index.length)];
foodPos.x = foodPosXY % grid.w;
foodPos.y = Math.floor(foodPosXY / grid.w);
if(index.length == 0) {
alert("I guess you win...!");
clearInterval(intervalId);
intervalId = -1;
}
}
function isSnakeCoord(x,y) {
for(let snakeBody of snake)
if(snakeBody.x == x && snakeBody.y == y)
return true;
return false;
}
function updateCanvas() {
snakeHeadId = snake.length-1;
var newSnakeHead = {x:snake[snakeHeadId].x+direction.x, y:snake[snakeHeadId].y+direction.y, xv:direction.x, yv:direction.y};
if(isSnakeCoord(newSnakeHead.x, newSnakeHead.y) || newSnakeHead.x < 0 || newSnakeHead.y < 0 || newSnakeHead.x >= grid.w || newSnakeHead.y >= grid.h) {
lose();
return;
}
if(newSnakeHead.x == foodPos.x && newSnakeHead.y == foodPos.y) {
snake.push(newSnakeHead);
turnGridOn(newSnakeHead.x, newSnakeHead.y);
placeFood();
ctx.fillStyle = foodColor;
turnGridOn(foodPos.x,foodPos.y);
ctx.fillStyle = snakeColor;
} else {
turnGridOff(snake[0].x, snake[0].y);
for(var i=0; i<snakeHeadId; i++)
snake[i] = snake[i+1];
snake[snakeHeadId] = newSnakeHead;
turnGridOn(snake[snakeHeadId].x, snake[snakeHeadId].y);
}
}
onload = function() {
canvas = document.getElementById("canvas");
canvas.width = grid.w*(dimension.w+padding)+padding;
canvas.height = grid.h*(dimension.h+padding)+padding;
ctx = canvas.getContext('2d');
ctx.font = font;
ctx.strokeRect(0,0,canvas.width,canvas.height);
for(let snakeBody of snake)
turnGridOn(snakeBody.x, snakeBody.y);
document.body.addEventListener("keydown",
function(e) {
let key = e.key;
if(key == "ArrowLeft" && !snake[snake.length-1].xv) direction = {x:-1, y:0};
if(key == "ArrowRight" && !snake[snake.length-1].xv) direction = {x:1, y:0};
if(key == "ArrowUp" && !snake[snake.length-1].yv) direction = {x:0, y:-1};
if(key == "ArrowDown" && !snake[snake.length-1].yv) direction = {x:0, y:1};
if(key == "p" || e.key == " ") pause();
if(key == "r") reload();
}
);
intervalId = setInterval(updateCanvas, speed);
placeFood();
ctx.fillStyle = foodColor;
turnGridOn(foodPos.x,foodPos.y);
ctx.fillStyle = snakeColor;
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>