-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
executable file
·228 lines (212 loc) · 6.56 KB
/
game.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
227
228
// loading canvas
var canvas = document.getElementById('Canvas');
var scene = canvas.getContext('2d');
scene.font = "20pt Arial";
// control
var KEY = {UP: 38, DOWN: 40, ESC: 27};
var pressed = {};
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
function onKeyDown(event){
pressed[event.keyCode] = true;
event.preventDefault();
}
function onKeyUp(event){
pressed[event.keyCode] = false;
event.preventDefault();
}
// loading sprites
var images = {};
images.BG = new Image();
images.BG.src = 'assets/background.png';
images.airplane = new Image();
images.airplane.src = 'assets/airplane2.png';
images.cloud1 = new Image();
images.cloud1.src = 'assets/heavy_cloud.png';
images.cloud2 = new Image();
images.cloud2.src = 'assets/cloud.png';
images.fire = new Image();
images.fire.src = 'assets/fire.png';
images.road = new Image();
images.road.src = 'assets/road.jpg';
// sound
var audio = {};
audio.engine = new Audio();
audio.engine.src = 'assets/engine.mp3';
audio.burning = new Audio();
audio.burning.preload = 'auto';
audio.burning.src = 'assets/burning.mp3';
// game objects
var airplane = {
image : images.airplane,
x: 0, y: canvas.height/2,
speed: 1,
autoPilot: '', iterations: 0,
status: 'flying',
update : function () { // here we describe the airplane behavior.
// manual fly
if (pressed[KEY.UP] && this.y > 0){
this.y -= 7;
return
} else if (pressed[KEY.DOWN] &&
this.y + this.image.height < canvas.height-100) {
this.y += 7;
return
}
// autopilot
if (this.iterations > 0){
switch (this.autoPilot){
case 'down':
if(this.y < canvas.height-80){ this.y += 7 };
break;
case 'up':
if(this.y > 7){this.y -= 7};
break;
}
this.iterations -= 1;
return
}
// give autopilot task to avoid clouds
if ( this.checkIfTheWayIsBlocked() ) {
if (this.y > canvas.height/4){
this.autoPilot = 'up'
} else {
this.autoPilot = 'down'
}
this.iterations = 40;
}
},
checkIfTheWayIsBlocked : function() {
for (var j in objects){
var obj = objects[j];
if (obj != this && // can't hit myself
obj.x > 0 + this.image.width && // cloud is ahead
obj.x < canvas.width/2 && // cloud is not too far
this.y < obj.y + obj.image.height &&
this.y + this.image.height > obj.y ){ return true }
}
return false
}
};
// this function will replace airplane.update() after hitting the cloud
function landing(){
if (airplane.y + airplane.image.height < canvas.height){
airplane.y += 0.8;
airplane.x += airplane.speed;
} else if (airplane.speed > 0){
audio.burning.pause();
airplane.status = 'landed';
Road.prototype.update = function(){
this.x -= 3;
};
airplane.speed -= 0.01;
airplane.x += airplane.speed;
} else {
gameover = true;
}
}
// cloud constructor
function Cloud( type, x, y ){
this.image = images[type];
this.x = x; this.y = y;
}
Cloud.prototype.update = function(){
this.x -= 6;
};
// road constructor
function Road(x, y){
this.image = images.road;
this.x = x; this.y = y;
}
Road.prototype.update = function () {
this.x -= 6;
};
// preloading game objects
var gameover = false;
var objects = [];
objects.push(airplane);
for (var i = 1; i<15; i++){
objects.push(new Cloud('cloud1', i*500, Math.round(Math.random()*400)));
}
audio.engine.play();
// main game functions
function updateGame(){
for (var i in objects){
objects[i].update();
}
// also we need to check if is it time to finish the game?
// after 14 clouds we need to hit the 15th and land the airplane
var lastCloud = objects[objects.length-1];
if (lastCloud.x + lastCloud.image.width < 0 &&
airplane.status == 'flying' ){ // in that case game is finishing anyway
objects.push(new Cloud('cloud2', canvas.width, airplane.y-50));
// and let's switch off airplane ability to avoid cloud
airplane.update = function(){};
}
}
function renderGame(){
scene.drawImage(images.BG, 0, 0);
for (var i in objects){
scene.drawImage(objects[i].image, objects[i].x, objects[i].y);
}
}
function renderParticles(){
if (airplane.status == 'burning'){
scene.drawImage(images.fire, airplane.x, airplane.y)
}
}
function distance(obj1, obj2){
var x1 = (obj1.x + obj1.image.width/2);
var y1 = (obj1.y + obj1.image.height/2);
var x2 = (obj2.x + obj2.image.width/2);
var y2 = (obj2.y + obj2.image.height/2);
var dx = x1 - x2;
var dy = y1 - y2;
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
}
function collisionDetector(){
if (airplane.status == 'flying') // no sense to check collision - plane already landing
for (var j in objects){
var obj = objects[j];
if (obj == airplane){ continue }
if (distance(airplane, obj) < 70){
// Our plane hits something - now we need to land the airplane
airplane.status = 'burning';
audio.burning.play();
audio.engine.pause();
airplane.update = landing;
Cloud.prototype.update = function(){
this.x -= 6;
// plane goes down - clouds going up!
this.y -= 0.6;
};
for (var i = 1; i<25; i++){
// add landing road:
// do it with unshift, otherwise road will cover the airplane while landing
objects.unshift(
new Road(800 + i*images.road.width, canvas.height - images.road.height)
);
}
}
}
}
function main(){
updateGame();
collisionDetector();
renderGame();
renderParticles();
if (pressed[KEY.ESC]) {
//alert('BYE BYE');
scene.fillText('BYE BYE', canvas.width/2, canvas.height/2);
audio.engine.pause();
audio.burning.pause();
return; // stops the game - we don't call 'requestAnimationFrame'
}
if (gameover){
alert('BE CAREFUL WHILE FLYING');
document.location.reload();
return
}
requestAnimationFrame(main); // continues the game
}
document.onload = requestAnimationFrame(main); //Let's Rock!