-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.html
192 lines (163 loc) · 4.57 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
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
<html>
<head><title>Snake game</title>
<style>
section {
display: grid;
grid-template-columns: repeat(24, 12px);
grid-template-rows: repeat(24, 12px);
}
div {
width: 10px;
height: 10px;
background: lightgrey;
}
div.black {
background: black;
}
button {margin: 10px;}
</style>
</head>
<body>
<button>Start</button>
<span id="msg">Snake!</span>
<section>
<!-- 24x24 empty div elements will be added here. -->
</section>
<p>Keys: w, a, s, d.</p>
<script>
// Most of this code is inspired by this excellent FORTH tutorial.
// https://skilldrick.github.io/easyforth/
// Create the HTML elmements, these never change, only their CSS class changes.
let sectionEl = document.querySelector("section");
for (let i=0; i<24*24; i++) {
sectionEl.appendChild(document.createElement('div'));
}
// Board is 24x24 matrix of pixels that start false
let board = [];
function initializeBoard() {
board = [];
for (let i=0; i<24*24; i++) {
board.push(true);
}
}
function render() {
let pixels = document.querySelectorAll('div');
let i = 0;
for (pixel of pixels) {
pixel.className = board[i] ? 'white' : 'black';
i++;
}
}
function drawWhite(x, y) {
let index = y * 24 + x;
board[index] = true;
}
function drawBlack(x, y) {
let index = y * 24 + x;
board[index] = false;
}
function drawWalls() {
for (let x=0; x<24; x++) {
drawBlack(x, 0);
drawBlack(x, 23);
}
for (let y=0; y<24; y++) {
drawBlack(0, y);
drawBlack(23, y);
}
}
let snake = [];
function initializeSnake() {
direction = 's';
snake = [[12, 4], // snakes head
[11, 4],
[10, 4],
[9, 4],
[8, 4]]; // last segement is white
}
function drawSnake(){
for (let segment of snake) {
drawBlack(segment[0], segment[1]);
}
let whiteSegment = snake[snake.length - 1];
drawWhite(whiteSegment[0], whiteSegment[1]);
}
// Direction the head is moving: w, a, s, d.
let direction = "s";
// This inserts a new segment at the start of the snake to represent the new position of the head. To keep the snake the same length, the last segment is removed.
function moveSnakeHeadAndTail() {
let newHead = [snake[0][0], snake[0][1]];
if (direction == "w") newHead[1]--;
if (direction == "s") newHead[1]++;
if (direction == "a") newHead[0]--;
if (direction == "d") newHead[0]++;
snake.unshift(newHead); // adds to front
snake.pop(); // removes last item
}
// Given the user's input, change the direction of the snake, if it is legal.
function maybeChangeDirection(key) {
if (key == "w" && (direction == "a" || direction == "d")) direction = key;
if (key == "s" && (direction == "a" || direction == "d")) direction = key;
if (key == "a" && (direction == "w" || direction == "s")) direction = key;
if (key == "d" && (direction == "w" || direction == "s")) direction = key;
}
var lastKey = null;
document.addEventListener(
'keydown', (e) => {lastKey = e.key;});
// Return a random in between min and max-1.
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
let appleX = null;
let appleY = null;
function moveApple() {
// Remove the eaten apple otherwise it will count as a collision;
drawWhite(appleX, appleY);
appleX = getRndInteger(1, 23);
appleY = getRndInteger(1, 23);
}
function drawApple() {
drawBlack(appleX, appleY);
}
function checkApple() {
if (appleX == snake[0][0] &&
appleY == snake[0][1]) {
// Grow by copying the last segement
snake.push(snake[snake.length - 1]);
moveApple();
}
}
let gameLoopIntervalId = null;
function start() {
initializeBoard();
drawWalls();
initializeSnake();
moveApple();
gameLoopIntervalId = setInterval(gameLoop, 700);
document.querySelector('#msg').innerText = 'Snake!';
}
// If the snake head hits any black pixel, it is game over.
// This is called after the apple has alredy been checked.
function endGameIfCollision(){
let snakeHeadX = snake[0][0];
let snakeHeadY = snake[0][1];
let index = snakeHeadY * 24 + snakeHeadX;
if (board[index] == false) gameOver();
}
function gameLoop() {
maybeChangeDirection(lastKey);
moveSnakeHeadAndTail();
checkApple();
endGameIfCollision();
drawSnake();
drawApple();
render();
}
function gameOver(){
clearInterval(gameLoopIntervalId);
document.querySelector('#msg').innerText = 'Game over. Length: ' + (snake.length - 1);
}
document.querySelector('button').addEventListener('click', (e) => {gameOver(); start();});
</script>
</body>
</html>