-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (117 loc) · 4.2 KB
/
index.js
File metadata and controls
132 lines (117 loc) · 4.2 KB
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
const container = document.getElementById('container');
container.style.position = 'relative';
container.style.width = '100%';
container.style.height = '100vh';
container.style.overflow = 'hidden';
container.style.backgroundColor = '#f0f0f0';
const centerPosition = (container.clientWidth / 2) - 25;
const line = document.createElement('div');
line.style.width = '100%';
line.style.height = '2px';
line.style.backgroundColor = 'black';
line.style.position = 'absolute';
line.style.top = '50%';
line.style.left = '0';
const dino = document.createElement('img');
dino.src = '/img/dino.png';
dino.style.position = 'absolute';
dino.style.width = '50px';
dino.style.height = '50px';
dino.style.left = '50px'
dino.style.top = 'calc(50% - 50px)';
const kaktus = document.createElement('img');
kaktus.src = '/img/kaktus.png';
kaktus.style.position = 'absolute';
kaktus.style.width = '50px';
kaktus.style.height = '50px';
kaktus.style.left = '150%';
kaktus.style.top = 'calc(50% - 50px)';
const kaktus2 = document.createElement('img');
kaktus2.src = '/img/kaktus.png';
kaktus2.style.position = 'absolute';
kaktus2.style.width = '50px';
kaktus2.style.height = '50px';
kaktus2.style.left = '150%';
kaktus2.style.top = 'calc(50% - 50px)';
const kaktus3 = document.createElement('img');
kaktus3.src = '/img/kaktus.png';
kaktus3.style.position = 'absolute';
kaktus3.style.width = '50px';
kaktus3.style.height = '50px';
kaktus3.style.left = '150%';
kaktus3.style.top = 'calc(50% - 50px)';
container.appendChild(line);
container.appendChild(dino);
container.appendChild(kaktus);
container.appendChild(kaktus2);
container.appendChild(kaktus3);
let position = 50;
let speed = 1;
let kaktusSpeed = 1;
let kaktusPositions = [
container.clientWidth + Math.random() * 500 + 100,
container.clientWidth + Math.random() * 500 + 300,
container.clientWidth + Math.random() * 500 + 500
];
let kaktusElements = [kaktus, kaktus2, kaktus3];
function moveDino() {
if (position >= container.clientWidth - 50) {
position = container.clientWidth - 50;
speed = 0;
} else if (position >= centerPosition) {
position = centerPosition;
speed = 0;
}
position += speed;
dino.style.left = position + 'px';
setTimeout(moveDino, 10);
}
function jumpDino() {
console.log("🦖 Прыжок!");
dino.style.top = 'calc(50% - 100px)';
setTimeout(() => {
console.log("⬇️ Динозавр приземлился");
dino.style.top = 'calc(50% - 50px)';
}, 800);
}
function kaktusMove() {
for (let i = 0; i < kaktusElements.length; i++) {
if (kaktusPositions[i] <= -50) {
kaktusPositions[i] = container.clientWidth + Math.random() * 800 + 100;
console.log(`🌵 Кактус ${i + 1} появился снова на расстоянии ${Math.round(kaktusPositions[i] - container.clientWidth)} пикселей`);
}
kaktusPositions[i] -= kaktusSpeed;
kaktusElements[i].style.left = kaktusPositions[i] + 'px';
}
setTimeout(kaktusMove, 10);
}
function kasaniya() {
for (let i = 0; i < kaktusPositions.length; i++) {
const distance = Math.abs(position - kaktusPositions[i]);
if (distance < 10 && dino.style.top == 'calc(50% - 50px)') {
speed = 0;
kaktusSpeed = 0;
console.log(`💥 Динозавр столкнулся с кактусом ${i + 1}! Игра остановлена!`);
break;
}
}
}
console.log("🎮 Игра началась! Динозавр бежит автоматически");
moveDino();
kaktusMove();
setInterval(kasaniya, 1);
document.addEventListener('keydown', (event) => {
if (event.code === 'ArrowUp') {
console.log("⬆️ Нажата стрелка вверх — ручной прыжок");
jumpDino()
} else if (event.code === 'ArrowRight') {
console.log("➡️ Нажата стрелка вправо — движение вперёд");
moveDino();
kaktusMove()
setInterval(kasaniya, 1)
}
});
document.addEventListener('touchstart', (event) => {
console.log("⬆️ Нажата стрелка вверх — ручной прыжок");
jumpDino()
});