-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnuzaq.js
123 lines (116 loc) · 3.22 KB
/
nuzaq.js
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
function gameInit() {
if( typeof document !== "undefined") {
return (function() {
_gameInit()
})()
}
}
function _gameInit() {
let _gameOver = false;
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
let width = 13;
let height = 9;
let snakeHeadX = 0;
let snakeHeadY = 0;
let direction = "ArrowRight";
let snakeLength = 3;
let snake = [];
let snakeTail = [];
let appleX;
let appleY;
const appleFill = "#00ff";
const emptyFill = "#ddd";
const snakeFill = "#000";
let interval = 150;
function drawCell(x, y, fill) {
ctx.beginPath();
ctx.rect(35 * x + 20,35 * y + 20,25,25);
ctx.fillStyle = fill;
ctx.fill();
ctx.closePath();
}
function drawApple() {
appleX = Math.floor(Math.random() * width);
appleY = Math.floor(Math.random() * height);
snake.forEach((snakeCell) => {
if(snakeCell[0] == appleX && snakeCell[1] == appleY) {
drawApple();
}
});
drawCell(appleX, appleY, appleFill)
}
function drawBoard() {
for (let j = 0; j < height; j++) {
for (let i = 0; i < width; i++) {
drawCell(i, j, emptyFill)
}
}
}
function init() {
drawBoard();
drawApple();
}
init();
function drawGame() {
moveSnake();
if(isSnakeOnBoard() && !isSnakeBitingItself()) {
drawCell(snakeHeadX, snakeHeadY, snakeFill)
if (snake.length > snakeLength) {
snakeTail = snake.shift();
drawCell(snakeTail[0], snakeTail[1], emptyFill)
}
if (snakeHeadX == appleX && snakeHeadY == appleY) {
snakeLength++;
drawApple();
}
}
}
const directions = {
"ArrowUp": [0,-1],
"ArrowDown": [0,1],
"ArrowLeft": [-1,0],
"ArrowRight": [1,0]
}
function setDirection(e) {
if (e.code.startsWith("Arrow")) {
direction = e.code;
}
}
function moveSnake() {
snake.push([snakeHeadX, snakeHeadY]);
snakeHeadX = snakeHeadX + directions[direction][0];
snakeHeadY = snakeHeadY + directions[direction][1];
}
function isSnakeOnBoard() {
if (
snakeHeadX < 0 ||
snakeHeadX > width - 1 ||
snakeHeadY < 0 ||
snakeHeadY > height - 1
) {
gameOver("The Snake went off the board!");
return false;
}
return true
}
function isSnakeBitingItself() {
snake.forEach((snakeCell) => {
if (snakeCell[0] == snakeHeadX && snakeCell[1] == snakeHeadY) {
gameOver("The Snake bit itself!");
return true;
}
});
return false;
}
function gameOver(issue) {
if (_gameOver !== true) {
clearInterval(drawInterval);
_gameOver = true;
alert('GAME OVER: ' + issue);
}
}
let drawInterval = setInterval(drawGame, interval);
document.addEventListener('keyup', setDirection);
}
export default gameInit;