-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
141 lines (137 loc) · 3.76 KB
/
app.js
File metadata and controls
141 lines (137 loc) · 3.76 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
132
133
134
135
136
137
138
139
140
141
const grid = document.querySelector(".grid");
const bird = document.createElement("div");
let birdLeftSpace = 100;
let birdBottomSpace = 250;
let isGameOver = false;
let gridWidth = 800;
let gridHeight = 500;
let pipeWidth = 10;
let pipeVerticalGap = 150;
let pipeCount = 2;
let pipes = [];
let gravity = 3;
let birdFallId;
let platMoveId;
let isFalling = false;
let isJumping = false;
function createBird() {
grid.appendChild(bird);
bird.classList.add("bird");
bird.style.left = birdLeftSpace + "px";
bird.style.bottom = birdBottomSpace + "px";
}
class Pipe {
constructor(newPipeLeft) {
this.gap = pipeVerticalGap;
this.left = newPipeLeft;
this.topPipeHeight = Math.random() * (gridHeight - pipeVerticalGap);
this.bottomPipeHeight = gridHeight - this.topPipeHeight - this.gap;
this.topPipe = document.createElement("div");
this.bottomPipe = document.createElement("div");
const visualOne = this.topPipe;
const visualTwo = this.bottomPipe;
visualOne.classList.add("pipe");
visualOne.classList.add("topPipe");
visualTwo.classList.add("Bottompipe");
visualTwo.classList.add("pipe");
visualOne.style.left = this.left + "px";
visualTwo.style.left = this.left + "px";
visualOne.style.height = this.topPipeHeight + "px";
visualTwo.style.height = this.bottomPipeHeight + "px";
visualTwo.style.bottom = 0 + "px";
visualOne.style.top = 0 + "px";
grid.appendChild(visualOne);
grid.appendChild(visualTwo);
}
}
function createPipes() {
for (let i = 0; i < pipeCount; i++) {
let pipeHoriGap = gridWidth / pipeCount;
let newPipeLeft = 300 + i * pipeHoriGap;
let newpipe = new Pipe(newPipeLeft);
pipes.push(newpipe);
}
}
function movePipes() {
// console.log(pipes);
pipes.forEach((pipe) => {
pipe.left -= 2;
let visualOne = pipe.topPipe;
let visualTwo = pipe.bottomPipe;
visualOne.style.left = pipe.left + "px";
visualTwo.style.left = pipe.left + "px";
if (pipe.left <= 0) {
let firstpipeVisualone = pipes[0].topPipe;
let firstpipeVisualtwo = pipes[0].bottomPipe;
firstpipeVisualone.classList.remove("pipe");
firstpipeVisualtwo.classList.remove("pipe");
pipes.shift();
let newPipe = new Pipe(gridWidth - 60);
pipes.push(newPipe);
}
});
}
function birdJump() {
isJumping = true;
isFalling = false;
birdBottomSpace += 50;
bird.style.bottom = birdBottomSpace + "px";
}
function birdFall() {
isFalling = true;
isJumping = false;
birdFallId = setInterval(() => {
isFalling = true;
console.log(birdBottomSpace);
console.log(birdLeftSpace);
if (birdTouchesceil()) {
console.log("touches ceil");
die();
} else if (birdTouchesGround()) {
console.log("touches ground");
die();
} else if (birdTouchesPipe()) {
console.log("touches pipe");
die();
}
birdBottomSpace -= gravity;
bird.style.bottom = birdBottomSpace + "px";
}, 30);
}
function control(e) {
if (isGameOver) return;
if (e.key === "ArrowUp") {
birdJump();
}
}
function birdTouchesGround() {
return birdBottomSpace <= 0;
}
function birdTouchesceil() {
return birdBottomSpace >= gridHeight - 20;
}
function birdTouchesPipe() {
return pipes.some(
(pipe) =>
birdLeftSpace + 20 >= pipe.left &&
birdLeftSpace <= pipe.left + pipeWidth &&
(birdBottomSpace <= pipe.bottomPipeHeight ||
birdBottomSpace + 20 >= gridHeight - pipe.topPipeHeight)
);
}
function die() {
clearInterval(birdFallId);
clearInterval(platMoveId);
isGameOver = true;
document.removeEventListener("keyup", control);
}
function start() {
if (!isGameOver) {
createBird();
createPipes();
platMoveId = setInterval(movePipes, 30);
document.addEventListener("keyup", control);
birdFall();
}
}
start();