MEGAPONG
MEGAPONG
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Elite Pong</title>
<style>
body {
background-color: black;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
canvas {
border: 2px solid white;
}
</style>
</head>
<body>
<canvas id="pong" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('pong');
const ctx = canvas.getContext('2d');
// Game constants
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
const PADDLE_WIDTH = 15;
const PADDLE_HEIGHT = 100;
const BALL_RADIUS = 10;
// Paddle object
function Paddle(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = 6;
this.dy = 0;
this.update = function() {
this.y += this.dy;
};
this.draw = function() {
ctx.fillStyle = '#FFF';
ctx.fillRect(this.x, this.y, this.width, this.height);
};
this.moveUp = function() {
if (this.y > 0) {
this.dy = -this.speed;
}
};
this.moveDown = function() {
if (this.y + this.height < HEIGHT) {
this.dy = this.speed;
}
};
this.stop = function() {
this.dy = 0;
};
}
// Ball object
function Ball(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.dx = 4 * (Math.random() > 0.5 ? 1 : -1);
this.dy = 4 * (Math.random() > 0.5 ? 1 : -1);
this.speed = 5;
this.update = function() {
this.x += this.dx;
this.y += this.dy;
};
this.draw = function() {
ctx.fillStyle = '#FFF';
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
};
this.reset = function() {
this.x = WIDTH / 2;
this.y = HEIGHT / 2;
this.dx = 4 * (Math.random() > 0.5 ? 1 : -1);
this.dy = 4 * (Math.random() > 0.5 ? 1 : -1);
this.speed = 5;
};
}
// Update positions
leftPaddle.update();
rightPaddle.update();
ball.update();
// Move AI paddle
aiMove();
// Draw everything
leftPaddle.draw();
rightPaddle.draw();
ball.draw();