-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappy.js
144 lines (110 loc) · 2.71 KB
/
flappy.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
'use strict'
let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");
canvas.width = 256;
canvas.height = 512;
let bird = new Image();
let back = new Image();
let road = new Image();
let pipeUp = new Image();
let pipeBottom = new Image();
bird.src = "img/bird.png";
back.src = "img/back.png";
road.src = "img/road.png";
pipeUp.src = "img/pipeUp.png";
pipeBottom.src = "img/pipeBottom.png";
let fly_audio = new Audio();
let score_audio = new Audio();
fly_audio.src = "audio/fly.mp3";
score_audio.src = "audio/score.mp3";
let score_text = document.getElementById("score");
let best_score_text = document.getElementById("best_score");
let xPos = 10;
let yPos = 150;
let gravity = 0.2;
let velY = 0;
let gap = 110;
let pipe = [];
pipe[0] = {
x: canvas.width,
y: 0
}
let score = 0;
let best_score = 0;
let pause = false;
function draw() {
if (!pause) {
context.drawImage(back, 0, 0);
context.drawImage(bird, xPos, yPos);
if (yPos + bird.height >= canvas.height - road.height) {
reload();
}
velY += gravity;
yPos += velY;
for (let i = 0; i < pipe.length; i++) {
if (pipe[i].x < -pipeUp.width) {
pipe.shift();
} else {
context.drawImage(pipeUp, pipe[i].x, pipe[i].y);
context.drawImage(pipeBottom, pipe[i].x, pipe[i].y + pipeUp.height + gap);
pipe[i].x -= 2;
if (pipe[i].x == 80) {
pipe.push({
x : canvas.width,
y : Math.floor(Math.random() * pipeUp.height) - pipeUp.height
});
}
}
if (xPos + bird.width >= pipe[i].x &&
xPos <= pipe[i].x + pipeUp.width &&
(yPos <= pipe[i].y + pipeUp.height ||
yPos + bird.height >= pipe[i].y + pipeUp.height + gap)) {
reload();
}
if (pipe[i].x == 0) {
score++;
score_audio.play();
}
}
context.drawImage(road, 0, canvas.height - road.height);
score_text.innerHTML = "SCORE: " + score;
best_score_text.innerHTML = "BEST SCORE: " + best_score;
} else {
context.drawImage(back, 0, 0);
context.drawImage(bird, xPos, yPos);
for (let i = 0; i < pipe.length; i++) {
context.drawImage(pipeUp, pipe[i].x, pipe[i].y);
context.drawImage(pipeBottom, pipe[i].x, pipe[i].y + pipeUp.height + gap);
}
context.drawImage(road, 0, canvas.height - road.height);
context.fillStyle = 'rgba(0, 0, 0, 0.3)';
context.fillRect(0, 0, canvas.width, canvas.height);
}
}
function reload() {
if (score > best_score) {
best_score = score;
}
xPos = 10;
yPos = 150;
velY = 0;
pipe = [];
score = 0;
pipe[0] = {
x: canvas.width,
y: 0
}
}
document.addEventListener("keydown", function(event){
if(event.code == 'ArrowUp'){
moveUp();
}
});
function moveUp() {
velY = -4;
fly_audio.play();
}
function game_pause() {
pause = !pause;
}
setInterval(draw, 20);