aboutsummaryrefslogtreecommitdiff
path: root/src/html/snake.html
blob: d6fa542fa7579b9880443fd1773a98bfdaa580e5 (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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!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>
    <canvas id="canvas"></canvas>
    <script>
        var gameInterval;
        var canvas;
        var ctx;

        var tileCountWidth;
        var tileCountHeight;
        var tileWidth;
        var tileHeight;
        var vx;
        var vy;
        var score;
        var t;
        var apple;
        var isGameOver;
        var mouseX;
        var mouseY;
        var length;

        function init() {
            tileCountWidth = 40
            tileCountHeight = 40
            tileWidth = 10;
            tileHeight = 10;
            snake = { body: [] };
            length = 5;
            setRandomCoords(snake);
            apple = {};
            setRandomCoords(apple);
            vx = 1;
            vy = 0;
            score = 0;
            isGameOver = false;
            mouseX = 0;
            mouseY = 0;
            t = 0;
        }

        window.onload = function () {
            canvas = document.getElementById("canvas");
            ctx = canvas.getContext("2d");

            window.addEventListener('resize', resizeCanvas, false);
            window.addEventListener('orientationchange', resizeCanvas, false);
            resizeCanvas();

            document.addEventListener("keydown", keyPush);
            document.addEventListener("mousedown", mousePush);
            gameInterval = setInterval(game, 1000 / 10);

            init();
        }

        function resizeCanvas() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }
        function game() {
            update();
            draw();
        }
        function draw() {
            color("black");
//            ctx.fillRect(0, 0, canvas.width, canvas.height);
            ctx.fillRect(0, 0, 400,400);
            color("white");
            ctx.fillRect(0, 400, 400, 410);
            for (var i = 0; i < snake.body.length; i++) {
                ctx.fillRect(snake.body[i].x * tileWidth, snake.body[i].y * tileHeight, tileWidth, tileHeight);
            }
            color("red");
            ctx.fillRect(apple.x * tileWidth, apple.y * tileHeight, tileWidth, tileHeight);

            font(20);
            color("black");
            ctx.fillText("Score: " + score, 20, 450);
            // ctx.fillText(mouseX + " " + mouseY, 20, 500);

            color("orange");
            // ctx.fillRect(210, 410, 390, 590)
            ctx.beginPath();
            ctx.arc(300, 500, 100, 0, 2 * Math.PI);
            ctx.stroke();
            color("black");
            ctx.beginPath();
            ctx.moveTo(210, 410);
            ctx.lineTo(390, 590);
            ctx.moveTo(390, 410);
            ctx.lineTo(210, 590);
            ctx.stroke();

            if (isGameOver) {
                color("red");
                font(20);
                ctx.fillText("Game over!", 160, 200);
                font(14);
                ctx.fillText("(tap to restart)", 152, 220);
            }
        }

        function update() {
            t++;
            snake.x += vx;
            snake.y += vy;
            if (snake.x < 0 || snake.x >= tileCountWidth || snake.y < 0 || snake.y >= tileCountHeight)
                gameOver();

            // if(snake.some((piece) => {return piece.x = x && piece.y == y;})){
            //     gameOver();
            // }

            snake.body.push({ "x": snake.x, "y": snake.y });
            if (snake.x == apple.x && snake.y == apple.y) {
                length++;
                setRandomCoords(apple);
                score += 10 + max(50 - t, 0)
                t = 0;
            }

            while (snake.body.length > length) {
                snake.body.shift();
            }
        }
        function max(a, b) {
            return a > b ? a : b;
        }
        function keyPush(e) {
            console.log(vx, vy);
            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;
            }
        }
        function mousePush(e) {
            if(isGameOver){
                init();
                return;
            }

            mouseX = e.clientX - 300;
            mouseY = e.clientY - 500;
            var d = canvas.getBoundingClientRect();
            mouseX -= d.left;
            mouseY -= d.top;

            if (Math.sqrt(mouseX * mouseX + mouseY * mouseY) > 100) {
                return;
            }

            if (Math.abs(mouseX) > Math.abs(mouseY)) {
                if (mouseX < 0) {
                    keyPush({keyCode:  37})
                } else {
                    keyPush({keyCode:  39})
                }
            } else {
                if (mouseY < 0) {
                    keyPush({keyCode:  38})
                } else {
                    keyPush({keyCode:  40})
                }
            }
        }
        function setRandomCoords(item) {
            item.x = randomInt(tileCountWidth);
            item.y = randomInt(tileCountHeight);
        }
        function randomInt(max) {
            return Math.floor(Math.random() * max);
        }
        function font(size) {
            ctx.font = size + "px Courier";
        }
        function color(c) {
            ctx.fillStyle = c;
        }
        function gameOver() {
            isGameOver = true;
            clearInterval(gameInterval);
        }
    </script>
</body>

</html>