-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (58 loc) · 1.66 KB
/
app.js
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
document.addEventListener('DOMContentLoaded', () => {
const bird = document.querySelector('.bird');
const gameDisplay = document.querySelector('.game-container');
const ground = document.querySelector('.ground');
let birdLeft = 220;
let birdBottom = 100;
let gravity = 2;
let isGameOver = false;
function startGame(){
if(birdBottom > 0){birdBottom -= gravity;}
bird.style.bottom = birdBottom + 'px';
bird.style.left = birdLeft + 'px';
}
let timerId = setInterval(startGame, 20);
function control(e) {
if(e.keyCode === 38){
jump();
}
}
function jump(){
if(birdBottom < 500){ birdBottom += 50;}
bird.style.bottom = birdBottom + 'px';
console.log(birdBottom);
}
document.addEventListener('keyup', control);
function generateObstacle(){
let obstacleLeft = 500;
let randomHeight = Math.random() * 60;
let obstacleBottom = randomHeight;;
const obstacle = document.createElement('div');
if(!isGameOver) obstacle.classList.add('obstacle');
gameDisplay.appendChild(obstacle);
obstacle.style.left = obstacleLeft + 'px';
obstacle.style.bottom = obstacleBottom + 'px';
function moveObstacle(){
obstacleLeft -=2;
obstacle.style.left = obstacleLeft + 'px';
if(obstacleLeft === -60){
clearInterval(timed);
gameDisplay.removeChild(obstacle);
}
if (
obstacleLeft > 200 && obstacleLeft < 280 && birdLeft === 220 && birdBottom < obstacleBottom + 160 ||
birdBottom === 0) {
gameOver();
clearInterval(timed);
}
console.log('Game over');
let timed = setInterval(moveObstacle, 20);
if(!isGameOver) setTimeout(generateObstacle, 3000);
}
generateObstacle();
function gameOver(){
clearInterval(timerId);
isGameOver = true;
document.removeEventListener('keyup', control);
}
});