-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.html
140 lines (127 loc) · 3.91 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake</title>
</head>
<body>
<h1>Testing</h1>
<canvas id="canvas" width="1100" height="1000"></canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var canvasContext = canvas.getContext("2d");
//Game Grid Options - Adjust these to change size of Game Tiles
var gridSizeX = 40;
var gridSizeY = 40;
var cellSize = 20;
var offset = cellSize * 2;
//Game State Variables - Dont adjust these
var foodPosition = {x: 0,y: 0};
var snake = [];
var playing = false;
var speedX = -1;
var speedY = 0;
var tickRate = 5;
//Difficulty Options
var tickRateNormal = 5;
var tickRatePressed = 10;
//initialize Game
getNewFoodPosition();
resetSnake();
redrawSnake();
addEventListener("keydown", event => {
if(!playing)
{
resetSnake();
gameLoop();
playing = true;
}
if(event.key === 'ArrowUp') {
changeSpeed(0,-1);
}
if(event.key === 'ArrowDown') {
changeSpeed(0,1);
}
if(event.key === 'ArrowLeft') {
changeSpeed(-1,0);
}
if(event.key === 'ArrowRight') {
changeSpeed(1,0);
}
});
function changeSpeed(x,y) {
speedX = x;
speedY = y;
tickRate = tickRatePressed;
}
addEventListener("keyup", event => {
console.log(event.key);
switch (event.key) {
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
tickRate = tickRateNormal;
}
});
async function gameLoop() {
while(isAlive()) {
await new Promise(r => setTimeout(r, 1000 / tickRate));
move(speedX,speedY);
}
canvasContext.font = '50px serif';
canvasContext.strokeText("Youre Dead",offset + (gridSizeX * cellSize / 2),offset + (gridSizeY * cellSize / 2));
playing = false;
}
function getNewFoodPosition() {
foodPosition = {x: Math.floor(Math.random() * gridSizeX), y: Math.floor(Math.random() * gridSizeY)}
}
function resetSnake() {
for(let i = 0; i< 4;i++) {
snake[i] = {x: gridSizeX / 2 + i,y: gridSizeY / 2}
}
}
function move(x,y) {
snake.unshift({x: snake[0].x + x,y: snake[0].y + y});
if(!(snake[0].x === foodPosition.x && snake[0].y === foodPosition.y)) {
snake.pop();
} else {
getNewFoodPosition();
}
redrawSnake();
}
function redrawSnake() {
canvasContext.clearRect(0,0,canvas.width,canvas.height);
canvasContext.strokeStyle = "black";
canvasContext.strokeRect(offset, offset, gridSizeX * cellSize, gridSizeY * cellSize);
canvasContext.fillStyle = "green";
let foodX = foodPosition.x * cellSize + offset + cellSize / 4;
let foody = foodPosition.y * cellSize + offset + cellSize / 4;
canvasContext.fillRect(foodX,foody,cellSize / 2,cellSize / 2);
drawSnakePart(snake[0].x, snake[0].y,"red");
for (let i = 1; i < snake.length; i++) {
drawSnakePart(snake[i].x, snake[i].y,"blue");
}
}
function drawSnakePart(x,y,color) {
canvasContext.fillStyle = color;
canvasContext.fillRect(x * cellSize + offset,y * cellSize + offset,cellSize,cellSize)
}
function isAlive() {
let alive = true;
if(snake[0].x >= gridSizeX || snake[0].x < 0 || snake[0].y >= gridSizeY || snake[0].y < 0) {
alive = false;
}
if(alive) {
for (let i = 1; i < snake.length; i++) {
if(snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
alive = false;
break;
}
}
}
return alive;
}
</script>
</html>