Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

pong game

hey, guys, I need help like immediately :D currently on an interview and if somebody can reply this in 1 hour I will be really happy :D

so the task is to make a pong game with js. I will share with you HTML, CSS and JS code that I wrote. The problem is it's not moving :D

INDEX.HTML: <!DOCTYPE html> <html>

<head>
    <title>PONG GAME</title>
    <link rel="stylesheet" type="text/css" href="./pong.css">
</head>

<body id=my>
    <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>
    <script src="./script.js"></script>
</body>

</html>

PONG.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: 500px; left: 15%; top: 20%; width: 1000px; }

.paddle { background: #fff; height: 85px; left: 0; top: 0; width: 12px; }

.paddle-player { background: hsl(360, 100%, 50%); left: 0; top: 150; }

.paddle-enemy { background: hsl(200, 100%, 50%); left:988px; top:415px; }

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

.score-player { color: hsl(360, 100%, 50%); left: 25%; }

.score-enemy { color: hsl(200, 100%, 50%); right: 25%; }

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

.ball { background: #fff; height: 15px; left: 50%; top: 50%; width: 15px; }

SCRIPT JS:

var gameWidth = 100%, gameHeight = 100%;

var paddleWidth = 12, paddleHeight = 85, paddleSpeed = 16;

var ballWidth = 15, ballHeight = 15, ballSpeedStart = 16, ballSpeed = ballSpeedStart;

var scoreMax = 5;

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 };

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

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

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; } }

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); }

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(); } }

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); }

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; } }

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; } }

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

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(); } }

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

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

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

}

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; }

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

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

init();

var gameWrap = document.querySelector('body'), game = document.querySelector('.pong'), gamePadding, gameBc, 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();

Thank you in advance :))

1 Answer

Steven Parker
Steven Parker
225,770 Points

Response in 1 hour or less of a non-trivial issue might be a bit much to expect here. But one thing that would help get faster responses is to always format posted code. Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Even better, you can make a snapshot of a workspace and post the link to it here.

And do you still need help with this issue?