aboutsummaryrefslogtreecommitdiff
path: root/src/snake/index.html
blob: ea982cf05fc96f10d140e8c3d263c6412957f7ea (plain)
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
<!doctype html>
<html lang="en">

<head>
    <title>Snake</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- <link rel="stylesheet" type="text/css" href="/css/styles.css"> -->
</head>

<body style="padding:0; margin:0; overflow:hidden;">
    <canvas width="400" height="400" id="canvas"></canvas>
    <script src="/highscore.js"></script>
    <script>
        var tileCountWidth = 40;
        var tileCountHeight = 40;
        var tileWidth = canvas.width / tileCountWidth;
        var tileHeight = canvas.height / tileCountHeight;
        var x, y;
        var vx, vy;
        var snake, apple;
        var isGameOver;
        var length, gameInterval, score;
        var username = undefined
        var inputThisFrame = false
        window.onload = function () {
            canvas = document.getElementById("canvas");
            ctx = canvas.getContext("2d");
            document.addEventListener("keydown", keyPush);
            init();
        }
        function init() {
            snake = [];
            x = randomInt(10);
            y = randomInt(40);
            vx = 1;
            vy = 0;
            apple = {};
            isGameOver = false;
            setRandomCoords(apple);
            length = 5;
            score = 0
            if(gameInterval != undefined){
                clearInterval(gameInterval)
            }
            gameInterval = setInterval(game, 1000 / 10);
        }
        function game() {
            inputThisFrame = false
            // DRAW
            color("black");
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            color("white");
            for (var i = 0; i < snake.length; i++) {
                ctx.fillRect(snake[i].x * tileWidth, snake[i].y * tileHeight, tileWidth, tileHeight);
            }
            color("red");
            ctx.fillRect(apple.x * tileWidth, apple.y * tileHeight, tileWidth, tileHeight);
            if (isGameOver) {
                color("blue");
                font(32);
                ctx.fillText("Game over!", 160, 200);
                font(24);
                ctx.fillText("(press any key)", 160, 240)
                return // don't update
            }

            // UPDATE
            x += vx;
            y += vy;
            for (var i = 0; i < snake.length; i++) {
                if(snake[i].x == x && snake[i].y == y){
                    gameOver()
                }
            }
            snake.push({ "x": x, "y": y });
            if (x < 0 || y < 0 || x >= 40 || y >= 40) {
                gameOver();
            } else if (x == apple.x && y == apple.y) {
                score++;
                length++;
                setRandomCoords(apple);
            }

            while (snake.length > length) {
                snake.shift();
            }

        }
        function keyPush(e) {
            if (isGameOver) {
                init()
                return
            }
            if (inputThisFrame) {
                return
            }

            switch (e.keyCode) {
                case 37:
                    if (vx == 1) return
                    vx = -1;
                    vy = 0;
                    break;
                case 38:
                    if (vy == 1) return
                    vx = 0;
                    vy = -1;
                    break;
                case 39:
                    if (vx == -1) return
                    vx = 1;
                    vy = 0;
                    break;
                case 40:
                    if (vy == -1) return
                    vx = 0;
                    vy = 1;
                    break;
            }
            inputThisFrame = true
        }
        function gameOver() {
            isGameOver = true;
            // clearInterval(gameInterval);
            username = submitScore("snake", score, username);
        }
        function setRandomCoords(item) {
            item.x = randomInt(40);
            item.y = randomInt(40);
        }
        function randomInt(max) {
            return Math.floor(Math.random() * max);
        }
        function font(size) {
            ctx.font = size + "px Courier";
        }
        function color(c) {
            ctx.fillStyle = c;
        }
    </script>
</body>

</html>