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

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>

  <div class="body">
    <div id="board">
      <div id="bat" class="bat"></div>
      <div id="ball" class="ball"></div>
    </div>
  </div>


  <script src="script.js"></script>

</body>

</html>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.body {
  min-height: 100vh;
  background-size: 100vw 100vh;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
}

#board {
  background: linear-gradient(rgb(170, 236, 170), rgb(236, 236, 167));
  width: 90vmin;
  height: 92vmin;
  border: 2px solid black;
  display: grid;
  grid-template-rows: repeat(18, 1fr);
  grid-template-columns: repeat(18, 1fr);
  position: relative;
}

.bat {
  background-color: black;
  height: 20px;
  width: 100px;
  position: absolute;
  bottom: 0;
  left: 40%;
}

.ball {
  border-radius: 50%;
  height: 10px;
  width: 10px;
  background-color: black;
  position: absolute;
  top: 50%;
  left: 50%;
}
              
            
!

JS

              
                let lastPaintTime = 0;
let speed = 10;
let batPosition = { x: 0, y: 0 }
let ballDirection = { x: 0, y: -1 }

function moveBall() {
  const ball = document.getElementById("ball");
  const ballRect = ball.getBoundingClientRect();
  const board = document.getElementById("board");
  const boardRect = board.getBoundingClientRect();

  ballRect.x = ballRect.x + ballDirection.x;
  ballRect.y = ballRect.y + ballDirection.y;
  const bat = document.getElementById("bat");
  const batRect = bat.getBoundingClientRect();


  if (ballRect.top <= boardRect.top) {
    // perform some action, for example,  console.log("ball touches the top of the box")
    ballDirection = { x: 0, y: 1 }
    document.getElementById("ball").style.transform = document.getElementById("ball").style.transform + `translate(${ballDirection.x}px, ${20 * ballDirection.y}px)`;

  }

  else if (ballRect.bottom >= boardRect.bottom) {
    // perform some action, for example,  console.log("ball touches the bottom of the box")
    // ballDirection = { x: 0, y: -1 }
    // document.getElementById("ball").style.transform = document.getElementById("ball").style.transform + `translate(${ballDirection.x}px, ${20 * ballDirection.y}px)`;

    return;
  }
  else if (ballRect.bottom >= batRect.top && ballRect.bottom <= batRect.bottom && ballRect.right >= batRect.left && ballRect.left <= batRect.right) {
    //bat touches the ball

    ballDirection = { x: 1, y: -1 }
    document.getElementById("ball").style.transform = document.getElementById("ball").style.transform + `translate(${20 * ballDirection.x}px, ${20 * ballDirection.y}px)`;

  }


  else {
    document.getElementById("ball").style.transform = document.getElementById("ball").style.transform + `translate(${ballDirection.x}px, ${20 * ballDirection.y}px)`;
  }







}

function main(ctime) {
  window.requestAnimationFrame(main);
  if ((ctime - lastPaintTime) / 1000 < 1 / speed) {
    return;
  }
  //paint now
  lastPaintTime = ctime;

  //move ball:
  moveBall()
}


//Main logic starts here
window.requestAnimationFrame(main);
window.addEventListener("keydown", function (e) {
  const board = document.getElementById("board");
  const boardRect = board.getBoundingClientRect();
  const bat = document.getElementById("bat");
  const batRect = bat.getBoundingClientRect();

  switch (e.key) {
    case "ArrowLeft":
      batPosition.x = -1;
      batPosition.y = 0;
      //move bat left
      if (batRect.left > boardRect.left) {
        document.getElementById("bat").style.transform = document.getElementById("bat").style.transform + `translate(${20 * batPosition.x}px, ${20 * batPosition.y}px)`;
      }

      break;
    case "ArrowRight":
      batPosition.x = 1;
      batPosition.y = 0;
      //move bat right
      if (batRect.right < boardRect.right) {
        document.getElementById("bat").style.transform = document.getElementById("bat").style.transform + `translate(${20 * batPosition.x}px, ${20 * batPosition.y}px)`;
      }

      break;

  }
})



              
            
!
999px

Console