Certainly! Below is a simple implementation of the classic Pong game using Phaser 3.88. This example includes two paddles (player and AI), a ball, basic collision, scoring, and simple AI movement.
How to run:
Include Phaser 3.88 in your HTML.
Add this JavaScript code.
Open in a browser.
Complete Pong Game Code (Phaser 3.88)
js
// Phaser 3.88 Pong Game
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#000000',
physics: {
default: 'arcade',
arcade: {
debug: false,
fps: 60
}
},
scene: {
preload,
create,
update
}
};
const game = new Phaser.Game(config);
let playerPaddle;
let aiPaddle;
let ball;
let cursors;
let playerScore = 0;
let aiScore = 0;
let playerScoreText;
let aiScoreText;
let ballSpeed = 300;
function preload() {
// No assets needed, we'll use graphics
}
function create() {
// Create player paddle (left)
playerPaddle = this.add.rectangle(50, this.cameras.main.height / 2, 20, 100, 0xffffff);
this.physics.add.existing(playerPaddle, true); // static body
// Create AI paddle (right)
aiPaddle = this.add.rectangle(this.cameras.main.width - 50, this.cameras.main.height / 2, 20, 100, 0xffffff);
this.physics.add.existing(aiPaddle, true); // static body
// Create ball
ball = this.add.circle(this.cameras.main.width / 2, this.cameras.main.height / 2, 10, 0xffffff);
this.physics.add.existing(ball);
ball.body.setCollideWorldBounds(true, 1, 1);
ball.body.setBounce(1, 1);
// Start ball moving in a random direction
resetBall.call(this);
// Enable cursor keys for player paddle
cursors = this.input.keyboard.createCursorKeys();
// Add collision between ball and paddles
this.physics.add.collider(ball, playerPaddle, hitPaddle, null, this);
this.physics.add.collider(ball, aiPaddle, hitPaddle, null, this);
// Add world bounds collision with callback for scoring
ball.body.onWorldBounds = true;
this.physics.world.on('worldbounds', (body, up, down, left, right) => {
if (left) {
// AI scores
aiScore++;
updateScoreText();
resetBall.call(this, 1);
} else if (right) {
// Player scores
playerScore++;
updateScoreText();
resetBall.call(this, -1);
}
});
// Score text
playerScoreText = this.add.text(200, 20, 'Player: 0', { fontSize: '32px', fill: '#fff' });
aiScoreText = this.add.text(520, 20, 'AI: 0', { fontSize: '32px', fill: '#fff' });
}
function update() {
// Player paddle movement
if (cursors.up.isDown) {
playerPaddle.y -= 10;
} else if (cursors.down.isDown) {
playerPaddle.y += 10;
}
// Keep player paddle inside screen
playerPaddle.y = Phaser.Math.Clamp(playerPaddle.y, playerPaddle.height / 2, this.cameras.main.height - playerPaddle.height / 2);
playerPaddle.body.updateFromGameObject();
// Simple AI paddle movement: follow the ball with some speed limit
if (ball.y < aiPaddle.y) {
aiPaddle.y -= 6;
} else if (ball.y > aiPaddle.y) {
aiPaddle.y += 6;
}
aiPaddle.y = Phaser.Math.Clamp(aiPaddle.y, aiPaddle.height / 2, this.cameras.main.height - aiPaddle.height / 2);
aiPaddle.body.updateFromGameObject();
}
function hitPaddle(ballObj, paddleObj) {
// Increase ball speed slightly on each paddle hit
ballSpeed *= 1.05;
// Calculate new velocity based on where the ball hits the paddle
let diff = ballObj.y - paddleObj.y;
let normalizedDiff = diff / (paddleObj.height / 2);
// Reverse X velocity and add some Y velocity based on hit position
ballObj.body.setVelocityX(-ballObj.body.velocity.x);
ballObj.body.setVelocityY(ballSpeed * normalizedDiff);
}
function resetBall(direction = Phaser.Math.Between(0, 1) ? 1 : -1) {
ballSpeed = 300;
ball.setPosition(this.cameras.main.width / 2, this.cameras.main.height / 2);
ball.body.setVelocity(0);
// Delay before launching ball
this.time.delayedCall(1000, () => {
// Launch ball in random Y direction, X direction based on who scored last
let angle = Phaser.Math.Between(-30, 30);
let velocityX = ballSpeed * direction;
let velocityY = ballSpeed * Math.sin(Phaser.Math.DegToRad(angle));
ball.body.setVelocity(velocityX, velocityY);
});
}
function updateScoreText() {
playerScoreText.setText('Player: ' + playerScore);
aiScoreText.setText('AI: ' + aiScore);
}
Explanation:
Player paddle controlled by up/down arrow keys.
AI paddle follows the ball with a fixed speed.
Ball bounces off paddles and world bounds.
Scores update when ball passes left or right edge.
Ball speed increases slightly on paddle hits.
Ball resets to center after a score with a short delay.
If you want me to help you embed this in an HTML file or add features like sound, multiplayer, or mobile controls, just ask!