-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_old.js
215 lines (138 loc) · 4.95 KB
/
main_old.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var game = new Phaser.Game(700,500,Phaser.AUTO,'game_div');
var c=0;
var userName =0 ;
var main_state={
preload:function(){
this.game.stage.backgroundColor='#0B0B61';
var text = "Loading....";
var text1 = "SpaceBar/MouseClick : Jump";
var style = { font: "30px Arial",fill:"#ffffff",align:"center"};
var style1 = { font: "20px Arial",fill:"#ffffff",align:"center"};
this.game.add.text(game.world.centerX-100,50,text,style);
this.game.add.text(game.world.centerX-120,150,text1,style1);
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');
this.game.load.audio('collide','assets/collide.wav');
},
create:function(){
// -----------------------------------------------------Working Code to resize Game
/*game.stage.scale.startFullScreen();
game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL; //resize your window to see the stage resize too
game.stage.scale.setShowAll();
game.stage.scale.refresh();*/
this.jump_sound = this.game.add.audio('jump');
this.collide_sound=this.game.add.audio('collide');
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(1500, this.add_row_of_pipes, this);
this.bird=this.game.add.sprite(150,250,'bird');
this.game.add.sprite(5,5,'score_image');
this.bird.body.gravity.y= 1000; //Orginal 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);
var space_key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
space_key.onDown.add(this.jump,this);
this.score = 0;
var style = {font:'20px Arial',fill:'#000000'};
this.label_score = this.game.add.text(50,45,"0",style);
},
update:function(){
this.ground.tilePosition.x -= 1.5;
if(c==0){
document.addEventListener("keydown",function(){ c++;
},false);
}
if(this.bird.inWorld==false)
{ this.display_score(); }
//this.restart_game(); }
if(this.bird.angle<10)
this.bird.angle+=1;
if (game.input.activePointer.isDown) {
game.input.onTap.addOnce(this.jump,this);
//this.touched = true;
//this.jump();
}
//this.ground.tilePosition.x -= 500;
this.game.physics.overlap(this.pipes,this.bird,this.hit_pipe,null,this); //Collision Detection
},
add_one_pipe:function(x,y){
var pipe = this.pipes.getFirstDead();
pipe.reset(x,y);
pipe.body.velocity.x = -150;
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;
},
hit_pipe:function(){
if(this.bird.alive=false)
return ;
var animation = this.game.add.tween(this.bird); //Add animation
animation.to({angle:+60},100);
animation.start();
this.game.time.events.remove(this.timer);
//this.game.time.events.remove(this.timer1);
this.pipes.forEachAlive(function(p){
p.body.velocity.x = 0;
}, this);
this.collide_sound.play();
//Sounds.collide.play();
},
jump:function()
{
if(this.bird.alive==false)
{
return;}
this.bird.body.velocity.y=-400; //Orginal 450
var animation = this.game.add.tween(this.bird); //Add animation
animation.to({angle:-20},100);
animation.start();
this.jump_sound.play();
},
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);
game.input.onTap.addOnce(this.restart_game,this);
},
high_score : function(){
if(localStorage.getItem("namo")<this.score)
localStorage.setItem("namo",this.score);
return localStorage.getItem("namo") ;
},
restart_game:function(){
this.game.time.events.remove(this.timer);
this.game.state.start('main');
},
};
game.state.add('main',main_state);
game.state.start('main');