-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
770 lines (658 loc) · 13.7 KB
/
index.html
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<style>
canvas {
border: 1px solid black;
width: 100%;
}
* {
-webkit-user-select: none; /*disable text selection */
}
</style>
</head>
<body>
<h1>Javascript game</h1>
<p>Left-Click on Button to exchange energy. 5 Star will get 1 energy </p>
<p id ="starText">Your current Star : </p>
<p id = "exchangeNum">You can exchange </p>
Exchange <input type="number" id="myText" min="1" max="10" size="4" value = "5" style = "display:inline"> star
<button id="myBtn" onclick="exchangeStar()">Exchange</button>
<canvas id="mycanvas" width="640" height="160" style = "margin-top : 1%" =></canvas>
<script>
// Check key pressed
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
// Size of World
var game_width = 640;
var game_height = 160;
const canvas = document.getElementById('mycanvas');
const ctx = canvas.getContext('2d');
// Information about level and heart
var level = 1;
var chance = 2;
var current_star = 0;
// Background
var background = new Image();
background.src = "\level1bg.jpg";
//Img of player
var sun = new Image();
sun.src = "\carlv1.png";
//Img of Energy
var energyI = new Image();
energyI.src = "\energy_location.png";
//Img of Star
var starI = new Image();
starI.src = "\star.png";
//Img of Boom
var boomI = new Image();
boomI.src = "\army.png";
//Img of goal
var goal = new Image()
goal.src = "\goal.png";
//Level 2 Enerry
var getE = false;
var current_E = 0;
// Boom
var boom = [
{// Boom [0]
x : canvas.width - 40,
y : 0,
width : 40,
height : 40,
rotation : 0,
speed : -1
},
{// Boom[1]
x : canvas.width - 40,
y : canvas.height - 40,
width : 40,
height : 40,
rotation : 0,
speed : -1
}
]
// Star
var star = [
{// Star 1
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 2
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 3
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 4
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 5
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
}
]
// Energy
var energy = {
x : 170,
y : 30,
width : 10,
height : 10
}
// Goal
var goalI = {
x : canvas.width - 60,
y : 0,
width : 60,
height : canvas.height
}
// Player
var player = {
x : 10,
y : 80,
speed : 2,
width : 40,
height : 40,
moveR : false,
moveL : false,
jumpU : false,
jumpD : false,
energy_Speed : false
}
// Enemy
var color_e = "rgb(0,255,120)";
var enemy = [
{// enemy 1
x : 100,
y : 0,
speed : 2,
width : 40,
height : 40
},
{//enemy 2
x : 170,
y : 120,
speed : -2,
width : 40,
height : 40
},
{//enemy 3
x : 240,
y : 0,
speed : 2,
width : 40,
height : 40
},
{//enemy 4
x : 310,
y : 120,
speed : -2,
width : 40,
height : 40
},
{// enemy 5
x : 380,
y : 0,
speed : 2,
width : 40,
height : 40
},
{// enemy 6
x : 450,
y : 120,
speed : -2,
width : 40,
height : 40
},
{// enemy 7
x : 520,
y : 0,
speed : 2,
width : 40,
height : 40
},
]
// Main Function
function updateAll(){
ctx.clearRect(0,0,game_width,game_height); // Clean
ctx.drawImage(background,0,0); // Draw Background
ctx.drawImage(goal, canvas.width - 60 , 0, 60, canvas.height); //Draw goal
textUpdate(); // set Text for level and health
playerMove();// set X coordinate player
ctx.drawImage(sun, player.x , player.y, player.width, player.height); // set player move.
boomMove();// Moving boom
checkEnery(); // Check and Draw energy
if(checkTouch(player,goalI)) setNewLevel(); // Check Winner
enemiesMove(); // set X coordinate enemy
enemiesMoving(); // set enemy moving
drawStar(); // Draw Star
requestAnimationFrame(updateAll);
}
// check press key
function keyDownHandler(e) {
if(e.key == "Enter")
document.getElementById("myBtn").click();
if(e.key == "Right" || e.key == "ArrowRight")
player.moveR = true;
else if(e.key == "Left" || e.key == "ArrowLeft")
player.moveL = true;
if(e.key == "ArrowUp")
player.jumpU = true;
else if(e.key == "ArrowDown")
player.jumpD = true;
// Enerry
if(e.key == "Control" && current_E > 0)
player.energy_Speed = true;
}
// check release key
function keyUpHandler(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
player.moveR = false;
}
else if(e.key == "Left" || e.key == "ArrowLeft") {
player.moveL = false;
}
if(e.key == "ArrowUp") {
player.jumpU = false;
}
else if(e.key == "ArrowDown") {
player.jumpD = false;
}
// Energy
if(e.key == "Control")
player.energy_Speed = false;
}
function returnP() // Return position
{
if(chance > 0)
{
chance--;
alert("Please becarefull, you have "+chance+" heart left!");
//Set Player
player.x = 10;
player.y = 80;
player.moveR = false;
player.moveL = false;
player.jumpU = false;
player.jumpD = false;
//Set Level 2
if(level == 2)
{
getE = false;
current_E = 0;
}
if(level == 3)
{
getE = true;
current_E = 2;
setBoomDefaut();
}
}// End set when have chance
else
{
alert("Game Over!");// No more Heart and lose and set to LV 1
setDefaut();
}
}
function drawStar() // Draw Star and check Touch
{
for (var i = 0; i < star.length; i++)
{
if(star[i].display) ctx.drawImage(starI, star[i].x, star[i].y, star[i].width, star[i].height)
if(checkTouch(player,star[i]) && star[i].display)
{
current_star++;
star[i].display = false;
}
}
}
function checkEnery()// Check and Draw Energy
{
if( level == 2 && getE == false)
{
ctx.fillStyle = "#DDFF46";
ctx.fillRect(energy.x, energy.y, energy.width, energy.height);
if(checkTouch(player, energy))
{
getE = true;
current_E++;
}
}
}
function playerMove()// Set X,Y coordinate of player and check Energy
{
if (player.moveR && player.x < canvas.width - player.width)
player.x += player.speed;
if (player.moveL && player.x > 0)
player.x -= player.speed;
if (player.jumpU && player.y > 0)
player.y -= player.speed;
if (player.jumpD && player.y < canvas.height - player.height)
player.y += player.speed;
if(player.x < canvas.width - player.width && player.energy_Speed) // Energy Speed Activate
{
player.x += 180;
player.energy_Speed = false;
current_E--;
ctx.clearRect(0,0,game_width,game_height);
ctx.drawImage(sun, player.x, player.y, player.width, player.height);
}
}
function enemiesMove()// Set X, Y coordinate of enermy and check if touch edge
{
for (var i = 0; i < enemy.length; i++)
{
enemy[i].y += enemy[i].speed;
if (enemy[i].y < 0 || enemy[i].y > canvas.height - enemy[i].height)
enemy[i].speed *= -1;
}
}
function enemiesMoving()// Draw Enemy
{
for (var i = 0; i < enemy.length; i++)
{
ctx.fillStyle = color_e;
ctx.fillRect(enemy[i].x, enemy[i].y, enemy[i].width, enemy[i].height);// set enemy moves by array
if (checkTouch(player, enemy[i]) == true)// Check if touching enemy decrease health or setDefaut in array for each enemy
{
returnP();
}
}
}
function boomMove()// Moving Boom and check Boom touch Car
{
if (level == 3)// Move on level 3 only
{
boom[0].x += boom[0].speed;
boom[1].x += boom[1].speed;
ctx.drawImage(boomI, boom[0].x, boom[0].y, boom[0].width, boom[0].height);
ctx.drawImage(boomI, boom[1].x, boom[1].y, boom[1].width, boom[1].height);
for (var i = 0; i < boom.length; i++)// Check if Boom touch edge & touch Car
{
if(boom[i].x < 0 || boom[i].x > canvas.width - boom[i].width) boom[i].speed *= -1;
if(checkTouch(player,boom[i])) returnP();
}
}
}
function setNewLevel() // Set New level
{
if(level < 2) // Set Level 2
{
// Set level and heart
level = 2;
chance += 10;
//Set Background and enemy color
background.src = "\level2bg.png";
color_e = "#FFFF99";
//Set Player
sun.src = "carlv2.png";
setPlayerToBegin();
//Set Enemy
setEnemyDefaut();
enemy[1].speed = -4;
enemy[4].speed = 4;
enemy[6].speed = 4;
enemy[2].height = 70;
enemy[5].height = 70;
enemy[6].height = 70;
enemy[5].y = canvas.height - enemy[5].height;
//Set Star
setStar();
return;
}
if(level < 3) // Set Level 3
{
//Set Level and Heart
level = 3;
chance += 10;
//Set Background and enemy color
background.src = "\level2bg.png";
color_e = "#FFFF99";
//Set Player
sun.src = "carlv2.png";
setPlayerToBegin();
// Set enemy
setEnemyDefaut();
enemy[1].speed = -4;
enemy[4].speed = 4;
enemy[6].speed = 4;
enemy[3].speed = -4;
for (var i = 0; i < enemy.length; i++)
{
enemy[i].height = 70;
if (enemy[i].y > 0) enemy[i].y = canvas.height - enemy[i].height;
}
//Set Star
setStar();
return;
}
}
function setEnemyDefaut()
{
enemy = [
{// enemy 1
x : 100,
y : 0,
speed : 2,
width : 40,
height : 40
},
{//enemy 2
x : 170,
y : 120,
speed : -2,
width : 40,
height : 40
},
{//enemy 3
x : 240,
y : 0,
speed : 2,
width : 40,
height : 40
},
{//enemy 4
x : 310,
y : 120,
speed : -2,
width : 40,
height : 40
},
{// enemy 5
x : 380,
y : 0,
speed : 2,
width : 40,
height : 40
},
{// enemy 6
x : 450,
y : 120,
speed : -2,
width : 40,
height : 40
},
{// enemy 7
x : 520,
y : 0,
speed : 2,
width : 40,
height : 40
},
]
}
function setStar()
{
star = [
{// Star 1
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 2
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 3
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 4
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
},
{// Star 5
x : Math.floor((Math.random() * 520) + 60),
y : Math.floor((Math.random() * 104) + 20),
width : 20,
height : 20,
display : true
}
]
}
function setDefaut() // Set to level 1 Defaut
{
color_e = "rgb(0,255,120)";
background.src = "\level1bg.jpg";
sun.src = "\carlv1.png";
chance = 2;
level = 1;
player = {
x : 10,
y : 80,
speed : 2,
width : 40,
height : 40,
moveR : false,
moveL : false
}
enemy = [
{// enemy 1
x : 100,
y : 0,
speed : 2,
width : 40,
height : 40
},
{//enemy 2
x : 170,
y : 120,
speed : -2,
width : 40,
height : 40
},
{//enemy 3
x : 240,
y : 0,
speed : 2,
width : 40,
height : 40
},
{//enemy 4
x : 310,
y : 120,
speed : -2,
width : 40,
height : 40
},
{// enemy 5
x : 380,
y : 0,
speed : 2,
width : 40,
height : 40
},
{// enemy 6
x : 450,
y : 120,
speed : -2,
width : 40,
height : 40
},
{// enemy 7
x : 520,
y : 0,
speed : 2,
width : 40,
height : 40
},
]
//Set Star
setStar();
}
function textUpdate()// Draw text Information
{
ctx.font = "bold 11pt Courier";
ctx.fillStyle = "#33CCFF";
ctx.fillText("Level: "+level, 10, 20);
ctx.fillStyle = "#FF6600";
ctx.fillText("Heart: "+chance,10,40);
if(level > 1) // draw energy
{
ctx.fillStyle = "#DD6600";
ctx.fillText("Energy: "+current_E,10,60);
if(current_E > 0)
{
ctx.drawImage(energyI, player.x + 180, player.y, player.width, player.height);
}
}
document.getElementById("starText").innerHTML = "Your current Star : "+ current_star;
document.getElementById("exchangeNum").innerHTML = "Your can exchange "+ Math.floor(current_star/5)+" energy";
}
function checkTouch(obj1, obj2) // Checking Function
{
var widthT =false;
var heightT = false;
// Check Objects
if (obj1.x + obj1.width > obj2.x && obj1.x < obj2.x + obj2.width && obj2 != goalI)
widthT = true;
if( obj1.y < obj2.y + obj2.height && obj1.height + obj1.y > obj2.y && obj2 != goalI)
heightT = true;
// Check Goal
if(obj1.x + obj1.width/2 > goalI.x){
return true;
}
return widthT && heightT;
}
function setPlayerToBegin() // Set Player To Begin area
{
player.x = 10;
player.y = 80;
player.speed = 3;
player.moveR = false;
player.moveL = false;
player.jumpU = false;
player.jumpD = false;
player.width = 20;
player.size = 10;
}
function setBoomDefaut() // Set BoomDefaut
{
boom = [
{// Boom [0]
x : canvas.width - 40,
y : 0,
width : 40,
height : 40,
rotation : 0,
speed : -1
},
{// Boom[1]
x : canvas.width - 40,
y : canvas.height - 40,
width : 40,
height : 40,
rotation : 0,
speed : -1
}
]
}
function exchangeStar()
{
var numExchange = document.getElementById("myText").value;
if (numExchange > current_star){
alert("Can't Exchange!!");
return;
}
var percent = Math.floor(numExchange/5);
if (percent => 1)
{
current_E = percent;
current_star -= numExchange;
}
else
alert("Can't exchange Energy!!!");
}
updateAll();
</script>
</body>
</html>