-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
163 lines (133 loc) · 4.1 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Breakout</title>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
<style type="text/css">
body { margin: 0; }
</style>
</head>
<body>
<script type="text/javascript">
var Breakout = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function Breakout ()
{
Phaser.Scene.call(this, { key: 'breakout' });
this.bricks;
this.paddle;
this.ball;
this.score = 0;
},
preload: function ()
{
this.load.image('doomfire', 'assets/doomfire.png');
this.load.atlas('assets', 'assets/breakout.png', 'assets/breakout.json');
},
create: function ()
{
// A simple background for our game
this.add.image(400, 240, 'doomfire');
scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#ed9814' });
// Enable world bounds, but disable the floor
this.physics.world.setBoundsCollision(true, true, true, false);
// Create the bricks in a 10x6 grid
this.bricks = this.physics.add.staticGroup({
key: 'assets', frame: [ 'blue1', 'red1', 'green1', 'yellow1', 'silver1', 'purple1' ],
frameQuantity: 10,
gridAlign: { width: 10, height: 6, cellWidth: 64, cellHeight: 32, x: 112, y: 100 }
});
this.ball = this.physics.add.image(400, 400, 'assets', 'ball1').setCollideWorldBounds(true).setBounce(1);
this.ball.setData('onPaddle', true);
this.paddle = this.physics.add.image(400, 450, 'assets', 'paddle1').setImmovable();
// Our colliders
this.physics.add.collider(this.ball, this.bricks, this.hitBrick, null, this);
this.physics.add.collider(this.ball, this.paddle, this.hitPaddle, null, this);
// Input events
this.input.on('pointermove', function (pointer) {
// Keep the paddle within the game
this.paddle.x = Phaser.Math.Clamp(pointer.x, 52, 748);
if (this.ball.getData('onPaddle'))
{
this.ball.x = this.paddle.x;
}
}, this);
this.input.on('pointerup', function (pointer) {
if (this.ball.getData('onPaddle'))
{
this.ball.setVelocity(-75, -300);
this.ball.setData('onPaddle', false);
}
}, this);
},
hitBrick: function (ball, brick)
{
brick.disableBody(true, true);
this.score += 1;
scoreText.setText('score: ' + this.score);
if (this.bricks.countActive() === 0)
{
this.resetLevel();
}
},
resetBall: function ()
{
this.score = 0;
scoreText.setText('score: ' + this.score);
this.ball.setVelocity(0);
this.ball.setPosition(this.paddle.x, 400);
this.ball.setData('onPaddle', true);
},
resetLevel: function ()
{
this.resetBall();
this.bricks.children.each(function (brick) {
brick.enableBody(false, 0, 0, true, true);
});
},
hitPaddle: function (ball, paddle)
{
var diff = 0;
if (ball.x < paddle.x)
{
// Ball is on the left-hand side of the paddle
diff = paddle.x - ball.x;
ball.setVelocityX(-10 * diff);
}
else if (ball.x > paddle.x)
{
// Ball is on the right-hand side of the paddle
diff = ball.x -paddle.x;
ball.setVelocityX(10 * diff);
}
else
{
// Ball is perfectly in the middle
// Add a little random X to stop it bouncing straight up!
ball.setVelocityX(2 + Math.random() * 8);
}
},
update: function ()
{
if (this.ball.y > 480)
{
this.resetLevel();
}
}
});
var config = {
type: Phaser.WEBGL,
width: 800,
height: 480,
parent: 'breakout',
scene: [ Breakout ],
physics: {
default: 'arcade'
}
};
var game = new Phaser.Game(config);
</script>
</body>
</html>