-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (77 loc) · 2.81 KB
/
script.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
document.addEventListener('DOMContentLoaded', ()=> {
const bird = document.querySelector('.bird');
const display = document.querySelector('.game-container');
const land = document.querySelector('.land');
let birdBottom = 250;
let birdLeft = 200;
let gravity = 4;
let gameOverIndicator = false;
function startGame(){
birdBottom -= gravity;
bird.style.left = birdLeft +'px';
bird.style.bottom = birdBottom +'px';
if(birdBottom < 0 ){
gameOverIndicator = true;
gameOver();
}
bird.classList.add('down');
bird.classList.remove('up');
}
let gameTimer = setInterval(startGame, 20);
function pressButton(e){
if(e.keyCode === 32){
jump();
}
}
function jump(){
birdBottom += 50;
bird.style.bottom = birdBottom +'px';
bird.classList.add('up');
bird.classList.remove('down');
}
document.addEventListener('keyup',pressButton);
function makeObstacles(){
let ostacleLeft = 500;
let randomHeight = Math.random()*70;
let obstacleBottom = randomHeight;
let topObstacleBottom = obstacleBottom +450;
const obstacle = document.createElement('div');
const topObstacle = document.createElement('div');
if(!gameOverIndicator){
obstacle.classList.add('obstacle');
topObstacle.classList.add('top-obstacle');
}
display.appendChild(obstacle);
display.appendChild(topObstacle);
obstacle.style.left = ostacleLeft +'px';
topObstacle.style.left = ostacleLeft +'px';
obstacle.style.bottom = obstacleBottom +'px';
topObstacle.style.bottom = topObstacleBottom +'px';
function moveObstacle(){
ostacleLeft -= 2;
obstacle.style.left = ostacleLeft +'px';
topObstacle.style.left = ostacleLeft +'px';
if(ostacleLeft === -60){
clearInterval(obstacleMoveTimer);
display.removeChild(obstacle);
display.removeChild(topObstacle);
}
if(
ostacleLeft > 200 && ostacleLeft <260 && birdLeft === 200 &&
(birdBottom < obstacleBottom +150 || birdBottom >obstacleBottom + 450 - 210)||birdBottom === 0
){
gameOverIndicator = true;
gameOver();
clearInterval(obstacleMoveTimer);
}
}
let obstacleMoveTimer = setInterval(moveObstacle,20);
if(!gameOverIndicator) setTimeout(makeObstacles,3000);
}
makeObstacles();
function gameOver(){
clearInterval(gameTimer);
gameOverIndicator = true;
document.removeEventListener('keyup', pressButton);
}
});