<?php
// Set up game parameters
$paddleWidth = 10;
$paddleHeight = 100;
$ballSize = 10;
// Create canvas
$canvasWidth = 400;
$canvasHeight = 200;
echo "<canvas id='pong' width='$canvasWidth' height='$canvasHeight'></canvas>";
// Paddle positions
$paddle1Y = $canvasHeight/2 - $paddleHeight/2;
$paddle2Y = $canvasHeight/2 - $paddleHeight/2;
// Ball position
$ballX = $canvasWidth/2;
$ballY = $canvasHeight/2;
$ballSpeedX = 5;
$ballSpeedY = 3;
// Main game loop
while (true) {
// Move ball
$ballX += $ballSpeedX;
$ballY += $ballSpeedY;
// Bounce off top and bottom
if ($ballY <= 0 || $ballY >= $canvasHeight) {
$ballSpeedY = -$ballSpeedY;
}
// Draw scene
echo "<script>
var ctx = document.getElementById('pong').getContext('2d');
ctx.clearRect(0, 0, $canvasWidth, $canvasHeight);
ctx.fillStyle = 'white';
// Draw ball
ctx.beginPath();
ctx.arc($ballX, $ballY, $ballSize, 0, 2 * Math.PI);
ctx.fill();
// Draw paddles
ctx.fillRect(0, $paddle1Y, $paddleWidth, $paddleHeight);
ctx.fillRect($canvasWidth - $paddleWidth, $paddle2Y, $paddleWidth, $paddleHeight);
</script>";
usleep(10000); // Sleep for 10ms
}
?>
yeah!