-
Notifications
You must be signed in to change notification settings - Fork 0
/
beau.html
1417 lines (1292 loc) · 38.5 KB
/
beau.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8"/>
<style>
canvas {
width:100%;
height: 50%;
border:1px solid #d3d3d3;
background-color: #f1f1f1;
display: block;
}
</style>
</head>
<center>
<button type="button" onmousedown="myGamePiece.up(4)" onmouseover="myGamePiece.up(1)" onmouseup="myGamePiece.stopy()" ontouchstart="myGamePiece.up(1)">UP</button><br>
<button type="button" onmousedown="myGamePiece.left(4)" onmouseover="myGamePiece.left(1)" onmouseup="myGamePiece.stopx()" ontouchstart="myGamePiece.left(1)">LEFT</button>
<button type="button" onmousedown="myGamePiece.stop()" onmouseover="myGamePiece.stop()" onmouseup="myGamePiece.stop()" ontouchstart="myGamePiece.stop()">STOP</button>
<button type="button" onmousedown="myGamePiece.right(4)" onmouseover="myGamePiece.right(1)" onmouseup="myGamePiece.stopx()" ontouchstart="myGamePiece.right(1)">RIGHT</button>
<br>
<button type="button" onmousedown="myGamePiece.down(4)" onmouseover="myGamePiece.down(1)" onmouseup="myGamePiece.stopy()" ontouchstart="myGamePiece.down(1)">DOWN</button>
<br>
<button type="button" onmouseover="myGamePiece.fire()" onmousedown="myGamePiece.fire()" onmouseup="myGamePiece.fire()" ontouchstart="myGamePiece.fire()">FIRE</button>
<br>
<p id='meet'></p>
<p id="fire"></p>
<br>
</center>
<h4>high scores</h4>
<ul text-align: left; id='highScores'></ul>
<body onload="startGame()">
<script>
// following https://michael-karen.medium.com/how-to-save-high-scores-in-local-storage-7860baca9d68
const NO_OF_HIGH_SCORES = 5;
const HIGH_SCORES = 'highScores';
var highScoreList = document.getElementById('highScores')
var xx = 0
function saveHighScore(score, highScores) {
var name = prompt('You got a highscore! Enter name:');
name = name.slice(0,3).toUpperCase()
var newScore = { score, name };
// 1. Add to list
highScores.push(newScore);
// 2. Sort the list
highScores.sort((a, b) => b.score - a.score);
// 3. Select new list
highScores.splice(NO_OF_HIGH_SCORES);
// 4. Save to local storage
localStorage.setItem(HIGH_SCORES, JSON.stringify(highScores));
};
function checkHighScore(score) {
const highScores = JSON.parse(localStorage.getItem(HIGH_SCORES)) ?? [];
const lowestScore = highScores[NO_OF_HIGH_SCORES - 1]?.score ?? 0;
if (score > lowestScore) {
saveHighScore(score, highScores); // TODO
showHighScores(); // TODO
}
}
function showHighScores() {
const highScores = JSON.parse(localStorage.getItem(HIGH_SCORES)) ?? [];
//const highScoreList = document.getElementById(HIGH_SCORES);
highScoreList.innerHTML = highScores
.map((score) => `<li>${score.score} - ${score.name}`)
.join('');
}
// from https://www.w3schools.com/graphics/game_sound.asp
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "true");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
var promise = this.sound.play();
// see https://developer.chrome.com/blog/autoplay/
if (promise !== undefined) {
promise.then(_ => {
// Autoplay started!
}).catch(error => {
// Autoplay was prevented.
// Show a "Play" button so that user can start playback.
});
}
}
this.stop = function(){
this.sound.pause();
}
}
// for testing
//const hobbit = "In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbithole, and that means comfort. It had a perfectly round door like a porthole, painted green, with a shiny yellow brass knob in the exact middle. The door opened on to a tubeshaped hall like a tunnel: a very comfortable tunnel without smoke, with panelled walls, and floors tiled and carpeted, provided with polished chairs, and lots and lots of pegs for hats and coats the hobbit was fond of visitors. The tunnel wound on and on, going fairly but not quite straight into the side of the hill The Hill, as all the people for many miles round called it and many little round doors opened out of it, first on one side and then on another. No going upstairs for the hobbit: bedrooms, bathrooms, cellars, pantries (lots of these), wardrobes (he had whole rooms devoted to clothes), kitchens, diningrooms, all were on the same floor, and indeed on the same passage. The best rooms were all on the lefthand side (going in), for these were the only ones to have windows, deepset round windows looking over his garden and meadows beyond, sloping down to the river."
let site = "https://raw.githubusercontent.com/UCL-EO/tudors/master/"
let level_up = 0
let level = 1
let maxlevels = 2;
let gameover = 0;
// max hits for castles
let maxhits = 6;
// max lives
let maxlives = 3;
// pause_count
let pause_count=100
let loseOneText = "You have lost the battle of the Spurs"
let loseTwoText = "You have lost to the Armada"
let winOneText = "You have won the battle of the Spurs";
let winTwoText = "You have beaten the Armada"
let meetOneText = "Meet the Tudors: Capture Pierre Terrail to win the battle of the Spurs"
let meetTwoText = "Meet the Tudors: Capture Philip II to beat the Armada"
// the pieces
let myGamePiece;
let yourGamePiece;
// interval between frames
let interval = 20
// image scale
let scale = 1
let myScore=0;
let buffer = 5;
let maxArrows = 3;
let maxEnemyArrows = 50;
let test=0;
let speedscale = 600
let pause_game = 0
let wait = 0
let mySound = new sound(site+"arrow.mp3");
let mySound2 = new sound(site+"crash.mp3");
let mySoundFrame = mySound
let winSound = new sound(site+"greensleeves-flute-and-guitar.mp3");
let winSound2 = new sound(site+"hampton_court.mp3");
let winSoundFrame = winSound
let mySoundLose = new sound(site+"machaut_puis.mp3")
let mySoundLose2 = new sound(site+"anonymous_cantigas.mp3")
let mySoundLoseFrame = mySoundLose
// was y : 633,
var background = {
file : site+"background.gif",
x : 1024,
y : 511,
startx : 0,
starty : 0,
type : "image",
direction : 0
};
var background2 = {
file : site+"background2.gif",
x : 1024,
y : 511,
startx : 0,
starty : 0,
type : "image",
direction : 0
};
var myAvatar = {
file : site+"henry.gif",
x : 70,
y : 70,
startx : 10,
starty : 120,
shot : {
file : site+"arrow.gif",
x : 50,
y : 10,
direction : 1,
},
castle : {
// root name for castle
file : site+"rhorse",
x : 77,
y : 77,
direction : 0
},
type : "image",
direction : 1
};
// level 2
var myAvatar2 = {
file : site+"drake.gif",
x : 70,
y : 70,
startx : 10,
starty : 120,
shot : {
file : site+"ball.gif",
x : 12,
y : 12,
direction : 1,
},
castle : {
// root name for castle
file : site+"englishship",
x : 77,
y : 77,
direction : 0
},
type : "image",
direction : 1
};
var yourAvatar = {
file : site+"pierre.gif",
x : 70,
y : 70,
startx : background.x - myAvatar.x - 10,
starty : 0,
shot : {
file : site+"back_arrow.gif",
x : 70,
y : 10,
direction : -1
},
castle : {
file : site+"horse",
x : 100,
y : 100,
direction : -1
},
type : "image",
direction : 1
};
var yourAvatar2 = {
file : site+"philip2.gif",
x : 70,
y : 70,
startx : background2.x - myAvatar2.x - 10,
starty : 0,
shot : {
file : site+"cannon.gif",
x : 10,
y : 10,
direction : -1
},
castle : {
file : site+"spanishship",
x : 100,
y : 100,
direction : -1
},
type : "image",
direction : 1
};
var myScoreTableSetup = {
font: "5px Consolas",
color : "red",
x : 50,
y : 50,
text : "Blaa blaa",
active : true
}
var myAvatarFrame=myAvatar
var yourAvatarFrame=yourAvatar
var backgroundFrame=background
var myScoreSetup = {
font: "25px Consolas",
color : "green",
x : 230,
y : 80,
text : ""
}
// text for game over etc
var myGameSetup = {
font: "100px Consolas",
color : "red",
x : background.x/4,
y : background.y/2,
text : ""
}
// text for game over etc
var myGameSetup2 = {
font: "50px Consolas",
color : "red",
x : background.x/4,
y : background.y/2+background.y/20,
text : ""
}
// life indicator
var myLifeSetup = {
font: "25px Consolas",
color : "black",
x : 230,
y : 120,
text : "❤️".repeat(maxlives) + " " + (level).toString()
}
function translate_origin (obj,startx,starty) {
//var new_obj = {...obj}
var new_obj = {
file : obj.file,
x : obj.x,
y : obj.y,
direction : obj.direction
}
new_obj.startx = Math.floor(startx);
new_obj.starty = Math.floor(starty);
return new_obj
}
// from https://stackoverflow.com/questions/5026961/html5-canvas-ctx-filltext-wont-do-line-breaks
var wrapText = function (context, text, x, y, maxWidth, lineHeight) {
var paragraphs = text.split("\n");
var textLines = [];
x = Math.ceil(x)
y = Math.ceil(y)
maxWidth = Math.ceil(maxWidth)
lineHeight = Math.ceil(lineHeight)
// Loop through paragraphs
for (var p = 0; p < paragraphs.length; p++) {
var line = "";
var words = paragraphs[p].split(" ");
// Loop through words
for (var w = 0; w < words.length; w++) {
var testLine = line + words[w] + " ";
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
// Make a line break if line is too long
if (testWidth > maxWidth) {
textLines.push(line.trim());
line = words[w] + " ";
}
else {
line = testLine;
}
}
textLines.push(line.trim());
}
// Move text up if centered vertically
if (context.textBaseline === 'middle')
y = y - ((textLines.length-1) * lineHeight) / 2;
if ( y < 1 ) y = 1;
if ( x < 1 ) x = 1;
// Render text on canvas
for (var tl = 0; tl < textLines.length; tl++) {
context.fillText(textLines[tl], x, y);
y += lineHeight;
}
};
function startGame(myAv,yourAv,bg) {
if ( ! myAv ) myAv = myAvatar
if ( ! yourAv ) yourAv = yourAvatar
if ( ! bg ) bg = background
// game pieces
myField = new component(bg);
// my character
myGamePiece = new component(myAv);
// your character
yourGamePiece = new component(yourAv);
// game over msgs
myGame = new text_component(myGameSetup);
myGame2 = new text_component(myGameSetup2);
// score
myScore = new text_component(myScoreSetup);
// Lives
myLife = new text_component(myLifeSetup);
myScoreTable = new text_component(myScoreTableSetup);
myGameArea.start();
}
// dummies for printer
var p1 = 5;
var p2 = 7;
// When true, moving the mouse draws on the canvas
let isDrawing = false;
let x = 0;
let y = 0;
var myGameArea = {
score : 0,
canvas : document.createElement("canvas"),
screen : document.addEventListener("mouseover", function() {
// use this to put messages on the screen when mouse over
p1 = ""
p2 = ""
printer(p1, p2);
}),
// from https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event
mdown : document.addEventListener('mousedown', e => {
e.returnValue = false;
e.preventDefault();
x = e.offsetX;
y = e.offsetY;
isDrawing = true;
}),
mup : document.addEventListener('mouseup', e => {
e.returnValue = false;
e.preventDefault();
if (isDrawing === true) {
x = 0;
y = 0;
isDrawing = false;
}
}),
mmove : document.addEventListener('mousemove', e => {
const t = 10 ;
e.returnValue = false;
e.preventDefault();
if (isDrawing === true) {
if ( x > 0 ) {
dx = -e.movementX; //x - e.offsetX;
dy = -e.movementY; //y - e.offsetY;
if(dx<0)myGamePiece.right(-1*t*dx);
else myGamePiece.left(t*dx);
if(dy<0)myGamePiece.down(-1*t*dy);
else myGamePiece.up(t*dy);
}
x = e.offsetX;
y = e.offsetY;
//printer(dx,dy)
}
}),
firer : document.addEventListener("dblclick", function(event) {
event.returnValue = false;
event.preventDefault();
// the above should fix selction issues in browser
myGamePiece.fire()
}),
keyboard : document.addEventListener("keydown", function(event) {
if (event.key == "d" || event.key == "l") {
myGamePiece.right(1);
} else if (event.key == "w" || event.key == "k") {
myGamePiece.up(1);
} else if (event.key == "s" || event.key == "j") {
myGamePiece.down(1);
} else if (event.key == "a" || event.key == "h") {
myGamePiece.left(1);
} else if (event.key == "q" || event.key == "r") {
reset_poor();
} else if (event.key == "f" || event.key == "m" ) {
myGamePiece.fire()
} else if (event.key == "z" || event.key == "p") {
myGamePiece.stop()
} else if (event.key == "P" || event.key == "O") {
myGamePiece.pause()
} else if (event.key == "C" ) {
cheat()
} else if (event.key == "X" ) {
myCheat();
}
}),
start : function() {
this.canvas.width = background.x;
this.canvas.height = background.y;
this.context = this.canvas.getContext("2d");
//this.context.textBaseline = 'middle'
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
// instructions
highScoreList.innerHTML = ""
showHighScores()
if ( level == 1 ) document.getElementById("meet").innerHTML = meetOneText;
else if ( level == 2 ) document.getElementById("meet").innerHTML = meetTwoText;
else document.getElementById("meet").innerHTML = ""
this.frameNo = 0;
this.interval = setInterval(updateGameArea, interval);
nCastles = Math.floor(Math.random()*6)+2
addLineOfCastles(myGamePiece,nCastles,Math.floor(backgroundFrame.x*0.1),myAvatarFrame.castle.x,myAvatarFrame.castle.y,myAvatarFrame.castle.file)
nCastles = 10
addLineOfCastles(yourGamePiece,nCastles,Math.floor(backgroundFrame.x*0.9-yourAvatarFrame.castle.x/4),yourAvatarFrame.castle.x,yourAvatarFrame.castle.y,yourAvatarFrame.castle.file)
},
textScreen : function(myText) {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
printTextObj(myText)
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
ctx = myGameArea.context;
ctx.drawImage(myField.image,
myField.x,
myField.y,
myField.width, myField.height);
}
}
function printer(a, b) {
document.getElementById("fire").innerHTML = a + " " + b;
}
var printTextObj = function (obj) {
// round up - use ceil
maxWidth = Math.ceil((background.x+obj.x) * 0.9)
// parseInt see
// https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_parseint1
// should work with eg "10px Consolas" to give 10
lineHeight= (Math.ceil(parseInt(obj.font)*1.1))
ctx = myGameArea.context
wrapText(ctx, obj.text, obj.x, obj.y, maxWidth, lineHeight)
}
function text_component(obj,active){
this.obj = obj;
//this.ctx = myGameArea.context;
if ( obj.text ) this.text = obj.text
this.x = this.obj.x;
this.y = this.obj.y;
this.font = this.obj.font;
this.color=this.obj.color;
if (active ) this.active = active
this.reset = function() {
this.x = this.obj.x;
this.y = this.obj.y;
this.font = this.obj.font;
this.color=this.obj.color;
}
this.update = function() {
ctx = myGameArea.context
ctx.font = this.font;
ctx.fillStyle = this.color;
if ( this.text ) ctx.fillText(this.text, this.x, this.y);
//if ( this.text ) printTextObj(this)
}
}
function clear_bullets(obj) {
if ( ! obj.myBullets.length ) return;
for (i = 0; i < obj.myBullets.length; i += 1) {
obj.myBullets[i].active = 0
}
obj.clearArrows()
}
// component is an object
// like an avatar
// but can contain Bullets and Castles
//
// The rules are:
// - Bullets can destroy Castles
function component(obj) {
this.image = new Image();
this.startfile = obj.file;
this.image.src = obj.file;
this.lives = maxlives;
this.startx = obj.startx;
this.starty = obj.starty;
this.startwidth = obj.x;
this.startheight = obj.y;
this.myCastles = [];
this.myBullets = [];
this.score = 0;
this.direction = obj.direction;
this.x = this.startx;
this.y = this.starty;
this.width = this.startwidth;
this.height = this.startheight;
this.speedX = 0;
this.speedY = 0;
this.image.src = this.startfile;
this.active=maxhits;
this.iwon = 0;
if (obj.shot) {
this.bullet = obj.shot;
this.castle = obj.castle;
}
this.reset = function() {
this.myBullets = [];
this.score = 0;
this.direction = obj.direction;
this.x = this.startx;
this.y = this.starty;
this.width = this.startwidth;
this.height = this.startheight;
this.speedX = 0;
this.speedY = 0;
this.image.src = this.startfile;
this.active=maxhits;
this.iwon = 0;
}
this.update = function() {
ctx = myGameArea.context;
if ( this.active) {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
}
}
this.newPos = function(isPiece) {
this.x += this.speedX;
this.y += this.speedY;
// you cant win if youre dead
if (this.lives) this.hitEnemy(isPiece)
this.hitBottom(isPiece);
this.hitTop(isPiece);
this.hitLeft(isPiece);
this.hitRight(isPiece);
}
this.clearArrows = function() {
var test;
var newList = [];
for (i = 0; i < this.myBullets.length; i += 1) {
test = this.myBullets[i]
if (test.x < 0 || test.x > background.x ) test.active = 0
if ( test.active > 0 ) {
newList.push(test);
}
}
this.myBullets = newList
}
this.bounceBottom = function(){
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.direction = 1 - this.direction
}
}
this.bounceTop = function(){
if (this.y < 0) {
this.direction = 1 - this.direction
}
}
this.hitBottom = function(isPiece) {
var rockbottom = myGameArea.canvas.height - this.height/2;
if (this.y > rockbottom) {
if ( isPiece ) {
this.y = rockbottom;
return true
} else {
this.active = 0
}
}
return false
}
this.hitTop = function(isPiece) {
if (this.y < -this.height/2) {
if ( isPiece ) {
this.y = -this.height/2;
return true
} else {
this.active = 0
}
}
return false
}
// what happens when you win
this.win = function() {
myGameArea.clear();
// clear the deck
winSoundFrame.play()
level_up=0;
// score
myGameArea.score += 1000;
clear_bullets(this)
clear_bullets(yourGamePiece)
//updateGameArea()
myScore.text="SCORE: " + Math.floor(myGameArea.score);
myScore.update();
if ( level == maxlevels ) {
myGame.text="YOU WIN"
checkHighScore(myGameArea.score)
myGame2.text=""
document.getElementById("meet").innerHTML = winTwoText;
this.iwon=1;
gameover=1
myGamePiece.x = Math.floor(background.x/2)
myGamePiece.y = Math.floor(background.y/2)
myGamePiece.width *= 3
myGamePiece.height *= 3
} else {
myGame.text="LEVEL UP"
myGame2.text=""
document.getElementById("meet").innerHTML = winOneText
pause_game=1
wait=pause_count * 10
level += 1
level_up=1
myGamePiece.x = Math.floor(background.x/2)
myGamePiece.y = Math.floor(background.y/2)
myGamePiece.width *= 3
myGamePiece.height *= 3
//updateGameArea()
// next level
}
myGame.update();
//updateGameArea()
if (level_up==1){
updateGameArea()
}
}
this.bigcheat = function(isPiece) {
// kill a ship
// loop over my castles
//for (j = 0; j < this.myCastles.length; j += 1) {
// this.myCastles[j].active = 0;
//}
// loop over enemy castles
for (j = 0; j < yourGamePiece.myCastles.length; j += 1) {
yourGamePiece.myCastles[j].active = 0;
}
// loop over enemy bullets
for (j = 0; j < yourGamePiece.myBullets.length; j += 1) {
yourGamePiece.myBullets[j].active=0;
}
// move to the right
//var rockbottom = myGameArea.canvas.width - this.width - buffer;
//this.x = rockbottom;
//this.newPos(true)
yourGamePiece.update()
//this.win()
}
this.die = function() {
// jump on a bullet
const yourPiece = yourGamePiece;
if ( yourPiece.myBullets.length ){
yourPiece.myBullets[0].active = 1;
this.x = yourPiece.myBullets[0].x;
this.y = yourPiece.myBullets[0].y;
}
}
this.hitEnemy = function(isPiece) {
if (myGamePiece.crashWith(yourGamePiece,0)){
if ( isPiece ) {
this.win();
return true
} else {
this.active = 0
}
}
return false
}
this.hitLeft = function(isPiece) {
var rockbottom = myGameArea.canvas.width - this.width - buffer;
if (this.x >= rockbottom) {
if ( isPiece ) {
this.x = rockbottom;
//this.win()
return true
} else {
this.active = 0
}
}
return false
}
this.hitRight = function(isPiece) {
if (this.x < 0) {
this.x = 0;
return true
}
return false
}
this.crashWith = function(otherobj,buff) {
if ( ! buff ) buff = buffer
if ( otherobj.active == 0 || this.active == 0) {
return false
}
var half = -1 * Math.floor(buff/2);
var myleft = this.x+half;
var myright = this.x + (this.width) - half;
var mytop = this.y + half;
var mybottom = this.y + (this.height) - half;
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
if ( crash ) {
mySoundFrame.play();
return true
}
return crash;
}
// fire and add to myBullets
this.fire = function() {
this.clearArrows()
if ( this.myBullets.length < maxArrows ) {
// this.myBullets.push(new component(translate_origin(myAvatarFrame.shot,this.x,this.y+Math.random()*70)));
this.myBullets.push(new component(translate_origin(myAvatarFrame.shot,this.x,Math.floor(this.y+45))));
this.myBullets[this.myBullets.length-1].active=maxhits;
document.getElementById("fire").style.color = '#d00'
printer("Fire!","")
}else{
document.getElementById("fire").style.color = '#000'
printer("Wait:","Out of ammo")
}
}
this.down = function(n) {
this.speedY = maxy(n);
}
this.up = function (n) {
this.speedY = -maxy(-n);
}
this.left = function(n) {
this.speedX = -maxx(-n);
}
this.right = function(n) {
this.speedX = maxx(n);
}
this.stopx = function() {
this.speedX = 0;
}
this.stopy = function() {
this.speedY = 0;
}
this.stop = function() {
this.stopx()
this.stopy()
}
this.removeBullet = function(bullet) {
bullet.update();
bullet.active = 0;
}
this.removeCastle = function(castle) {
// remove a castle life
castle.active -= 1;
fileroot = castle.startfile.split("_")[0]
castle.image.src = fileroot + "_" + (maxhits - castle.active).toString() + ".gif"
}
this.remove = function(bullet,castle) {
// remove a life
this.removeBullet(bullet)
this.removeCastle(castle)
}
this.bangTest = function(that) {
// check if bullets collide with castles
for (j = 0; j < this.myCastles.length; j += 1) {
for (i = 0; i < this.myBullets.length; i += 1) {
if (this.myCastles[j].crashWith(this.myBullets[i],0)) {
this.remove(this.myBullets[i],this.myCastles[j])
}
}
}
// check if bullets collide with other objects bullets
for (j = 0; j < that.myBullets.length; j += 1) {
for (i = 0; i < this.myBullets.length; i += 1) {
if (that.myBullets[j].crashWith(this.myBullets[i],0)) {
this.removeBullet(this.myBullets[i])
that.removeBullet(that.myBullets[j])
myGameArea.score += 10
myGame.text="GREAT SHOT"
myGame2.text=""
myGame.update();
}
}
}
// check if bullets collide with other objects castles
for (j = 0; j < that.myCastles.length; j += 1) {
for (i = 0; i < this.myBullets.length; i += 1) {
if (that.myCastles[j].crashWith(this.myBullets[i],0)) {
this.remove(this.myBullets[i],that.myCastles[j])
myGameArea.score += 1
myGame.text="DAMAGE"
myGame2.text=""
myGame.update();
}
}
}
// check if your bullets collide with my castles
for (j = 0; j < this.myCastles.length; j += 1) {
for (i = 0; i < that.myBullets.length; i += 1) {
if (this.myCastles[j].crashWith(that.myBullets[i],0)) {
that.remove(that.myBullets[i],this.myCastles[j])
}
}
}
// check if your castles collide with your castles
for (j = 0; j < that.myCastles.length; j += 1) {
for (i = 0; i < this.myCastles.length; i += 1) {
if (that.myCastles[j].crashWith(this.myCastles[i],0)) {
that.removeCastle(this.myCastles[i])
that.removeCastle(that.myCastles[j])
}
}
}
}
}
function myCheat() {
// kill a ship
j = Math.floor(Math.random() * myGamePiece.myCastles.length)
myGamePiece.myCastles[j].active=0;
return
}
function cheat() {
// kill a ship
j = Math.floor(Math.random() * yourGamePiece.myCastles.length)
yourGamePiece.myCastles[j].active=0;
// loop over enemy bullets
for (j = 0; j < yourGamePiece.myBullets.length; j += 1) {
yourGamePiece.myBullets[j].active=0;
}
//yourGamePiece.update()
return
}
function loveHearts(myPiece) {
const nBroken = maxlives - myPiece.lives
const nLive = myPiece.lives
myLife.text=""
if (nBroken > 0) myLife.text += "💔".repeat(nBroken)
if (nLive > 0) myLife.text += "💚".repeat(nLive)
// add on the level indicator
myLife.text += " level " + (level).toString()
myLife.update();
return
}
function loser(myPiece,yourPiece) {
// what happens if you lose
// game over
for (i = 0; i < myPiece.myBullets.length; i += 1) {
myPiece.myBullets[i].active = 0
}
myGame.text="GAME OVER"
//checkHighScore(myGameArea.score)
if ( level == 1 ) {
document.getElementById("meet").innerHTML = loseOneText
} else {
document.getElementById("meet").innerHTML = loseTwoText;
}
myGame2.text=""
gameover = 1;
myPiece.iwon=0;
yourPiece.iwon=1;
yourPiece.x = Math.floor(background.x/2)
yourPiece.y = Math.floor(background.y/2)
yourPiece.width *= 3
yourPiece.height *= 3
myGame.update();
mySoundLoseFrame.play()
return true
}
function amIHit(myPiece,yourPiece) {
// have you shot me with your arrows?
// check if your arrows collide with myPiece
for (i = 0; i < yourPiece.myBullets.length; i += 1) {
if (myPiece.crashWith(yourPiece.myBullets[i],0)) {
myPiece.lives = myPiece.lives - 1;
myGame.text="YOU'RE HIT"
myGame2.text=""
loveHearts(myPiece)
yourPiece.myBullets[i].active = 0
if ( myPiece.lives < 1 ) if (loser(myPiece,yourPiece)) return true
}
}
// collision tests for castles and arrows
myPiece.bangTest(yourPiece)
// check if myPiece collides with my castles
// bounce back
for (j = 0; j < myPiece.myCastles.length; j += 1) {
if ( myPiece.myCastles[j].active > 0) {
if (myPiece.crashWith(myPiece.myCastles[j],buffer)) {
myPiece.stop()
myPiece.left(1)
}
}
}