-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
71 lines (55 loc) · 1.61 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
<canvas id="gc"></canvas>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="util.js"></script>
<script src="counter.js"></script>
<script src="apple.js"></script>
<script src="snake.js"></script>
<script src="screen.js"></script>
<script src="controller.js"></script>
<script>
var screen;
var snake;
var counter;
var interval;
window.onload=startGame;
function startGame() {
let squares = 40; // number of squares on the grid (horizontal)
let snakeTail = 6; // snake tail size
let pointsPerApple = 10; // points for each apple ate
screen = new Screen(squares);
snake = new Snake(snakeTail, screen.verticalSquares,
screen.horizontalSquares);
apple = new Apple(screen.verticalSquares, screen.horizontalSquares);
counter = new Counter(pointsPerApple);
sortApple();
interval = setInterval(draw,1000/15);
document.addEventListener("keydown",keyPush);
}
function draw() {
snake.crawl();
if (snake.ateOwnTail()) {
gameOver();
}
if (snake.ateApple(apple)) {
snake.growTail();
counter.plusOne();
sortApple();
}
screen.draw(snake, apple, counter);
}
function gameOver() {
clearInterval(interval);
interval = setInterval(()=>screen.blinkSnakeHead(snake), 200);
onAnyKeyPress = function onKeyPress() {
clearInterval(interval);
document.removeEventListener('keydown', onKeyPress);
startGame();
}
document.addEventListener("keydown", onAnyKeyPress);
}
function sortApple() {
do {
apple.sortPosition();
} while (snake.overlapsApple(apple));
}
</script>