1+ var game = new Phaser . Game ( 400 , 490 , Phaser . AUTO , 'game_div' ) ;
2+ var game_state = { } ;
3+
4+ game_state . main = function ( ) { } ;
5+ game_state . main . prototype = {
6+
7+ preload : function ( ) {
8+ this . game . stage . backgroundColor = '#71c5cf' ;
9+ this . game . load . image ( 'bird' , 'assets/bird.png' ) ;
10+ this . game . load . image ( 'pipe' , 'assets/pipe.png' ) ;
11+ } ,
12+
13+ create : function ( ) {
14+ this . bird = this . game . add . sprite ( 100 , 245 , 'bird' ) ;
15+ this . bird . body . gravity . y = 1000 ;
16+
17+ var space_key = this . game . input . keyboard . addKey ( Phaser . Keyboard . SPACEBAR ) ;
18+ space_key . onDown . add ( this . jump , this ) ;
19+
20+ this . pipes = game . add . group ( ) ;
21+ this . pipes . createMultiple ( 20 , 'pipe' ) ;
22+ this . timer = this . game . time . events . loop ( 1500 , this . add_row_of_pipes , this ) ;
23+
24+ this . score = 0 ;
25+ var style = { font : "30px Arial" , fill : "#ffffff" } ;
26+ this . label_score = this . game . add . text ( 20 , 20 , "0" , style ) ;
27+ } ,
28+
29+ update : function ( ) {
30+ if ( this . bird . inWorld == false )
31+ this . restart_game ( ) ;
32+
33+ this . game . physics . overlap ( this . bird , this . pipes , this . restart_game , null , this ) ;
34+ } ,
35+
36+ jump : function ( ) {
37+ this . bird . body . velocity . y = - 350 ;
38+ } ,
39+
40+ restart_game : function ( bird , pipe ) {
41+ this . game . time . events . remove ( this . timer ) ;
42+ this . game . state . start ( 'main' ) ;
43+ } ,
44+
45+ add_one_pipe : function ( x , y ) {
46+ var pipe = this . pipes . getFirstDead ( ) ;
47+ pipe . reset ( x , y ) ;
48+ pipe . body . velocity . x = - 200 ;
49+ pipe . outOfBoundsKill = true ;
50+ } ,
51+
52+ add_row_of_pipes : function ( ) {
53+ var hole = Math . floor ( Math . random ( ) * 5 ) + 1 ;
54+
55+ for ( var i = 0 ; i < 8 ; i ++ )
56+ if ( i != hole && i != hole + 1 )
57+ this . add_one_pipe ( 400 , i * 60 + 10 ) ;
58+
59+ this . score += 1 ;
60+ this . label_score . content = this . score ;
61+ } ,
62+ } ;
63+
64+ game . state . add ( 'main' , game_state . main ) ;
65+ game . state . start ( 'main' ) ;
0 commit comments