-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
120 lines (102 loc) · 3.4 KB
/
main.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
var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create,update: update , render: render });
function preload () {
game.load.image('circle', 'assets/circle.png');
game.load.image('block', 'assets/earth.png');
// Firefox does not support MP3 Files so we will use OGG for firefox
game.load.audio('failMusic', ['assets/failMusic.mp3', 'assets/failMusic.ogg']);
game.load.audio('mainMusic', ['assets/mainMusic.mp3', 'assets/mainMusic.ogg']);
};
function create (){
this.status = 'play';
game.stage.backgroundColor = '#fff';
game.physics.startSystem(Phaser.Physics.ARCADE);
this.circle = game.add.sprite(40, (window.innerHeight / 2) - 20, 'circle');
game.physics.enable(this.circle);
// load main music and play it
this.failMusic = game.add.audio('this.failMusic');
this.mainMusic = game.add.audio('this.mainMusic');
this.mainMusic.play();
this.blocks = game.add.group();
this.velo = -100;
this.count = 1;
this.timer = game.time.events.loop(1000, () => {
var randomC = (Math.random() * (this.count - 1)) + 1;
for (var i = 1; i <= randomC; i++){
var randomY = (Math.random() * (window.innerWidth - 25));
this.block = game.add.sprite(window.innerWidth, randomY, 'block');
game.physics.enable(this.block);
this.block.body.velocity.x = this.velo - 50;
this.velo -= 5;
this.blocks.add(this.block);
}
this.count += 1;
this.score += 1;
this.labelScore.text = this.score;
}, this);
this.score = 0;
this.labelScore = game.add.text(20, 20, "0",
{ font: "30px Arial", fill: "#000" });
setInterval(() => {
if (this.status == 'play'){
this.mainMusic.play();
}
}, 177000);
setInterval(() => {
if (this.status == 'loose'){
this.failMusic.play();
}
}, 46000);
upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
upKey.onDown.add(upKeyFunc, this);
upKey.onUp.add(upKeyFuncUp, this);
downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
downKey.onDown.add(downKeyFunc, this);
downKey.onUp.add(downKeyFuncUp, this);
};
function upKeyFunc(){
this.circle.body.velocity.y = -300;
}
function downKeyFunc(){
this.circle.body.velocity.y = +300;
}
function upKeyFuncUp(){
this.circle.body.velocity.y = 0;
}
function downKeyFuncUp(){
this.circle.body.velocity.y = 0;
}
function update(){
if (this.circle.y < 0 || this.circle.y > window.innerHeight){
this.failMusic.play();
this.status = 'loose';
alert('Really?! No cheat, that is our rule!! Play again?');
this.mainMusic.play();
this.status = 'play';
this.count = 0;
this.velo = -100;
this.blocks.removeAll();
this.score = 0;
this.circle.body.velocity.y = 0;
this.circle.position.x = 40;
this.circle.position.y = (window.innerHeight / 2) - 20;
}
game.physics.arcade.overlap(
this.circle, this.blocks, restartGame, null, this);
}
function restartGame(){
this.failMusic.play();
this.status = 'loose';
alert('Really?! You got only '+this.score+' ! Play again?');
this.mainMusic.play();
this.status = 'play';
this.count = 0;
this.velo = -100;
this.blocks.removeAll();
this.score = 0;
this.circle.body.velocity.y = 0;
this.circle.position.x = 40;
this.circle.position.y = (window.innerHeight / 2) - 20;
}
function render() {
game.debug.text("Next Attack In: " + game.time.events.duration.toFixed(0), 32, 32 );
}