Whtmlets/js2020/Jakhangirov/index.html — различия между версиями
Материал из Department of Theoretical and Applied Mechanics
м |
(→Код программы) |
||
(не показаны 4 промежуточные версии 2 участников) | |||
Строка 10: | Строка 10: | ||
{{#widget:Iframe |url=http://tm.spbstu.ru/htmlets/js2020/Jakhangirov/index.html |width=800 |height=650 |border=0 }} | {{#widget:Iframe |url=http://tm.spbstu.ru/htmlets/js2020/Jakhangirov/index.html |width=800 |height=650 |border=0 }} | ||
− | ==Код программы | + | ==Код программы== |
<div class="mw-collapsible mw-collapsed"> | <div class="mw-collapsible mw-collapsed"> | ||
− | '''Код программы | + | '''Код программы на языке JavaScript:''' <div class="mw-collapsible-content"> |
<syntaxhighlight lang="javascript" line start="1" enclose="div"> | <syntaxhighlight lang="javascript" line start="1" enclose="div"> | ||
− | + | const canvas = document.getElementById("game"); | |
− | + | const ctx = canvas.getContext("2d"); | |
− | |||
− | |||
− | |||
− | + | const ground = new Image(); | |
− | + | ground.src = "img/ground.png"; | |
− | |||
− | |||
− | + | const foodImg = new Image(); | |
− | + | foodImg.src = "img/food.png"; | |
− | + | ||
− | + | let box = 32; | |
− | |||
− | |||
− | + | let score = 0; | |
− | |||
− | + | let food = { | |
− | + | x: Math.floor((Math.random() * 17 + 1)) * box, | |
− | + | y: Math.floor((Math.random() * 15 + 3)) * box, | |
− | + | }; | |
− | |||
− | + | let snake = []; | |
− | + | snake[0] = { | |
− | + | x: 9 * box, | |
− | + | y: 10 * box | |
− | + | }; | |
− | |||
− | + | document.addEventListener("keydown", direction); | |
− | |||
− | + | let dir; | |
− | |||
− | + | function direction(event) { | |
− | + | if(event.keyCode == 37 && dir != "right") | |
− | + | dir = "left"; | |
− | + | else if(event.keyCode == 38 && dir != "down") | |
− | + | dir = "up"; | |
+ | else if(event.keyCode == 39 && dir != "left") | ||
+ | dir = "right"; | ||
+ | else if(event.keyCode == 40 && dir != "up") | ||
+ | dir = "down"; | ||
} | } | ||
− | function | + | function eatTail(head, arr) { |
− | + | for(let i = 0; i < arr.length; i++) { | |
− | + | if(head.x == arr[i].x && head.y == arr[i].y) | |
+ | clearInterval(game); | ||
} | } | ||
− | |||
} | } | ||
− | function | + | function drawGame() { |
− | + | ctx.drawImage(ground, 0, 0); | |
− | + | ||
− | + | ctx.drawImage(foodImg, food.x, food.y); | |
− | + | ||
− | + | for(let i = 0; i < snake.length; i++) { | |
− | + | ctx.fillStyle = i == 0 ? "blue" : "orange"; | |
− | + | ctx.fillRect(snake[i].x, snake[i].y, box, box); | |
} | } | ||
− | + | ||
− | + | ctx.fillStyle = "black"; | |
− | + | ctx.font = "50px Times New Roman"; | |
− | + | ctx.fillText(score, box * 2.5, box * 1.7); | |
− | + | ||
− | + | let snakeX = snake[0].x; | |
− | + | let snakeY = snake[0].y; | |
+ | |||
+ | if(snakeX == food.x && snakeY == food.y) { | ||
+ | score++; | ||
+ | food = { | ||
+ | x: Math.floor((Math.random() * 17 + 1)) * box, | ||
+ | y: Math.floor((Math.random() * 15 + 3)) * box, | ||
+ | }; | ||
+ | } else { | ||
+ | snake.pop(); | ||
} | } | ||
+ | |||
+ | if(snakeX < box || snakeX > box * 17 | ||
+ | || snakeY < 3 * box || snakeY > box * 17) | ||
+ | clearInterval(game); | ||
+ | |||
+ | if(dir == "left") snakeX -= box; | ||
+ | if(dir == "right") snakeX += box; | ||
+ | if(dir == "up") snakeY -= box; | ||
+ | if(dir == "down") snakeY += box; | ||
+ | |||
+ | let newHead = { | ||
+ | x: snakeX, | ||
+ | y: snakeY | ||
+ | }; | ||
+ | |||
+ | eatTail(newHead, snake); | ||
+ | |||
+ | snake.unshift(newHead); | ||
} | } | ||
− | + | let game = setInterval(drawGame, 100); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | </div> |
Текущая версия на 03:21, 2 августа 2020
Описание[править]
Генератор лабиринта на JavaScript
Исполнитель: Джахангиров Мансур
Группа 3630103/90001 Кафедра Теоретической механики
Визуализация[править]
Код программы[править]
Код программы на языке JavaScript:
1 const canvas = document.getElementById("game");
2 const ctx = canvas.getContext("2d");
3
4 const ground = new Image();
5 ground.src = "img/ground.png";
6
7 const foodImg = new Image();
8 foodImg.src = "img/food.png";
9
10 let box = 32;
11
12 let score = 0;
13
14 let food = {
15 x: Math.floor((Math.random() * 17 + 1)) * box,
16 y: Math.floor((Math.random() * 15 + 3)) * box,
17 };
18
19 let snake = [];
20 snake[0] = {
21 x: 9 * box,
22 y: 10 * box
23 };
24
25 document.addEventListener("keydown", direction);
26
27 let dir;
28
29 function direction(event) {
30 if(event.keyCode == 37 && dir != "right")
31 dir = "left";
32 else if(event.keyCode == 38 && dir != "down")
33 dir = "up";
34 else if(event.keyCode == 39 && dir != "left")
35 dir = "right";
36 else if(event.keyCode == 40 && dir != "up")
37 dir = "down";
38 }
39
40 function eatTail(head, arr) {
41 for(let i = 0; i < arr.length; i++) {
42 if(head.x == arr[i].x && head.y == arr[i].y)
43 clearInterval(game);
44 }
45 }
46
47 function drawGame() {
48 ctx.drawImage(ground, 0, 0);
49
50 ctx.drawImage(foodImg, food.x, food.y);
51
52 for(let i = 0; i < snake.length; i++) {
53 ctx.fillStyle = i == 0 ? "blue" : "orange";
54 ctx.fillRect(snake[i].x, snake[i].y, box, box);
55 }
56
57 ctx.fillStyle = "black";
58 ctx.font = "50px Times New Roman";
59 ctx.fillText(score, box * 2.5, box * 1.7);
60
61 let snakeX = snake[0].x;
62 let snakeY = snake[0].y;
63
64 if(snakeX == food.x && snakeY == food.y) {
65 score++;
66 food = {
67 x: Math.floor((Math.random() * 17 + 1)) * box,
68 y: Math.floor((Math.random() * 15 + 3)) * box,
69 };
70 } else {
71 snake.pop();
72 }
73
74 if(snakeX < box || snakeX > box * 17
75 || snakeY < 3 * box || snakeY > box * 17)
76 clearInterval(game);
77
78 if(dir == "left") snakeX -= box;
79 if(dir == "right") snakeX += box;
80 if(dir == "up") snakeY -= box;
81 if(dir == "down") snakeY += box;
82
83 let newHead = {
84 x: snakeX,
85 y: snakeY
86 };
87
88 eatTail(newHead, snake);
89
90 snake.unshift(newHead);
91 }
92
93 let game = setInterval(drawGame, 100);
94
95
96
97
98
99
100 //