Skip to content

Commit 306ef31

Browse files
addedintro text and player available lives
1 parent b78984f commit 306ef31

File tree

1 file changed

+103
-55
lines changed

1 file changed

+103
-55
lines changed

public/src/games/breakout/breakout.js

Lines changed: 103 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,46 @@ var Breakout = new Phaser.Class({
44

55
initialize:
66

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() {
2027
this.load.atlas('assets', 'assets/games/breakout/breakout.png', 'assets/games/breakout/breakout.json');
2128
},
2229

23-
create: function ()
24-
{
30+
create: function() {
2531
// Enable world bounds, but disable the floor
2632
this.physics.world.setBoundsCollision(true, true, true, false);
2733

2834
// Create the bricks in a 10x6 grid
2935
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'],
3138
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+
}
3347
});
3448

3549
this.ball = this.physics.add.image(400, 500, 'assets', 'ball1').setCollideWorldBounds(true).setBounce(1);
@@ -42,91 +56,125 @@ var Breakout = new Phaser.Class({
4256
this.physics.add.collider(this.ball, this.paddle, this.hitPaddle, null, this);
4357

4458
// Input events
45-
this.input.on('pointermove', function (pointer) {
59+
this.input.on('pointermove', function(pointer) {
4660

4761
// Keep the paddle within the game
4862
this.paddle.x = Phaser.Math.Clamp(pointer.x, 52, 748);
4963

50-
if (this.ball.getData('onPaddle'))
51-
{
64+
if (this.ball.getData('onPaddle')) {
5265
this.ball.x = this.paddle.x;
5366
}
5467

5568
}, this);
5669

57-
this.input.on('pointerup', function (pointer) {
70+
this.input.on('pointerup', function(pointer) {
5871

59-
if (this.ball.getData('onPaddle'))
60-
{
72+
if (this.ball.getData('onPaddle')) {
6173
this.ball.setVelocity(-75, -300);
6274
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);
6384
}
6485

6586
}, 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);
69109
},
70110

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);
77116

78-
if (this.bricks.countActive() === 0)
79-
{
117+
if (this.bricks.countActive() === 0) {
80118
this.resetLevel();
81119
}
82120
},
83121

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() {
86142
this.ball.setVelocity(0);
87143
this.ball.setPosition(this.paddle.x, 500);
88144
this.ball.setData('onPaddle', true);
89145
},
90146

91-
resetLevel: function ()
92-
{
147+
resetLevel: function() {
93148
this.resetBall();
94149

95-
this.bricks.children.each(function (brick) {
150+
this.bricks.children.each(function(brick) {
96151

97152
brick.enableBody(false, 0, 0, true, true);
98153

99154
});
100155
},
101156

102-
hitPaddle: function (ball, paddle)
103-
{
157+
hitPaddle: function(ball, paddle) {
104158
var diff = 0;
105159

106-
if (ball.x < paddle.x)
107-
{
160+
if (ball.x < paddle.x) {
108161
// Ball is on the left-hand side of the paddle
109162
diff = paddle.x - ball.x;
110163
ball.setVelocityX(-10 * diff);
111-
}
112-
else if (ball.x > paddle.x)
113-
{
164+
} else if (ball.x > paddle.x) {
114165
// Ball is on the right-hand side of the paddle
115-
diff = ball.x -paddle.x;
166+
diff = ball.x - paddle.x;
116167
ball.setVelocityX(10 * diff);
117-
}
118-
else
119-
{
168+
} else {
120169
// Ball is perfectly in the middle
121170
// Add a little random X to stop it bouncing straight up!
122171
ball.setVelocityX(2 + Math.random() * 8);
123172
}
124173
},
125174

126-
update: function ()
127-
{
128-
if (this.ball.y > 600)
129-
{
175+
update: function() {
176+
if (this.ball.y > 600) {
177+
this.ballLost();
130178
this.resetBall();
131179
}
132180
}
@@ -138,7 +186,7 @@ var config = {
138186
width: 800,
139187
height: 600,
140188
parent: 'phaser-example',
141-
scene: [ Breakout ],
189+
scene: [Breakout],
142190
physics: {
143191
default: 'arcade'
144192
}

0 commit comments

Comments
 (0)