-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (82 loc) · 3.38 KB
/
app.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
import Bird from "./modules/bird.js";
import TowPipes from "./modules/twoPipes.js";
import InputHandler from "./modules/input.js";
import Score from "./modules/score.js";
import Ground from "./modules/ground.js";
import Collision from './modules/collisionDetection.js';
import GameState from './modules/gameState.js';
import AudioHandler from "./modules/audio.js";
let allpipes = [];
let time = 0;
const soundUrl = "https://raw.githubusercontent.com/avshalom-mogos/flappyBird-js/master/assets/audio";
const wingSound = new AudioHandler(`${soundUrl}/wing.wav`);
const hitSound = new AudioHandler(`${soundUrl}/hit.wav`);
const dieSound = new AudioHandler(`${soundUrl}/die.wav`);
const pointSound = new AudioHandler(`${soundUrl}/point.ogg`);
const canvas = document.querySelector("#gameScreen");
const ctx = canvas.getContext("2d");
canvas.width = document.body.offsetWidth > 500 ? 500 : document.body.offsetWidth;
canvas.height = document.body.offsetHeight
const GAME_WIDTH = canvas.offsetWidth;
const GAME_HEIGHT = canvas.offsetHeight;
const numOfPipes = 100;
const spaceBetweenPipes = GAME_WIDTH / 1.8;
// ========================================================================== Init the game
// the state of game
const gameState = new GameState(GAME_WIDTH, GAME_HEIGHT);
//create bird
const bird = new Bird(GAME_WIDTH, GAME_HEIGHT, time, wingSound);
//create score
const score = new Score(GAME_WIDTH, pointSound);
//create ground
const ground = new Ground(GAME_WIDTH, GAME_HEIGHT);
// collision handler
const collision = new Collision(hitSound, dieSound);
//handle input from user
new InputHandler(bird, gameState, canvas, reset);
//create new pipes
for (let index = 0; index < numOfPipes; index++) {
allpipes.push(new TowPipes(bird, GAME_WIDTH + (index * spaceBetweenPipes), GAME_HEIGHT, ground));
}
//========================================================================== End
//=================================================================== Reset the game
function reset() {
collision.reset();
gameState.reset();
score.reset();
bird.reset();
allpipes = [];
for (let index = 0; index < numOfPipes; index++) {
allpipes.push(new TowPipes(bird, GAME_WIDTH + (index * spaceBetweenPipes), GAME_HEIGHT, ground));
}
}
//=================================================================== End
//=================================================================== Game loop
function gameLoop() {
//clear canvas before next draw
ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
const gameIsRunning = gameState.game.currentState === gameState.game.running;
const gameIsOver = gameState.game.currentState === gameState.game.over;
//bird
bird.draw(ctx, time, gameState);
time++;
allpipes.forEach(pairOfPipes => {
score.update(bird, pairOfPipes, gameState);
pairOfPipes.draw(ctx);
if (gameIsRunning) pairOfPipes.update();
});
if (gameIsRunning) bird.update();
//stop ground on collision
if (!gameIsOver) ground.update();
gameState.draw(ctx)
//ground
ground.draw(ctx);
if (gameIsRunning && collision.detectCollision(bird, allpipes, ground)) {
//end game on collision
gameState.game.currentState = gameState.game.over;
};
score.draw(ctx, gameState);
requestAnimationFrame(gameLoop);
};
gameLoop();
//=================================================================== End