-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
197 lines (145 loc) · 5.49 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
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
var game = new Phaser.Game(700,500,Phaser.AUTO,'game_div');
var score = 0 ;
name = 'amiteb' ;
var load_state={
preload:function(){
this.game.stage.backgroundColor='rbga(0,0,0,0.5)';
this.game.load.image('gameOver_image','assets/gameOver.png');
this.game.load.image('ground','assets/tile.png');
this.game.load.image('bird','assets/bird1.png');
this.game.load.image('background','assets/background.png');
this.game.load.image('background1','assets/background1.png');
this.game.load.image('score_image','assets/score.png');
this.game.load.image('pipe','assets/pipe.png');
this.game.load.audio('jump', 'assets/jump.wav');
// hero images
this.game.load.image('amiteb','images/amiteb.png');
this.game.load.image('akshey','images/akshey.png');
this.game.load.image('ameer','images/ameer.png');
this.game.load.image('catrina','images/catrina.png');
this.game.load.image('runvier','images/runvier.png');
this.game.load.image('sheruk','images/sheruk.png');
this.game.load.image('sulmen','images/sulmen.png');
this.game.load.image('sunni','images/sunni.png');
},
create:function(){
this.game.state.start('menu');
}
};
var menu_state={
create: function() {
var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
space_key.onDown.add(this.start, this);
var style = { font: "30px Arial", fill: "#ffffff" };
var x = game.world.width/2, y = game.world.height/2;
var text = this.game.add.text(x, y-50, "Press space to start", style);
text.anchor.setTo(0.5, 0.5);
if (score > 0) {
var score_label = this.game.add.text(x, y+50, "score: " + score, style);
score_label.anchor.setTo(0.5, 0.5);
}
},
start: function() {
this.game.state.start('play');
document.getElementById('leaderboard_score').style.display="none";
document.getElementById('leaderboard_text').style.display="none";
document.getElementById('leader_board').style.display="none";
}
};
var play_state = {
create: function() {
var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
space_key.onDown.add(this.jump, this);
this.game.add.sprite(5,5,'score_image');
var bgNum = Math.floor(Math.random()*2); //Load background randomly
if(bgNum==0){
this.game.add.sprite(0,0,'background');
}
else{
this.game.add.sprite(0,0,'background1');
}
this.pipes = game.add.group();
this.pipes.createMultiple(40, 'pipe');
this.timer = this.game.time.events.loop(1300, this.add_row_of_pipes, this);
this.bird = this.game.add.sprite(150, 245, name);
this.bird.body.gravity.y = 1000;
this.bird.anchor.setTo(-0.2, 0.5);
this.ground = game.add.tileSprite(0, game.world.height-64, game.world.width,64,'ground');
this.ground.tileScale.setTo(1, 1);
this.game.add.sprite(5,5,'score_image');
this.score = 0;
var style = {font:'20px Arial',fill:'#000000'};
this.label_score = this.game.add.text(50,45,"0",style);
this.jump_sound = this.game.add.audio('jump');
},
update: function() {
if (this.bird.inWorld == false)
this.display_score();
if (this.bird.angle < 20)
this.bird.angle += 1;
this.ground.tilePosition.x -= 1.5;
this.game.physics.overlap(this.bird, this.pipes, this.hit_pipe, null, this);
},
jump: function() {
if (this.bird.alive == false)
return;
this.bird.body.velocity.y = -350;
this.game.add.tween(this.bird).to({angle: -20}, 100).start();
this.jump_sound.play();
},
hit_pipe: function() {
if (this.bird.alive == false)
return;
this.bird.alive = false;
this.game.time.events.remove(this.timer);
this.pipes.forEachAlive(function(p){
p.body.velocity.x = 0;
}, this);
},
restart_game: function() {
this.game.time.events.remove(this.timer);
// This time we go back to the 'menu' state
this.game.state.start('menu');
},
add_one_pipe: function(x, y) {
var pipe = this.pipes.getFirstDead();
pipe.reset(x, y);
pipe.body.velocity.x = -200;
pipe.outOfBoundsKill = true;
},
add_row_of_pipes:function(){
var hole=Math.floor(Math.random()*5)+1;
for(var i=0;i<=8;i++)
{
if (i != hole && i != hole +1 && i!=hole-1)
this.add_one_pipe(800,i*60 );
}
this.score+=1;
this.label_score.content= this.score;
},
display_score:function(){
this.game.time.events.remove(this.timer);
this.pipes.forEachAlive(function(p){
p.body.velocity.x = 0;
}, this)
this.ground.tilePosition.x =0;
var text = " : "+this.score;
var text_1 = " : "+ this.high_score();
var style = { font: "25px Arial",fill:"#08088A",align:"center"};
this.game.add.sprite(200,150,'gameOver_image');
this.game.add.text(420,220,text,style);
this.game.add.text(420,260,text_1,style);
var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
space_key.onDown.add(this.restart_game, this);
},
high_score : function(){
//if(localStorage.getItem("namo")<this.score)
localStorage.setItem("namo",this.score);
score= parseInt(localStorage.getItem("namo"));
return localStorage.getItem("namo") ;
},
};
game.state.add('load',load_state);
game.state.add('menu',menu_state);
game.state.add('play',play_state);
game.state.start('load');