-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy.js
209 lines (162 loc) · 4.63 KB
/
my.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var btnPlay = document.getElementById('button');
var label = document.getElementById('label');
var level = document.getElementById('nextLevel');
var buttonAudio = document.getElementById('buttonAudio');
var audio = new Audio();
audio.src = src = "sfx_wing.mp3";
var backgroundHomePage = new Image();
backgroundHomePage.src = "backgroundHomePage.jpg";
backgroundHomePage.addEventListener("load", homePage, false);
var gameWidth = 1500;
var gameHeight = 600;
var birdWidth = 90;
var birdHeight = 60;
pipeWidth = 400;
pipeHeight = 160;
var bX = 10;
var bY = 300;
var gap = 75;
var count = 1;
var gravity = 1.5;
var speed = 2;
var distance = 700;
var score = 0;
var play = true;
var bird = new Image();
bird.src = "birds.png";
var background = new Image();
background.src = "background.png";
var finishBackground = new Image();
finishBackground.src = "finishBackground.png";
var pipeDown = new Image();
pipeDown.src = "pipeDown.png";
var pipeUp = new Image();
pipeUp.src = "pipeUp.png";
var min = (-1 * (gameHeight - pipeHeight - birdHeight - gap));
var max = (pipeHeight);
var pipes = [];
// on key down
document.addEventListener("keydown", moveUp);
function moveUp() {
bY -= 25;
}
pipes[0] = {
x: canvas.width - 700,
y: -pipeHeight,
}
function initGame() {
pipes = [];
pipes[0] = {
x: canvas.width - 700,
y: -pipeHeight,
}
bY = 300;
distance = 700;
score = 0;
buttonAudio.style.visibility = 'visible';
}
// game
function game() {
btnPlay.style.visibility = 'hidden';
ctx.drawImage(background, 0, 0, gameWidth, gameHeight);
ctx.drawImage(bird, bX, bY, birdWidth, birdHeight);
start();
}
function start() {
drawShow();
if (play) {
for (var i = 0; i < pipes.length; i++) {
audio.play();
ctx.drawImage(pipeDown, pipes[i].x, 0, pipeWidth, pipeHeight);
ctx.drawImage(pipeUp, pipes[i].x, gameHeight, pipeWidth, pipes[i].y);
pipes[i].x -= speed;
if (pipes[i].x == distance) {
pipes.push({
x: canvas.width - 200,
y: Math.floor(Math.random() * max) + min
});
}
if (count % 6 == 0) {
nextLevel();
}
if ((bY >= pipes[i].y + gameHeight - 50 || bY <= pipeHeight) && (bX >= pipes[i].x + 93 && bX <= pipes[i].x + 250)) {
play = false;
} else if (bX >= pipes[i].x + 250) {
pipes.shift();
i--;
count++;
score++;
}
}
bY += gravity;
requestAnimationFrame(game);
} else {
finish();
}
}
function finish() {
ctx.clearRect(0, 0, gameWidth, gameHeight);
ctx.drawImage(finishBackground, 0, 0, gameWidth, gameHeight);
level.style.visibility = 'hidden';
buttonAudio.style.visibility = 'hidden';
btnPlay.style.visibility = 'visible';
label.style.visibility = 'visible';
console.log(finishBackground);
btnPlay.innerHTML = "Return To Home Game";
label.innerText = " Your score is: " + score;
}
function homePage() {
ctx.clearRect(0, 0, gameWidth, gameHeight);
level.style.visibility = 'hidden';
label.style.visibility = 'hidden';
buttonAudio.style.visibility = 'hidden';
ctx.drawImage(backgroundHomePage, 0, 0, gameWidth, gameHeight);
// console.log("after");
}
function drawShow() {
ctx.font = "16px Arial";
ctx.fillStyle = "#FFFFE0";
ctx.fillText("Score: " + score, 8, 20);
}
buttonAudio.addEventListener('click', () => {
console.log(audio.muted);
if (!audio.muted) {
buttonAudio.innerHTML = "unmute";
audio.muted = true;
} else {
buttonAudio.innerHTML = "mute";
buttonAudio.border = "red";
audio.muted = false;
}
})
btnPlay.onclick = function() {
console.log(play);
if (play) {
initGame();
ctx.clearRect(0, 0, gameWidth, gameHeight);
game();
} else {
ctx.clearRect(0, 0, gameWidth, gameHeight);
play = true;
btnPlay.innerHTML = "Let's Start the Game";
label.style.visibility = 'hidden';
homePage();
}
}
function nextLevel() {
count = 1;
// let anim = Animation(() => {
setTimeout(() => {
level.style.visibility = 'visible';
level.innerText = " Your level is: " + (score / 5);
console.log("alert!!!");
}, 3000);
level.style.visibility = 'hidden';
if (distance != 1000)
distance += 50;
console.log(speed);
console.log(distance);
}
homePage();