forked from varunpant/CrappyBird
-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.html
907 lines (752 loc) · 28.3 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
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
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="Flappy Bird,game,Crappy Bird,clone,javascript,html5 game,mobile,canvas,flappy bird source" name="keywords"/>
<meta name="description" content="Clone of Flappy bird game in javascript using canvas 2d">
<meta name="viewport" content="width=device-width,
user-scalable=no, initial-scale=1, maximum-scale=1, user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<style type="text/css">
body {
margin: 0;
padding: 0;
background: #000;
}
canvas {
display: block;
margin: 0 auto;
background: #fff;
}
audio {
display: none;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
/*
=================================================================
script: CrappyBird
author: Varun Pant
date: April 6, 2014
site: http://www.varunpant.com
=================================================================
*/
// http://paulirish.com/2011/requestanimationframe-for-smart-animating
// shim layer with setTimeout fallback
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
//sounds
var soundJump = new Audio("wing.ogg");
var soundScore = new Audio("point.ogg");
var soundHit = new Audio("hit.ogg");
var soundDie = new Audio("die.ogg");
var soundSwoosh = new Audio("swooshing.ogg");
//http://www.storiesinflight.com/html5/audio.html
var channel_max = 10; // number of channels
audiochannels = new Array();
for (a = 0; a < channel_max; a++) { // prepare the channels
audiochannels[a] = new Array();
audiochannels[a]['channel'] = new Audio(); // create a new audio object
audiochannels[a]['finished'] = -1; // expected end time for this channel
}
function play_sound(s) {
for (a = 0; a < audiochannels.length; a++) {
thistime = new Date();
if (audiochannels[a]['finished'] < thistime.getTime()) { // is this channel finished?
audiochannels[a]['finished'] = thistime.getTime() + s.duration * 1000;
audiochannels[a]['channel'].src = s.src;
audiochannels[a]['channel'].load();
audiochannels[a]['channel'].play();
break;
}
}
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
// namespace our game
var FB = {
// set up some inital values
WIDTH: 320,
HEIGHT: 480,
scale: 1,
// the position of the canvas
// in relation to the screen
offset: {
top: 0,
left: 0
},
// store all bird, touches, pipes etc
entities: [],
currentWidth: null,
currentHeight: null,
canvas: null,
ctx: null,
score: {
taps: 0,
coins: 0
},
distance: 0,
digits:[],
fonts:[],
// we'll set the rest of these
// in the init function
RATIO: null,
bg_grad: "day",
game:null,
currentWidth: null,
currentHeight: null,
canvas: null,
ctx: null,
ua: null,
android: null,
ios: null,
gradients: {},
init: function () {
var grad;
// the proportion of width to height
FB.RATIO = FB.WIDTH / FB.HEIGHT;
// these will change when the screen is resize
FB.currentWidth = FB.WIDTH;
FB.currentHeight = FB.HEIGHT;
// this is our canvas element
FB.canvas = document.getElementsByTagName('canvas')[0];
// it's important to set this
// otherwise the browser will
// default to 320x200
FB.canvas.width = FB.WIDTH;
FB.canvas.height = FB.HEIGHT;
// the canvas context allows us to
// interact with the canvas api
FB.ctx = FB.canvas.getContext('2d');
// we need to sniff out android & ios
// so we can hide the address bar in
// our resize function
FB.ua = navigator.userAgent.toLowerCase();
FB.android = FB.ua.indexOf('android') > -1 ? true : false;
FB.ios = (FB.ua.indexOf('iphone') > -1 || FB.ua.indexOf('ipad') > -1) ? true : false;
// setup some gradients
grad = FB.ctx.createLinearGradient(0, 0, 0, FB.HEIGHT);
grad.addColorStop(0, '#036');
grad.addColorStop(0.5, '#69a');
grad.addColorStop(1, 'yellow');
FB.gradients.dawn = grad;
grad = FB.ctx.createLinearGradient(0, 0, 0, FB.HEIGHT);
grad.addColorStop(0, '#69a');
grad.addColorStop(0.5, '#9cd');
grad.addColorStop(1, '#fff');
FB.gradients.day = grad;
grad = FB.ctx.createLinearGradient(0, 0, 0, FB.HEIGHT);
grad.addColorStop(0, '#036');
grad.addColorStop(0.3, '#69a');
grad.addColorStop(1, 'pink');
FB.gradients.dusk = grad;
grad = FB.ctx.createLinearGradient(0, 0, 0, FB.HEIGHT);
grad.addColorStop(0, '#036');
grad.addColorStop(1, 'black');
FB.gradients.night = grad;
// listen for clicks
window.addEventListener('click', function (e) {
e.preventDefault();
FB.Input.set(e);
}, false);
// listen for touches
window.addEventListener('touchstart', function (e) {
e.preventDefault();
// the event object has an array
// called touches, we just want
// the first touch
FB.Input.set(e.touches[0]);
}, false);
window.addEventListener('touchmove', function (e) {
// we're not interested in this
// but prevent default behaviour
// so the screen doesn't scroll
// or zoom
e.preventDefault();
}, false);
window.addEventListener('touchend', function (e) {
// as above
e.preventDefault();
}, false);
// we're ready to resize
FB.resize();
FB.changeState("Splash");
FB.loop();
},
resize: function () {
FB.currentHeight = window.innerHeight;
// resize the width in proportion
// to the new height
FB.currentWidth = FB.currentHeight * FB.RATIO;
// this will create some extra space on the
// page, allowing us to scroll pass
// the address bar, and thus hide it.
if (FB.android || FB.ios) {
document.body.style.height = (window.innerHeight + 50) + 'px';
}
// set the new canvas style width & height
// note: our canvas is still 320x480 but
// we're essentially scaling it with CSS
FB.canvas.style.width = FB.currentWidth + 'px';
FB.canvas.style.height = FB.currentHeight + 'px';
// the amount by which the css resized canvas
// is different to the actual (480x320) size.
FB.scale = FB.currentWidth / FB.WIDTH;
// position of canvas in relation to
// the screen
FB.offset.top = FB.canvas.offsetTop;
FB.offset.left = FB.canvas.offsetLeft;
// we use a timeout here as some mobile
// browsers won't scroll if there is not
// a small delay
window.setTimeout(function () {
window.scrollTo(0, 1);
}, 1);
},
// this is where all entities will be moved
// and checked for collisions etc
update: function () {
FB.game.update();
FB.Input.tapped = false;
},
// this is where we draw all the entities
render: function () {
FB.Draw.rect(0, 0, FB.WIDTH, FB.HEIGHT, FB.gradients[FB.bg_grad]);
// cycle through all entities and render to canvas
for (i = 0; i < FB.entities.length; i += 1) {
FB.entities[i].render();
}
FB.game.render();
},
// the actual loop
// requests animation frame
// then proceeds to update
// and render
loop: function () {
requestAnimFrame(FB.loop);
FB.update();
FB.render();
},
changeState: function(state) {
FB.game = new window[state]();
FB.game.init();
}
};
// abstracts various canvas operations into
// standalone functions
FB.Draw = {
clear: function () {
FB.ctx.clearRect(0, 0, FB.WIDTH, FB.HEIGHT);
},
rect: function (x, y, w, h, col) {
FB.ctx.fillStyle = col;
FB.ctx.fillRect(x, y, w, h);
},
circle: function (x, y, r, col) {
FB.ctx.fillStyle = col;
FB.ctx.beginPath();
FB.ctx.arc(x + 5, y + 5, r, 0, Math.PI * 2, true);
FB.ctx.closePath();
FB.ctx.fill();
},
Image:function(img,x,y){
FB.ctx.drawImage(img,x,y);
},
Sprite: function (img, srcX, srcY, srcW, srcH, destX, destY, destW, destH, r) {
FB.ctx.save();
FB.ctx.translate(destX, destY);
FB.ctx.rotate(r * (Math.PI / 180));
FB.ctx.translate(-(destX + destW / 2), -(destY + destH / 2));
FB.ctx.drawImage(img, srcX, srcY, srcW, srcH, destX, destY, destW, destH);
FB.ctx.restore();
},
semiCircle: function (x, y, r, col) {
FB.ctx.fillStyle = col;
FB.ctx.beginPath();
FB.ctx.arc(x, y, r, 0, Math.PI, false);
FB.ctx.closePath();
FB.ctx.fill();
},
text: function (string, x, y, size, col) {
FB.ctx.font = 'bold ' + size + 'px Monospace';
FB.ctx.fillStyle = col;
FB.ctx.fillText(string, x, y);
}
};
FB.Input = {
x: 0,
y: 0,
tapped: false,
set: function (data) {
this.x = (data.pageX - FB.offset.left) / FB.scale;
this.y = (data.pageY - FB.offset.top) / FB.scale;
this.tapped = true;
}
};
FB.Cloud = function (x, y) {
this.x = x;
this.y = y;
this.r = 30;
this.col = 'rgba(255,255,255,1)';
this.type = 'cloud';
// random values so particles do no
// travel at the same speeds
this.vx = -0.10;
this.remove = false;
this.update = function () {
// update coordinates
this.x += this.vx;
if (this.x < (0 - 115)) {
this.respawn();
}
};
this.render = function () {
FB.Draw.circle(this.x + this.r, (this.y + this.r), this.r, this.col);
FB.Draw.circle(this.x + 55, (this.y + this.r / 2), this.r / 0.88, this.col);
FB.Draw.circle(this.x + 55, (this.y + this.r + 15), this.r, this.col);
FB.Draw.circle(this.x + 85, (this.y + this.r), this.r, this.col);
};
this.respawn = function () {
this.x = ~~ (Math.random() * this.r * 2) + FB.WIDTH;
this.y = ~~ (Math.random() * FB.HEIGHT / 2)
};
};
FB.BottomBar = function (x, y, r) {
this.x = x;
this.y = y
this.r = r;
this.vx = -1;
this.name = 'BottomBar';
this.update = function () {
// update coordinates
this.x += this.vx;
if (this.x < (0 - this.r)) {
this.respawn();
}
};
this.render = function () {
FB.Draw.rect(this.x, this.y, this.r, 100, '#D2691E');
for (var i = 0; i < 10; i++) {
FB.Draw.semiCircle(this.x + i * (this.r / 9), this.y, 20, '#050');
}
}
this.respawn = function () {
this.x = FB.WIDTH - 1;
}
}
FB.Tree = function (x, y) {
this.x = x;
this.y = y
this.r = 30;
this.h = 50;
this.w = this.r * 2;
this.vx = -1;
this.type = 'Tree';
this.update = function () {
// update coordinates
this.x += this.vx;
if (this.x < (0 - this.r * 2)) {
this.respawn();
}
};
this.render = function () {
//FB.Draw.rect(this.x, this.y, this.w, this.h, '#c20');
FB.Draw.circle(this.x + this.r, (this.y + this.r) - 10, this.r, 'green', '#050');
FB.Draw.circle(this.x + (this.r / 2), (this.y + this.r) - 10, this.r / 3, 'rgba(0,0,0,0.08)');
FB.Draw.rect(this.x + this.r, this.y + this.r, 10, this.r, 'brown', '#d20');
}
this.respawn = function () {
this.x = FB.WIDTH + this.r;
}
}
FB.Pipe = function (x, w) {
this.centerX = x;
this.coin = true
this.w = w;
this.h = FB.HEIGHT - 150;
this.vx = -1;
this.type = 'pipe';
this.update = function () {
// update coordinates
this.centerX += this.vx;
if (this.centerX == (0 - this.w)) {
this.respawn();
}
};
this.render = function () {
if (this.coin) {
FB.Draw.circle(this.centerX + this.w / 2 - 5, this.centerY - 5, 5, "Gold")
}
FB.Draw.rect(this.centerX, 0, this.w, this.centerY - 50, '#8ED6FF');
FB.Draw.rect(this.centerX, this.centerY + 50, this.w, this.h - this.centerY, '#8ED6FF');
}
this.respawn = function () {
this.centerY = this.randomIntFromInterval(70, 220);
this.centerX = 320 - this.w + 160;
this.coin = true;
}
this.randomIntFromInterval = function (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
this.centerY = this.randomIntFromInterval(70, 220);
}
FB.Bird = function () {
this.img = new Image();
this.img.src = 'bird.png';
this.gravity = 0.25;
this.width = 34;
this.height = 24;
this.ix = 0;
this.iy = 0;
this.fr = 0;
this.vy = 180;
this.vx = 70;
this.velocity = 0;
this.play = false;
this.jump = -4.6;
this.rotation = 0;
this.type = 'bird';
this.update = function () {
if (this.fr++ > 5) {
this.fr = 0;
if (this.iy == this.height * 3) {
this.iy = 0
}
this.iy += this.height;
}
if (this.play) {
this.velocity += this.gravity;
this.vy += this.velocity;
if (this.vy <= 0) {
this.vy = 0;
}
if (this.vy >= 370) {
this.vy = 370;
}
this.rotation = Math.min((this.velocity / 10) * 90, 90);
}
if (FB.Input.tapped) {
this.play = true;
play_sound(soundJump);
this.velocity = this.jump;
}
};
this.render = function () {
FB.Draw.Sprite(this.img, this.ix, this.iy, this.width, this.height, this.vx, this.vy, this.width, this.height, this.rotation);
}
}
FB.Particle = function (x, y, r, col, type) {
this.x = x;
this.y = y;
this.r = r;
this.col = col;
this.type = type || 'circle';
this.name = 'particle';
// determines whether particle will
// travel to the right of left
// 50% chance of either happening
this.dir = (Math.random() * 2 > 1) ? 1 : -1;
// random values so particles do no
// travel at the same speeds
this.vx = ~~ (Math.random() * 4) * this.dir;
this.vy = ~~ (Math.random() * 7);
this.remove = false;
this.update = function () {
// update coordinates
this.x += this.vx;
this.y -= this.vy;
// increase velocity so particle
// accelerates off screen
this.vx *= 0.99;
this.vy *= 0.99;
// adding this negative amount to the
// y velocity exerts an upward pull on
// the particle, as if drawn to the
// surface
this.vy -= 0.35;
// offscreen
if (this.y > FB.HEIGHT) {
this.remove = true;
}
};
this.render = function () {
if (this.type === 'star') {
FB.Draw.star(this.x, this.y, this.col);
} else {
FB.Draw.circle(this.x, this.y, this.r, this.col);
}
};
};
// checks if two entities are touching
FB.Collides = function (bird, pipe) {
if(bird.vy >=370){
return true;
}
if (pipe.coin && bird.vx > pipe.centerX + pipe.w / 2 - 5) {
pipe.coin = false;
FB.score.coins += 1;
FB.digits = FB.score.coins.toString().split('');
play_sound(soundScore);
}
var bx1 = bird.vx - bird.width / 2;
var by1 = bird.vy - bird.height / 2;
var bx2 = bird.vx + bird.width / 2;
var by2 = bird.vy + bird.height / 2;
var upx1 = pipe.centerX;
var upy1 = 0;
var upx2 = pipe.centerX + pipe.w;
var upy2 = pipe.centerY - 50;
var lpx1 = pipe.centerX;
var lpy1 = pipe.centerY + 50;
var lpx2 = upx2;
var lpy2 = pipe.h;
var c1 = !(bx1 > upx2 ||
bx2 < upx1 ||
by1 > upy2 ||
by2 < upy1)
var c2 = !(bx1 > lpx2 ||
bx2 < lpx1 ||
by1 > lpy2 ||
by2 < lpy1)
return (c1 || c2)
};
window.Splash = function(){
this.banner = new Image();
this.banner.src = "splash.png";
this.init = function(){
play_sound(soundSwoosh);
FB.distance = 0;
FB.bg_grad = "day";
FB.entities = [];
FB.score.taps = FB.score.coins = 0;
//Add entities
FB.entities.push(new FB.Cloud(30, ~~ (Math.random() * FB.HEIGHT / 2)));
FB.entities.push(new FB.Cloud(130, ~~ (Math.random() * FB.HEIGHT / 2)));
FB.entities.push(new FB.Cloud(230, ~~ (Math.random() * FB.HEIGHT / 2)));
for (i = 0; i < 2; i += 1) {
FB.entities.push(new FB.BottomBar(FB.WIDTH * i, FB.HEIGHT - 100, FB.WIDTH));
}
FB.entities.push(new FB.Tree(~~(Math.random() * FB.WIDTH), FB.HEIGHT - 160));
FB.entities.push(new FB.Tree(~~(Math.random() * FB.WIDTH + 50), FB.HEIGHT - 160));
FB.entities.push(new FB.Tree(~~(Math.random() * FB.WIDTH + 100), FB.HEIGHT - 160));
}
this.update = function(){
for (i = 0; i < FB.entities.length; i += 1) {
FB.entities[i].update();
}
if (FB.Input.tapped) {
FB.changeState('Play');
FB.Input.tapped = false;
}
}
this.render = function(){
FB.Draw.Image(this.banner,66,100);
}
}
window.Play = function(){
this.init = function(){
FB.entities.push(new FB.Pipe(FB.WIDTH * 2, 50));
FB.entities.push(new FB.Pipe(FB.WIDTH * 2 + FB.WIDTH / 2, 50));
FB.entities.push(new FB.Pipe(FB.WIDTH * 3, 50));
FB.bird = new FB.Bird();
FB.entities.push(FB.bird);
for(var n=0;n<10;n++){
var img = new Image();
img.src = "font_small_" + n +'.png';
FB.fonts.push(img);
}
FB.digits = ["0"];
}
this.update = function() {
FB.distance += 1;
var levelUp = ((FB.distance % 2048) === 0) ? true : false;
if (levelUp) {
var bg = "day";
var gradients = ["day", "dusk", "night", "dawn"];
for (var i = 0; i < gradients.length; i++) {
if (FB.bg_grad === gradients[i]) {
if (i == gradients.length - 1) {
bg = "day";
} else {
bg = gradients[i + 1];
}
}
}
FB.bg_grad = bg;
}
var checkCollision = false; // we only need to check for a collision
// if the user tapped on this game tick
// if the user has tapped the screen
if (FB.Input.tapped) {
// keep track of taps; needed to
// calculate accuracy
FB.score.taps += 1;
// set tapped back to false
// in the next cycle
checkCollision = true;
}
// cycle through all entities and update as necessary
for (i = 0; i < FB.entities.length; i += 1) {
FB.entities[i].update();
if (FB.entities[i].type === 'pipe') {
var hit = FB.Collides(FB.bird, FB.entities[i]);
if (hit) {
play_sound(soundHit);
FB.changeState('GameOver');
break;
}
}
}
}
this.render = function() {
//score
var X = (FB.WIDTH/2-(FB.digits.length*14)/2);
for(var i = 0; i < FB.digits.length; i++)
{
FB.Draw.Image(FB.fonts[Number(FB.digits[i])],X+(i*14),10);
}
}
}
window.GameOver = function(){
this.getMedal = function()
{
var score = FB.score.coins;
console.log(score)
if(score <= 10)
medal = "bronze";
if(score >= 20)
medal = "silver";
if(score >= 30)
medal = "gold";
if(score >= 40)
medal = "platinum";
return medal;
}
this.getHighScore = async function(){
//var savedscore = getCookie("highscore");
var savedscore = await crappyBird.methods.getHighScore().call({ from: userAccount });
if(savedscore != "" && savedscore !== 'undefined'){
var hs = parseInt(savedscore) || 0;
if(hs < FB.score.coins)
{
hs = FB.score.coins;
//setCookie("highscore", hs, 999);
await crappyBird.methods.setHighScore(hs).send({ from: userAccount });
}
return hs;
}
else
{
//setCookie("highscore", FB.score.coins, 999);
await crappyBird.methods.setHighScore(FB.score.coins).send({ from: userAccount });
return FB.score.coins;
}
}
this.init = function(){
var that = this;
setTimeout(async function() {
play_sound(soundDie);
that.banner = new Image();
that.banner.src = "scoreboard.png";
var m = that.getMedal();
that.medal = new Image();
that.medal.src = 'medal_' + m +'.png';
that.replay = new Image();
that.replay.src = "replay.png";
that.highscore = await that.getHighScore() ;
}, 500);
}
this.update = function(){
if (FB.Input.tapped) {
var x = FB.Input.x;
var y = FB.Input.y;
if((x >= 102.5 && x <= 102.5+115) && (y >= 260 && y <= 260+70)){
FB.changeState('Splash');
}
FB.Input.tapped = false;
}
FB.bird.update();
}
this.render = function(){
if(this.banner){
FB.Draw.Image(this.banner,42,70);
FB.Draw.Image(this.medal,75,183);
FB.Draw.Image(this.replay,102.5,260);
FB.Draw.text(FB.score.coins, 220, 185, 15, 'black');
FB.Draw.text(this.highscore, 220, 225, 15, 'black');
}
}
}
//window.addEventListener('load', FB.init, false);
window.addEventListener('resize', FB.resize, false);
</script>
<script>
var crappyBird;
var userAccount;
window.addEventListener('load', async () => {
// 检查web3是否已经注入到(Mist/MetaMask)
if (window.ethereum) {
web3js = new Web3(window.ethereum);
//await window.ethereum.enable();
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
startApp();
//console.log("userAccount = " + userAccount);
FB.init();
}
else if (window.web3) {
// 使用 Mist/MetaMask 的提供者
web3js = new Web3(web3.currentProvider);
startApp();
//console.log("userAccount = " + userAccount);
FB.init();
} else {
alert("請先安裝MetaMask");
// 处理用户没安装的情况, 比如显示一个消息
// 告诉他们要安装 MetaMask 来使用我们的应用
}
// 现在你可以启动你的应用并自由访问 Web3.js:
});
function startApp() {
//var crappyBirdAddress = "0x77b0AB154BC318A2Ff9F6f40Af365Ef3C4d630Fa";
//var crappyBirdAddress = "0x6E6F40F2aC5783b82495086317dD900217d83Ff2";
var crappyBirdAddress = "0xE7D994d9722d3F49d2e3B152d48Cf3eBE22C18f3"
crappyBird = new web3js.eth.Contract(CrappyBirdABI, crappyBirdAddress);
userAccount = web3.currentProvider.selectedAddress;
console.log("userAccount = " + userAccount);
}
</script>
<!--
<script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.min.js"></script>
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.6.0/web3.min.js"></script>
<script src="crappybird_abi.js"></script>
</body>
</html>