-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy.js
226 lines (186 loc) · 5.8 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const blockContainer=document.getElementById("block-container");
const canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
const block= document.getElementById("block");
const scoreBoard = document.getElementById("score");
var gameState = false;
var x = 0,
y = 0;
var gap = 110;
const highScoreBoard= document.getElementById("highScoreBoard");
var highScore=0
const initialBirdPos = 150;
class Obstruction {
// A class to create the obstruction of the game- poles
constructor(x, y, gap) {
//The pole needs to have a x coordinate to move on the X axis
this.width = 100;
this.x = x;
this.y = y;
this.gap = gap;
}
up_y_start = 0; // Y axis coordinate where the upper pole will start
up_y_end = y - gap / 2; // Y axis coordinate where the pole will end
// Fomula used to account for the gap between the poles
down_y_start = y + gap / 2; // Y axis coordinate where the lower pole will start
down_y_end = canvas.height - 100; // Y axis coordinate where the lower pole will end
speed = 5;
generate(world) {
if (gameState) {
var move = setInterval(() => {
check();
if (!gameState) {clearInterval(move); blockContainer.appendChild(block);
} // ending a game when its over
//code that clears they bitmap
ctx.clearRect(
this.x,
this.down_y_start,
this.width,
this.down_y_end - this.down_y_start
);
ctx.clearRect(this.x, this.up_y_start, this.width, this.up_y_end);
//code to change the x coordinate of the poles, so that it moves.
this.x -= this.speed;
//code to redraw the pole with changed coordinate
ctx.fillStyle = "green";
ctx.fillRect(this.x, this.up_y_start, this.width, this.up_y_end);
ctx.fillRect(
this.x,
this.down_y_start,
this.width,
this.down_y_end - this.down_y_start
);
//code to generate another pole after a pole has crossed the bird
if (this.x == 490) {
world.generate_poles(world);
}
//code to clear the setInterval() from the object(pole) that has left the screen
if (this.x <= -this.width) {
clearInterval(move);
}
}, 24);
}
}
}
class World {
pole = null;
generate_ground() {
const floorHeight = 100;
var ground = setInterval(function () {
if (!gameState) clearInterval(ground); // ending a game when its over
ctx.fillStyle = "brown";
ctx.fillRect(0, canvas.height - floorHeight, canvas.width, floorHeight);
}, 24);
}
generate_poles(currentWorld) {
x = 950; //keeps distance between objects
y = 80 + Math.floor(Math.random() * 240); //randomly generates where the midpoint of gap will lie
this.pole = new Obstruction(x, y, gap);
this.pole.generate(currentWorld); //generates a pole with random gap location
}
generate_world(currentWorld) {
if (gameState) {
this.generate_ground();
this.generate_poles(currentWorld); // generate poles for current world
}
}
}
class Bird {
constructor() {
this.currentX = 530;
this.currentY = initialBirdPos;
this.targetX = 0;
this.targetY = 400;
this.check = false;
//this.gameOn = false;
}
render() {
if (gameState) {
this.targetY = 400; // where the birds want to go
let dy = 2; //speed of bird
let move = setInterval(() => {
if (!gameState) clearInterval(move);
//code to check if the bird has reached the target
if (
this.currentY == this.targetY ||
(this.currentY <= this.targetY && dy < 0)
) {
clearInterval(move);
this.render();
}
//clear the bitmap for the bird motion
ctx.clearRect(this.currentX, this.currentY, 40, 40);
//code to check if a bird wants to go up or down
if (this.currentY > this.targetY && this.check) {
dy = -2;
this.check = false;
}
this.currentY += dy; // changes the Y coordinate of the bird
ctx.fillStyle = "orange";
ctx.fillRect(this.currentX, this.currentY, 40, 40);
}, 6.5);
}
}
space() {
// function that makes the bird fly when the space key is pressed
this.targetY = this.currentY - 50;
this.check = true;
}
}
var world = new World();
var bird = new Bird();
document.addEventListener("keydown", keyDownTextField, false); //listens to the pressed key
function keyDownTextField(e) {
// executes command based on the key pressed
var keyCode = e.keyCode;
if (keyCode == 32) {
//Space bar pressed
bird.space();
} else if (keyCode == 13) {
//Enter key pressed
gameState = !gameState;
main();
} else console.log("Enter valid key");
}
function main() {
if (gameState) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
bird.currentY = initialBirdPos;
bird.render();
world.generate_world(world);
block.remove();
check();
set_score();
}
else{
blockContainer.appendChild(block);
}
}
function check() {
let poleXStart = world.pole.x - 10;
let poleXEnd = poleXStart + world.pole.width + 10;
let poleUpYEnd = world.pole.up_y_end;
let poleDownYStart = world.pole.down_y_start;
let birdX = bird.currentX;
let birdUpperY = bird.currentY;
let birdLowerY = bird.currentY + 40;
if (
(birdX >= poleXStart &&
birdX <= poleXEnd &&
(birdLowerY >= poleDownYStart || birdUpperY <= poleUpYEnd)) ||
birdLowerY >= 420
) {
gameState = false;
}
}
function set_score() {
let scoreRate = 20;
let score = 0;
let scoreCounter = setInterval(function () {
score++;
scoreBoard.innerText = score;
highScore= Math.max(highScore,score);
highScoreBoard.innerText= highScore;
if (!gameState) clearInterval(scoreCounter);
}, scoreRate);
}