-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakout.html
272 lines (231 loc) · 9.64 KB
/
breakout.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Atari Breakout with Phaser - Enhanced Version</title>
<script src="https://cdn.jsdelivr.net/npm/phaser@3/dist/phaser.min.js"></script>
</head>
<body>
<script>
var config = {
type: Phaser.AUTO,
width: 850,
height: 600,
backgroundColor: '#2d2d2d',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
var paddle;
var ball;
var bricks;
var cursors;
var score = 0;
var scoreText;
var lives = 3;
var livesText;
var gameStarted = false;
var gameOver = false;
var gameOverText;
var ballInitialSpeed = 250; // Increased initial speed
var paddleSpeed = 500; // Increased paddle speed
function preload() {
// Create paddle image
var paddleGraphics = this.make.graphics({ x: 0, y: 0, add: false });
paddleGraphics.fillStyle(0xffffff, 1);
paddleGraphics.fillRect(0, 0, 100, 20);
paddleGraphics.generateTexture('paddle', 100, 20);
// Create ball image
var ballGraphics = this.make.graphics({ x: 0, y: 0, add: false });
ballGraphics.fillStyle(0xffffff, 1);
ballGraphics.fillCircle(10, 10, 10);
ballGraphics.generateTexture('ball', 20, 20);
// Create brick images
this.brickColors = [0xff0000, 0xffff00, 0x00ff00, 0x0000ff];
this.brickWidth = 50;
this.brickHeight = 20;
for (var i = 0; i < this.brickColors.length; i++) {
var color = this.brickColors[i];
var brickGraphics = this.make.graphics({ x: 0, y: 0, add: false });
brickGraphics.fillStyle(color, 1);
brickGraphics.fillRect(0, 0, this.brickWidth, this.brickHeight);
brickGraphics.generateTexture('brick' + i, this.brickWidth, this.brickHeight);
}
}
function create() {
// Create the paddle
paddle = this.physics.add.sprite(config.width / 2, config.height - 50, 'paddle').setImmovable();
paddle.body.allowGravity = false;
paddle.setCollideWorldBounds(true);
// Create the ball
ball = this.physics.add.sprite(config.width / 2, config.height - 70, 'ball');
ball.setCollideWorldBounds(true);
ball.setBounce(1);
ball.body.onWorldBounds = true;
// Create the bricks
bricks = this.physics.add.staticGroup();
var offsetX = 65;
var offsetY = 50;
var cols = 14;
var rows = 4;
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
var brickX = offsetX + col * (this.brickWidth + 5);
var brickY = offsetY + row * (this.brickHeight + 5);
var brick = bricks.create(brickX, brickY, 'brick' + row).setImmovable();
}
}
// Add collision between ball and paddle
this.physics.add.collider(ball, paddle, hitPaddle, null, this);
// Add collision between ball and bricks
this.physics.add.collider(ball, bricks, hitBrick, null, this);
// Enable cursor keys
cursors = this.input.keyboard.createCursorKeys();
// Display score and lives
scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '20px', fill: '#ffffff' });
livesText = this.add.text(700, 16, 'Lives: 3', { fontSize: '20px', fill: '#ffffff' });
// Input handling for spacebar
this.input.keyboard.on('keydown-SPACE', function () {
if (gameOver) {
// Restart the game
this.scene.restart();
score = 0;
lives = 3;
gameOver = false;
gameStarted = false;
} else if (!gameStarted) {
// Start the game
ball.setVelocity(Phaser.Math.Between(-ballInitialSpeed, ballInitialSpeed), -ballInitialSpeed);
gameStarted = true;
if (gameOverText) {
gameOverText.destroy();
}
if (instructionText) {
instructionText.destroy();
}
}
}, this);
}
function update() {
// Move paddle
if (cursors.left.isDown) {
paddle.setVelocityX(-paddleSpeed);
} else if (cursors.right.isDown) {
paddle.setVelocityX(paddleSpeed);
} else {
paddle.setVelocityX(0);
}
// Keep paddle within bounds
if (paddle.x < paddle.width / 2) {
paddle.x = paddle.width / 2;
} else if (paddle.x > config.width - paddle.width / 2) {
paddle.x = config.width - paddle.width / 2;
}
// Ball follows paddle before game starts
if (!gameStarted) {
ball.setX(paddle.x);
ball.setY(paddle.y - 20);
}
// Check for ball going out of bounds
if (ball.y > (config.height - 20)) {
loseLife.call(this);
}
}
function hitPaddle(ball, paddle) {
// Calculate difference between ball and paddle
var diff = 0;
if (ball.x < paddle.x) {
// Ball is on the left-hand side of paddle
diff = paddle.x - ball.x;
ball.body.velocity.x = (-10 * diff);
} else if (ball.x > paddle.x) {
// Ball is on the right-hand side of paddle
diff = ball.x - paddle.x;
ball.body.velocity.x = (10 * diff);
} else {
// Ball is perfectly in the middle
// Add a little random to prevent straight vertical bounce
ball.body.velocity.x = 2 + Math.random() * 8;
}
// Ensure ball's velocity.y is upward
ball.body.velocity.y = -Math.abs(ball.body.velocity.y);
}
function hitBrick(ball, brick) {
brick.disableBody(true, true);
score += 10;
scoreText.setText('Score: ' + score);
// Check if all bricks are destroyed
if (bricks.countActive() === 0) {
// Won the game
gameOver = true;
gameStarted = false;
ball.disableBody(true, true);
gameOverText = this.add.text(config.width / 2, config.height / 2, 'YOU WON!\nClick to Restart', { fontSize: '40px', fill: '#ffffff', align: 'center' });
gameOverText.setOrigin(0.5);
}
}
function loseLife() {
lives--;
livesText.setText('Lives: ' + lives);
// Make the paddle go red
paddle.setTint(0xff0000);
// Define start and end colors for the tween
var startColor = Phaser.Display.Color.ValueToColor(0xff0000);
var endColor = Phaser.Display.Color.ValueToColor(0xffffff);
if (lives > 0) {
gameStarted = false;
ball.setVelocity(0);
ball.setPosition(paddle.x, paddle.y - 20);
// After 1 second, fade the paddle back to white
this.time.delayedCall(1000, function () {
this.tweens.add({
targets: { t: 0 },
t: 1,
duration: 1000, // Duration of the fade effect (1 second)
onUpdate: function (tween, target) {
var t = target.t;
// Interpolate between red and white
var colorObject = Phaser.Display.Color.Interpolate.ColorWithColor(startColor, endColor, 1, t);
// Convert the interpolated color to a hex value
var color = Phaser.Display.Color.GetColor(colorObject.r, colorObject.g, colorObject.b);
// Set the paddle's tint to the interpolated color
paddle.setTint(color);
},
onComplete: function () {
// Clear the tint after the fade effect is complete
paddle.clearTint();
},
onCompleteScope: this
});
}, [], this);
} else {
// Game over
gameOver = true;
gameStarted = false;
ball.disableBody(true, true);
// Display "GAME OVER" message
gameOverText = this.add.text(config.width / 2, config.height / 2, 'GAME OVER\nClick to Restart', {
fontSize: '40px',
fill: '#ffffff',
align: 'center'
});
gameOverText.setOrigin(0.5);
// Clear the paddle tint when the game is over
paddle.clearTint();
}
}
</script>
<h1>Breakout</h1>
<p>Use <b>Spacebar</b> to release the ball, and <b>Left</b> + <b>Right</b> to control the paddle.</p>
</body>
</html>