-
Notifications
You must be signed in to change notification settings - Fork 13
/
game.js
190 lines (154 loc) · 4.81 KB
/
game.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext("2d");
// we will need the gamecontainer to make it blurry
// when we display the end menu
const gameContainer = document.getElementById('game-container');
const flappyImg = new Image();
flappyImg.src = 'assets/flappy_dunk.png';
//Game constants
const FLAP_SPEED = -3; //Improves the fluidity and accuracy of the bird when moving around the screen
const BIRD_WIDTH = 40;
const BIRD_HEIGHT = 30;
const PIPE_WIDTH = 50;
const PIPE_GAP = 125;
// Bird variables
let birdX = 50;
let birdY = 50;
let birdVelocity = 0;
let birdAcceleration = 0.1;
// Pipe variables
let pipeX = 400;
let pipeY = canvas.height - 200;
// score and highscore variables
let scoreDiv = document.getElementById('score-display');
let score = 0;
let highScore = 0;
// we add a bool variable, so we can check when flappy passes we increase
// the value
let scored = false;
// lets us control the bird with the space key
document.body.onkeyup = function(e) {
if (e.code == 'Space') {
birdVelocity = FLAP_SPEED;
}
}
// lets us restart the game if we hit game-over
document.getElementById('restart-button').addEventListener('click', function() {
hideEndMenu();
resetGame();
loop();
})
function increaseScore() {
// increase now our counter when our flappy passes the pipes
if(birdX > pipeX + PIPE_WIDTH &&
(birdY < pipeY + PIPE_GAP ||
birdY + BIRD_HEIGHT > pipeY + PIPE_GAP) &&
!scored) {
score++;
scoreDiv.innerHTML = score;
scored = true;
}
// reset the flag, if bird passes the pipes
if (birdX < pipeX + PIPE_WIDTH) {
scored = false;
}
}
function collisionCheck() {
// Create bounding Boxes for the bird and the pipes
const birdBox = {
x: birdX,
y: birdY,
width: BIRD_WIDTH,
height: BIRD_HEIGHT
}
const topPipeBox = {
x: pipeX,
y: pipeY - PIPE_GAP + BIRD_HEIGHT,
width: PIPE_WIDTH,
height: pipeY
}
const bottomPipeBox = {
x: pipeX,
y: pipeY + PIPE_GAP + BIRD_HEIGHT,
width: PIPE_WIDTH,
height: canvas.height - pipeY - PIPE_GAP
}
// Check for collision with upper pipe box
if (birdBox.x + birdBox.width > topPipeBox.x &&
birdBox.x < topPipeBox.x + topPipeBox.width &&
birdBox.y < topPipeBox.y) {
return true;
}
// Check for collision with lower pipe box
if (birdBox.x + birdBox.width > bottomPipeBox.x &&
birdBox.x < bottomPipeBox.x + bottomPipeBox.width &&
birdBox.y + birdBox.height > bottomPipeBox.y) {
return true;
}
// check if bird hits boundaries
if (birdY < 0 || birdY + BIRD_HEIGHT > canvas.height) {
return true;
}
return false;
}
function hideEndMenu () {
document.getElementById('end-menu').style.display = 'none';
gameContainer.classList.remove('backdrop-blur');
}
function showEndMenu () {
document.getElementById('end-menu').style.display = 'block';
gameContainer.classList.add('backdrop-blur');
document.getElementById('end-score').innerHTML = score;
// This way we update always our highscore at the end of our game
// if we have a higher high score than the previous
if (highScore < score) {
highScore = score;
}
document.getElementById('best-score').innerHTML = highScore;
}
// we reset the values to the beginning so we start
// with the bird at the beginning
function resetGame() {
birdX = 50;
birdY = 50;
birdVelocity = 0;
birdAcceleration = 0.1;
pipeX = 400;
pipeY = canvas.height - 200;
score = 0;
}
function endGame() {
showEndMenu();
}
function loop() {
// reset the ctx after every loop iteration
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Flappy Bird
ctx.drawImage(flappyImg, birdX, birdY);
// Draw Pipes
ctx.fillStyle = '#333';
ctx.fillRect(pipeX, -50, PIPE_WIDTH, pipeY); // improves the accuracy of the upper column against collision
ctx.fillRect(pipeX, pipeY + PIPE_GAP, PIPE_WIDTH, canvas.height - pipeY);
// now we would need to add an collision check to display our end-menu
// and end the game
// the collisionCheck will return us true if we have a collision
// otherwise false
if (collisionCheck()) {
endGame();
return;
}
// forgot to mvoe the pipes
pipeX -= 1.5;
// if the pipe moves out of the frame we need to reset the pipe
if (pipeX < -50) {
pipeX = 400;
pipeY = Math.random() * (canvas.height - PIPE_GAP) + PIPE_WIDTH;
}
// apply gravity to the bird and let it move
birdVelocity += birdAcceleration;
birdY += birdVelocity;
// always check if you call the function ...
increaseScore()
requestAnimationFrame(loop);
}
loop();