Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="pong">
	<div class="paddle paddle-player"></div>
	<div class="paddle paddle-enemy"></div>
	<div class="score score-player">0</div>
	<div class="score score-enemy">0</div>
	<div class="net"></div>
	<div class="ball"></div>
</div>
              
            
!

CSS

              
                html,
body {
	height: 100%;	
}

body {
	background: #111;
	background: radial-gradient(#222, #111);
	overflow: hidden;
}

.pong,
.pong * {
	position: absolute;
}

.pong {
	box-shadow: 0 0 0 8px #666;
	height: 1080px;
	left: 50%;
	top: 50%;
	width: 1920px;
}

.paddle {
	background: #fff;
	height: 240px;
	left: 0;
	top: 0;
	width: 60px;
}

.paddle-player { 
	background: hsl(130, 100%, 60%);
}

.paddle-enemy {
	background: hsl(200, 100%, 60%);
}

.score {
	color: white;
	font: bold 6em/1 'Roboto Mono', monospace;
	top: 80px;
}

.score-player {
	color: hsl(130, 100%, 60%);
	left: 180px;
}

.score-enemy {
	color: hsl(200, 100%, 60%);
	right: 180px;
}

.net {
	background: #666;
	height: 100%;
	left: 50%;
	margin-left: -2px;
	top: 0;
	width: 4px;
}

.ball {
	background: #fff;
	height: 60px;
	left: 0;
	top: 0;
	width: 60px;
}
              
            
!

JS

              
                /*==========================================
Config 
==========================================*/

// game
var gameWidth = 1920,
	gameHeight = 1080;

// paddles
var paddleWidth = 60,
	paddleHeight = 240,
	paddleSpeed = 16;

// ball
var ballWidth = 60,
	ballHeight = 60,
	ballSpeedStart = 16,
	ballSpeed = ballSpeedStart;

// score - what are we playing to?
var scoreMax = 5;

/*==========================================
Objects 
==========================================*/

var paddlePlayer = {
	elem: document.querySelector('.paddle-player'),
	x: 0,
	y: gameHeight / 2 - paddleHeight / 2,
	width: paddleWidth,
	height: paddleHeight,
	speed: paddleSpeed,
	moveUp: false,
	moveDown: false
};

var paddleEnemy = {
	elem: document.querySelector('.paddle-enemy'),
	x: gameWidth - paddleWidth,
	y: gameHeight / 2 - paddleHeight / 2,
	width: paddleWidth,
	height: paddleHeight,
	speed: paddleSpeed,
	moveUp: false,
	moveDown: false
};
	
var ball = {
	elem: document.querySelector('.ball'),
	x: gameWidth / 2 - ballWidth / 2,
	y: gameHeight / 2 - ballHeight / 2,
	vx: ballSpeed,
	vy: ballSpeed,
	width: ballWidth,
	height: ballHeight
};

var scorePlayer = {
	elem: document.querySelector('.score-player'),
	value: 0
};

var scoreEnemy = {
	elem: document.querySelector('.score-enemy'),
	value: 0
};

/*==========================================
Initialize 
==========================================*/

function init() {
	addEventListeners();
	loop();
}

/*==========================================
Resets
==========================================*/

function resetGame() {
	ballSpeed = ballSpeedStart;
	scorePlayer.value = 0;
	scoreEnemy.value = 0;
	resetBall();
}

function resetBall() {
	ball.x = gameWidth / 2 - ballWidth / 2;
	ball.y = gameHeight / 2 - ballHeight / 2;
	ball.vx = 0;
	ball.vy = 0;
	setTimeout(function() {
		ball.vx = ballSpeed;
		ball.vy = ballSpeed;
	}, 1000);
}

/*==========================================
Events 
==========================================*/

function addEventListeners() {
	window.addEventListener('keydown', function(e) {
		if (e.which === 38) { 
			paddlePlayer.moveUp = true;
		}
		if (e.which === 40) {
			paddlePlayer.moveDown = true;
		}
	});

	window.addEventListener('keyup', function(e) {
		if (e.which === 38) { 
			paddlePlayer.moveUp = false;
		}
		if (e.which === 40) {
			paddlePlayer.moveDown = false;
		}
	});
}

/*==========================================
AABB Collision Detection
==========================================*/

function collisionAABB(r1, r2) {
	if (!(
		r1.x > r2.x + r2.width ||  // rect1 is on the right of rect2
		r1.x + r1.width < r2.x ||  // rect1 is on the left of rect2
		r1.y > r2.y + r2.height || // rect1 is below rect2
		r1.y + r1.height < r2.y    // rect1 is above rect2
	)) {
		return true;
	}
}

/*==========================================
Move Ball
==========================================*/

function moveBall() {
	ball.x += ball.vx;
	ball.y += ball.vy;
}

/*==========================================
Move Player
==========================================*/

function movePlayer() {
	if (paddlePlayer.moveUp) {
		paddlePlayer.y -= paddlePlayer.speed;
	} else if (paddlePlayer.moveDown) {
		paddlePlayer.y += paddlePlayer.speed;
	}
}

/*==========================================
Move Enemy
==========================================*/

function moveEnemy() {
	if (Math.random() < 0.2) {
		paddleEnemy.moveUp = false;
		paddleEnemy.moveDown = false;
		if (ball.y + ballHeight < paddleEnemy.y + paddleEnemy.height / 2) {
			paddleEnemy.moveUp = true;
		} else if (ball.y > paddleEnemy.y + paddleEnemy.height / 2) {
			paddleEnemy.moveDown = true;
		}
	}
	
	if (paddleEnemy.moveUp) {
		paddleEnemy.y -= paddleEnemy.speed;
	} else if (paddleEnemy.moveDown) {
		paddleEnemy.y += paddleEnemy.speed;
	}
}

/*==========================================
Contain Ball
==========================================*/

function containBall() {
	if (ball.y <= 0) {
		ball.y = 0;
		ball.vy = -ball.vy;
	} else if (ball.y + ball.height >= gameHeight) {
		ball.y = gameHeight - ball.height;
		ball.vy = -ball.vy;
	}

	if (ball.x <= 0) {
		scoreEnemy.value += 1;
		ballSpeed += 1;
		resetBall();
	} else if (ball.x + ball.width >= gameWidth) {
		scorePlayer.value += 1;
		ballSpeed += 1;
		resetBall();
	}
}

/*==========================================
Contain Paddles
==========================================*/

function containPaddles() {
	paddlePlayer.y = Math.max(0, paddlePlayer.y);
	paddlePlayer.y = Math.min(gameHeight - paddlePlayer.height, paddlePlayer.y);
	
	paddleEnemy.y = Math.max(0, paddleEnemy.y);
	paddleEnemy.y = Math.min(gameHeight - paddleEnemy.height, paddleEnemy.y);
}

/*==========================================
Check Collisions
==========================================*/

function checkCollisions() {
	if (collisionAABB(ball, paddlePlayer)) {
		ball.x = paddlePlayer.x + paddlePlayer.width;
		ball.vx = -ball.vx;
	}
	
	if (collisionAABB(ball, paddleEnemy)) {
		ball.x = paddleEnemy.x - ball.width;
		ball.vx = -ball.vx;
	}
}

/*==========================================
Check Win State
==========================================*/

function checkWinState() {
	if (scorePlayer.value >= scoreMax) {
		console.log('You win!');
		resetGame();
	} else if (scoreEnemy.value >= scoreMax) {
		console.log('You get nothing! You lose! Good day, sir!');
		resetGame();
	}
}

/*==========================================
Update
==========================================*/

function update() {
	moveBall();
	movePlayer();
	moveEnemy();
	containBall();
	containPaddles();
	checkCollisions();
	checkWinState();
}

/*==========================================
Render
==========================================*/

function render() {
	paddlePlayer.elem.style.transform = 'translate(' + paddlePlayer.x + 'px, ' + paddlePlayer.y + 'px)';
	paddleEnemy.elem.style.transform = 'translate(' + paddleEnemy.x + 'px, ' + paddleEnemy.y + 'px)';
	ball.elem.style.transform = 'translate(' + ball.x + 'px, ' + ball.y + 'px)';
	scorePlayer.elem.innerHTML = scorePlayer.value;
	scoreEnemy.elem.innerHTML = scoreEnemy.value;
}

/*==========================================
Loop 
==========================================*/

function loop() {
	requestAnimationFrame(loop);
	update();
	render();
}

/*==========================================
Let's Play! 
==========================================*/

init(); // to win it!

/*==========================================
Non-Gameplay Related Scaling
==========================================*/

var gameWrap = document.querySelector('body'),
	game = document.querySelector('.pong'),
	gamePadding = 0.15,
	gameBcr,
	gameWidth,
	gameHeight,
	gameRatio,
	gameScale,
	gameWrapWidth,
	gameWrapHeight;
	
function setScale() {
	game.style.transform = 'translateX(-50%) translateY(-50%) scale(1)';
	gameBcr = game.getBoundingClientRect();
	gameWidth = gameBcr.width;
	gameHeight = gameBcr.height;
	gameRatio = gameHeight / gameWidth;
	gameWrapWidth = gameWrap.offsetWidth - ( gameWrap.offsetWidth * gamePadding );
	gameWrapHeight = gameWrap.offsetHeight - ( gameWrap.offsetHeight * gamePadding );
	if (gameWrapWidth > gameWrapHeight / gameRatio) {
		gameScale = gameWrapHeight / gameRatio / gameWidth;
	} else {
		gameScale = gameWrapWidth * gameRatio / gameHeight;
	}
	game.style.transform = 'translateX(-50%) translateY(-50%) scale(' + gameScale + ')';
}

function onResize() {
	setScale();
}

addEventListener('resize', onResize);

setScale();
              
            
!
999px

Console