@@ -4,32 +4,46 @@ var Breakout = new Phaser.Class({
4
4
5
5
initialize :
6
6
7
- function Breakout ( )
8
- {
9
- Phaser . Scene . call ( this , { key : 'breakout' } ) ;
10
-
11
- this . bricks ;
12
- this . paddle ;
13
- this . ball ;
14
- this . score = 0 ;
15
- this . scoreText ;
16
- } ,
17
-
18
- preload : function ( )
19
- {
7
+ function Breakout ( ) {
8
+ Phaser . Scene . call ( this , {
9
+ key : 'breakout'
10
+ } ) ;
11
+
12
+ this . bricks ;
13
+ this . paddle ;
14
+ this . ball ;
15
+
16
+ // initialize score and lives counters
17
+ this . score = 0 ;
18
+ this . lives = 3 ;
19
+
20
+ // display score and lives status to user
21
+ this . scoreText ;
22
+ this . livesText ;
23
+ this . introText ;
24
+ } ,
25
+
26
+ preload : function ( ) {
20
27
this . load . atlas ( 'assets' , 'assets/games/breakout/breakout.png' , 'assets/games/breakout/breakout.json' ) ;
21
28
} ,
22
29
23
- create : function ( )
24
- {
30
+ create : function ( ) {
25
31
// Enable world bounds, but disable the floor
26
32
this . physics . world . setBoundsCollision ( true , true , true , false ) ;
27
33
28
34
// Create the bricks in a 10x6 grid
29
35
this . bricks = this . physics . add . staticGroup ( {
30
- key : 'assets' , frame : [ 'blue1' , 'red1' , 'green1' , 'yellow1' , 'silver1' , 'purple1' ] ,
36
+ key : 'assets' ,
37
+ frame : [ 'blue1' , 'red1' , 'green1' , 'yellow1' , 'silver1' , 'purple1' ] ,
31
38
frameQuantity : 10 ,
32
- gridAlign : { width : 10 , height : 6 , cellWidth : 64 , cellHeight : 32 , x : 112 , y : 100 }
39
+ gridAlign : {
40
+ width : 10 ,
41
+ height : 6 ,
42
+ cellWidth : 64 ,
43
+ cellHeight : 32 ,
44
+ x : 112 ,
45
+ y : 100
46
+ }
33
47
} ) ;
34
48
35
49
this . ball = this . physics . add . image ( 400 , 500 , 'assets' , 'ball1' ) . setCollideWorldBounds ( true ) . setBounce ( 1 ) ;
@@ -42,91 +56,125 @@ var Breakout = new Phaser.Class({
42
56
this . physics . add . collider ( this . ball , this . paddle , this . hitPaddle , null , this ) ;
43
57
44
58
// Input events
45
- this . input . on ( 'pointermove' , function ( pointer ) {
59
+ this . input . on ( 'pointermove' , function ( pointer ) {
46
60
47
61
// Keep the paddle within the game
48
62
this . paddle . x = Phaser . Math . Clamp ( pointer . x , 52 , 748 ) ;
49
63
50
- if ( this . ball . getData ( 'onPaddle' ) )
51
- {
64
+ if ( this . ball . getData ( 'onPaddle' ) ) {
52
65
this . ball . x = this . paddle . x ;
53
66
}
54
67
55
68
} , this ) ;
56
69
57
- this . input . on ( 'pointerup' , function ( pointer ) {
70
+ this . input . on ( 'pointerup' , function ( pointer ) {
58
71
59
- if ( this . ball . getData ( 'onPaddle' ) )
60
- {
72
+ if ( this . ball . getData ( 'onPaddle' ) ) {
61
73
this . ball . setVelocity ( - 75 , - 300 ) ;
62
74
this . ball . setData ( 'onPaddle' , false ) ;
75
+
76
+ // player started game so disable into text
77
+ this . introText . visible = false ;
78
+ }
79
+
80
+ // If starting a new game initialize lives to 3
81
+ if ( this . lives == 0 ) {
82
+ this . lives = 3 ;
83
+ this . livesText . setText ( 'Lives: ' + this . lives ) ;
63
84
}
64
85
65
86
} , this ) ;
66
-
67
- // Score components
68
- this . scoreText = this . add . text ( 32 , 550 , 'Score: 0' , { font : "20px Arial" , fill : "#ffffff" , align : "left" } ) ;
87
+
88
+ // Display current score
89
+ this . scoreText = this . add . text ( 32 , 550 , 'Score: 0' , {
90
+ font : "20px Arial" ,
91
+ fill : "#ffffff" ,
92
+ align : "left"
93
+ } ) ;
94
+
95
+ // Available lives status display
96
+ this . livesText = this . add . text ( 680 , 550 , 'Lives: 3' , {
97
+ font : "20px Arial" ,
98
+ fill : "#ffffff" ,
99
+ align : "left"
100
+ } ) ;
101
+
102
+ // Game Intro/Game over status display
103
+ this . introText = this . add . text ( window . innerWidth / 2 , 400 , 'Click To Start' , {
104
+ font : "40px Arial" ,
105
+ fill : "#ffffff" ,
106
+ align : "center"
107
+ } ) ;
108
+ this . introText . setOrigin ( 0.5 , 0.5 ) ;
69
109
} ,
70
110
71
- hitBrick : function ( ball , brick )
72
- {
73
- this . score += 10 ;
74
- this . scoreText . setText ( "Score: " + this . score ) ;
75
-
76
- brick . disableBody ( true , true ) ;
111
+ hitBrick : function ( ball , brick ) {
112
+ this . score += 10 ;
113
+ this . scoreText . setText ( "Score: " + this . score ) ;
114
+
115
+ brick . disableBody ( true , true ) ;
77
116
78
- if ( this . bricks . countActive ( ) === 0 )
79
- {
117
+ if ( this . bricks . countActive ( ) === 0 ) {
80
118
this . resetLevel ( ) ;
81
119
}
82
120
} ,
83
121
84
- resetBall : function ( )
85
- {
122
+ ballLost : function ( ) {
123
+
124
+ this . lives -- ;
125
+ this . livesText . setText ( 'Lives: ' + this . lives ) ;
126
+
127
+ if ( this . lives === 0 ) {
128
+ this . gameOver ( ) ;
129
+ }
130
+ } ,
131
+
132
+ gameOver : function ( ) {
133
+
134
+ this . ball . body . velocity . setTo ( 0 , 0 ) ;
135
+
136
+ this . introText . text = 'Game Over!' ;
137
+ this . introText . visible = true ;
138
+
139
+ } ,
140
+
141
+ resetBall : function ( ) {
86
142
this . ball . setVelocity ( 0 ) ;
87
143
this . ball . setPosition ( this . paddle . x , 500 ) ;
88
144
this . ball . setData ( 'onPaddle' , true ) ;
89
145
} ,
90
146
91
- resetLevel : function ( )
92
- {
147
+ resetLevel : function ( ) {
93
148
this . resetBall ( ) ;
94
149
95
- this . bricks . children . each ( function ( brick ) {
150
+ this . bricks . children . each ( function ( brick ) {
96
151
97
152
brick . enableBody ( false , 0 , 0 , true , true ) ;
98
153
99
154
} ) ;
100
155
} ,
101
156
102
- hitPaddle : function ( ball , paddle )
103
- {
157
+ hitPaddle : function ( ball , paddle ) {
104
158
var diff = 0 ;
105
159
106
- if ( ball . x < paddle . x )
107
- {
160
+ if ( ball . x < paddle . x ) {
108
161
// Ball is on the left-hand side of the paddle
109
162
diff = paddle . x - ball . x ;
110
163
ball . setVelocityX ( - 10 * diff ) ;
111
- }
112
- else if ( ball . x > paddle . x )
113
- {
164
+ } else if ( ball . x > paddle . x ) {
114
165
// Ball is on the right-hand side of the paddle
115
- diff = ball . x - paddle . x ;
166
+ diff = ball . x - paddle . x ;
116
167
ball . setVelocityX ( 10 * diff ) ;
117
- }
118
- else
119
- {
168
+ } else {
120
169
// Ball is perfectly in the middle
121
170
// Add a little random X to stop it bouncing straight up!
122
171
ball . setVelocityX ( 2 + Math . random ( ) * 8 ) ;
123
172
}
124
173
} ,
125
174
126
- update : function ( )
127
- {
128
- if ( this . ball . y > 600 )
129
- {
175
+ update : function ( ) {
176
+ if ( this . ball . y > 600 ) {
177
+ this . ballLost ( ) ;
130
178
this . resetBall ( ) ;
131
179
}
132
180
}
@@ -138,7 +186,7 @@ var config = {
138
186
width : 800 ,
139
187
height : 600 ,
140
188
parent : 'phaser-example' ,
141
- scene : [ Breakout ] ,
189
+ scene : [ Breakout ] ,
142
190
physics : {
143
191
default : 'arcade'
144
192
}
0 commit comments