-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
190 lines (171 loc) · 3.95 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
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("canvas");
const ctx = canvas.getContext("2d");
const img = new Image();
img.src = "./assets/flappy-bird-set.png";
// general settings
let gamePlaying = false;
const gravity = 0.5;
const speed = 6.2;
const size = [51, 36];
const jump = -11.5;
const cTenth = canvas.width / 10;
let index = 0,
bestScore = 0,
flight,
flyHeight,
currentScore,
lastScore = 0,
pipe;
// pipe settings
const pipeWidth = 78;
const pipeGap = 270;
const pipeLoc = () =>
Math.random() * (canvas.height - (pipeGap + pipeWidth) - pipeWidth) +
pipeWidth;
const setup = () => {
currentScore = 0;
flight = jump;
// set initial flyHeight (middle of screen - size of the bird)
flyHeight = canvas.height / 2 - size[1] / 2;
// setup first 3 pipes
pipes = Array(3)
.fill()
.map((a, i) => [canvas.width + i * (pipeGap + pipeWidth), pipeLoc()]);
};
const render = () => {
// make the pipe and bird moving
index++;
// ctx.clearRect(0, 0, canvas.width, canvas.height);
// background first part
ctx.drawImage(
img,
0,
0,
canvas.width,
canvas.height,
-((index * (speed / 2)) % canvas.width) + canvas.width,
0,
canvas.width,
canvas.height
);
// background second part
ctx.drawImage(
img,
0,
0,
canvas.width,
canvas.height,
-(index * (speed / 2)) % canvas.width,
0,
canvas.width,
canvas.height
);
// pipe display
if (gamePlaying) {
pipes.map((pipe) => {
// pipe moving
pipe[0] -= speed;
// top pipe
ctx.drawImage(
img,
432,
588 - pipe[1],
pipeWidth,
pipe[1],
pipe[0],
0,
pipeWidth,
pipe[1]
);
// bottom pipe
ctx.drawImage(
img,
432 + pipeWidth,
108,
pipeWidth,
canvas.height - pipe[1] + pipeGap,
pipe[0],
pipe[1] + pipeGap,
pipeWidth,
canvas.height - pipe[1] + pipeGap
);
// give 1 point & create new pipe
if (pipe[0] <= -pipeWidth) {
currentScore++;
// check if it's the best score
bestScore = Math.max(bestScore, currentScore);
// remove & create new pipe
pipes = [
...pipes.slice(1),
[pipes[pipes.length - 1][0] + pipeGap + pipeWidth, pipeLoc()],
];
}
// if hit the pipe, end
if (
[
pipe[0] <= cTenth + size[0],
pipe[0] + pipeWidth >= cTenth,
pipe[1] > flyHeight || pipe[1] + pipeGap < flyHeight + size[1],
].every((elem) => elem)
) {
gamePlaying = false;
lastScore = currentScore;
setup();
}
});
}
// draw bird
if (gamePlaying) {
ctx.drawImage(
img,
432,
Math.floor((index % 9) / 3) * size[1],
...size,
cTenth,
flyHeight,
...size
);
flight += gravity;
flyHeight = Math.min(flyHeight + flight, canvas.height - size[1]);
} else {
ctx.drawImage(
img,
432,
Math.floor((index % 9) / 3) * size[1],
...size,
canvas.width / 2 - size[0] / 2,
flyHeight,
...size
);
flyHeight = canvas.height / 2 - size[1] / 2;
// text accueil
ctx.fillText(`Best score : ${bestScore}`, 85, 245);
ctx.fillText(`Last score : ${lastScore}`, 85, 200);
ctx.fillText("Click to play", 90, 535);
ctx.font = "bold 30px courier";
}
document.getElementById("bestScore").innerHTML = `Best : ${bestScore}`;
document.getElementById(
"currentScore"
).innerHTML = `Current : ${currentScore}`;
// tell the browser to perform anim
window.requestAnimationFrame(render);
};
// launch setup
setup();
img.onload = render;
// start game
document.addEventListener("click", () => {
if (!gamePlaying) {
gamePlaying = true;
}
flight = jump;
});
document.addEventListener("keydown", (event) => {
if (event.code === "Space") {
if (!gamePlaying) {
gamePlaying = true;
}
flight = jump;
}
});