-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
394 lines (364 loc) · 9.92 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
document.addEventListener("DOMContentLoaded", function(event) {
//======================Setting of variables============================
var canvas = document.getElementById("game");
var ctx = canvas.getContext("2d");
var background = {
y : -4000 + canvas.height, //canvas has 400px, background 4000px
minX : 10,
maxX : 690
};
var rocket = {
direction : null,
x : canvas.width/2,//set rocket in the middle
y : canvas.height - 128 - 35,//image height = 128 //land = 35
engine : 0,
speed : 0,
maxSpeed : 300,
turning : 3,
state : 1000
};
var heightRocket;
var gravitation = 3;
var numberObjects = 50;
var randomType;
var types;
//random from m to n -> Math.floor(Math.random() * (n-m+1)) + m
var cloud = {};
for (i=0; i<=numberObjects; i++){
randomType = Math.floor(Math.random() * 3);
types = ["small", "big", "storm"];
cloud[i] = {
//only in canvas width
x : Math.floor((Math.random() * background.maxX) + background.minX),
//only (400,1600)
y : Math.floor(Math.random() * -1600),
type: types[randomType]
};
}
var meteorite = {};
for (i=0; i<=numberObjects; i++){
randomType = Math.floor(Math.random() * 2);
types = ["small", "big"];
meteorite[i] = {
//only in canvas width
x : Math.floor((Math.random() * background.maxX) + background.minX),
//only (1800, 3000)
y : Math.floor((Math.random() * -1200) - 1800),
type: types[randomType]
};
}
//in km
var karmarLine = 100;
var troposphere = 20;
var stratosphere = 50;
var mesosphere = 85;
var unit;
var message = "";
//Status of game - win|lost|start|explosion
var game = null;
//==========================Images======================================
//upload images
var img = {};
var imgNames = ["land", "rocketOff", "rocketOffLeft",
"rocketOffRight", "rocketOn", "rocketOnLeft",
"rocketOnRight", "rocketExplosion", "satellite",
"smallCloud", "bigCloud", "stormCloud",
"smallMeteorite", "bigMeteorite"];
for (var i = 0; i <= imgNames.length-1; i++){
img[imgNames[i]] = new Image();
img[imgNames[i]].src = 'images/'+imgNames[i]+".png";
}
//========================Graphics and rendering========================
//render graphics
function render(){
drawImage("background");
if(rocket.engine == 0){
drawImage("rocketOff");
} else {
drawImage("rocketOn");
}
if(game == "explosion"){
drawImage("rocketExplosion");
}
if(game == "win"){
drawImage("satellite");
}
drawImage("sky");
drawImage("space");
drawText();
move();
}
//draw images
function drawImage(name){
switch(name){
case "background":
ctx.drawImage(img.land, 0, background.y);
break;
case "rocketOff":
//center, which direction
if (rocket.direction == null){
ctx.drawImage(img.rocketOff, rocket.x, rocket.y);
break;
}
//left, which direction
if (rocket.direction == "left"){
ctx.drawImage(img.rocketOffLeft, rocket.x, rocket.y);
break;
}
//right, which direction
if (rocket.direction == "right"){
ctx.drawImage(img.rocketOffRight, rocket.x, rocket.y);
break;
}
case "rocketOn":
//center, which direction
if (rocket.direction == null){
ctx.drawImage(img.rocketOn, rocket.x, rocket.y);
break;
}
//left, which direction
if (rocket.direction == "left"){
ctx.drawImage(img.rocketOnLeft, rocket.x, rocket.y);
break;
}
//right, which direction
if (rocket.direction == "right"){
ctx.drawImage(img.rocketOnRight,rocket.x, rocket.y);
break;
}
case "rocketExplosion":
ctx.drawImage(img.rocketExplosion,rocket.x, rocket.y);
break;
case "satellite":
ctx.drawImage(img.satellite,rocket.x+100, rocket.y);
break;
case "sky":
for (i=0; i<=numberObjects; i++){
if (cloud[i].type == "big"){
ctx.drawImage(img.bigCloud, cloud[i].x, cloud[i].y);
}
if (cloud[i].type == "small"){
ctx.drawImage(img.smallCloud, cloud[i].x, cloud[i].y);
}
if (cloud[i].type == "storm"){
ctx.drawImage(img.stormCloud, cloud[i].x, cloud[i].y);
}
}
break;
case "space":
for (i=0; i<=numberObjects; i++){
if (meteorite[i].type == "small"){
ctx.drawImage(img.smallMeteorite, meteorite[i].x, meteorite[i].y);
}
if (meteorite[i].type == "big"){
ctx.drawImage(img.bigMeteorite, meteorite[i].x, meteorite[i].y);
}
}
break;
}
calcHeightRocket();
}
function drawText(){
ctx.font = "15px Ubuntu";
ctx.fillStyle = "#ffffff";
ctx.fillText("State: "+rocket.state,5,15);
ctx.fillText("Speed: "+rocket.speed,5,30);
ctx.fillText("Height: "+heightRocket+" "+unit,5, 45);
ctx.fillText("Terminal: "+message, 5, 60);
}
//===============================Listener===============================
addEventListener('keydown', controls, true);
function controls(e) {
switch (e.keyCode) {
// Left arrow, change locationX of rocket, change direction
case 37:
//you can not change direction at start
if (game != "start"){
rocket.x = rocket.x-rocket.turning;
if (rocket.direction == null){
rocket.direction = "left";
}
if (rocket.direction == "right"){
rocket.direction = null;
}
if (rocket.direction == "left"){
rocket.direction = "left";
}
}
break;
// Up arrow, turn on the engine
case 38:
//start, enable gravitation
game = null;
rocket.engine = 1;
break;
// Right arrow, change locationX of rocket, change direction
case 39:
//you can not change direction at start
if (game != "start"){
rocket.x = rocket.x+rocket.turning;
if (rocket.direction == null){
rocket.direction = "right";
}
if (rocket.direction == "left"){
rocket.direction = null;
}
if (rocket.direction == "right"){
rocket.direction = "right";
}
}
break;
// Down arrow, turn off the engine
case 40:
rocket.engine = 0;
break;
}
}
//move rocket
function move(){
//if the rocket has speed, height++ and change position of clouds
if (rocket.speed > 0){
background.y++;
for (i=0; i<=numberObjects; i++){
cloud[i].y++;
meteorite[i].y++;
}
} else{
//when the rocket launch, can not fall
if (game != "start"){
background.y--;
for (i=0; i<=numberObjects; i++){
cloud[i].y--;
meteorite[i].y--;
}
}
}
//if engine is on, increase speed
if (rocket.engine == 1 && rocket.speed <= rocket.maxSpeed){
rocket.speed++;
} else{
//when the rocket launch, can not decrease speed of rocket
if (game != "start"){
rocket.speed = rocket.speed - gravitation;
}
}
}
//=================================Logic================================
//calculate height of rocket
function calcHeightRocket(){
heightRocket = (background.y+3600)*28;//100000/3600=28
unit = "m";
//change units to km
if (heightRocket > 15000){
heightRocket = heightRocket/1000;
heightRocket = Math.round(heightRocket * 100) / 100;
unit = "km";
}
}
//win the game
function win(){
if (heightRocket > karmarLine && unit == "km"){
game = "win";
message = "[Victory] Your space program was successful, satellite was launched";
}
}
//lost connection the observatory
function lostConnection(){
if (rocket.x < background.minX || rocket.x > background.maxX){
game = "lost";
message = "[Lost of connection] Rocket has deviated from its course";
}
}
//destruction of the rocket
function destruction(){
if (rocket.speed < -300){
game = "explosion";
message = "[Destruction] Maximal descending speed is 300";
}
if (heightRocket < -30 && game == null){
game = "explosion";
message = "[Explosion] The rocket is not designed for landing on the ground";
}
if (rocket.state <= 0){
game = "explosion";
message = "[Destruction] Rocket was heavily damaged";
}
}
//detect the type of atmosphere
function detectTypeAtmosphere(){
if (heightRocket >= troposphere && unit == "km"){
message = "[Info] The rocket reached troposphere";
}
if (heightRocket >= stratosphere && unit == "km"){
message = "[Info] The rocket reached stratosphere";
}
if (heightRocket >= mesosphere && unit == "km"){
message = "[Info] The rocket reached mesosphere";
}
}
//detect contact with another object
function detectContact(type){
for (i=0; i<=numberObjects; i++){
var distanceX;
var distanceY;
var difference;
if (type == "sky"){
difference = 60;
distanceX = rocket.x-cloud[i].x;
distanceY = rocket.y-cloud[i].y;
} else {
difference = 45;
distanceX = rocket.x-meteorite[i].x;
distanceY = rocket.y-meteorite[i].y;
}
if (distanceX < difference && distanceX > -difference
&& distanceY < difference && distanceY > -difference){
if (type == "sky"){
switch(cloud[i].type){
case "small":
rocket.state = rocket.state - 1;
break;
case "big":
rocket.state = rocket.state - 2;
break;
case "storm":
rocket.state = rocket.state - 3;
break;
}
} else {
switch(meteorite[i].type){
case "small":
rocket.state = rocket.state - 5;
break;
case "big":
rocket.state = rocket.state - 10;
break;
}
}
}
}
}
//==========================Starting games==============================
// Cross-browser support for requestAnimationFrame
var w = window;
var requestAnimationFrame = w.requestAnimationFrame ||
w.webkitRequestAnimationFrame ||
w.msRequestAnimationFrame ||
w.mozRequestAnimationFrame;
//start, disable gravitation
game = "start";
//main function
function main(){
detectContact("sky");
detectContact("space");
detectTypeAtmosphere();
win();
lostConnection();
destruction();
render();
if (game != "explosion" && game != "lost" && game != "win"){
requestAnimationFrame(main);
}
}
main();
});