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

Save Automatically?

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

              
                
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js"></script>
     <script src="https://cdn.jsdelivr.net/gh/molleindustria/p5.play/lib/p5.play.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/13.8.12/Tone.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                //Programming Digital Media
//PONG Project- Final Version Example
//code by Matthew A. Bardin [2019]


//declaring and initializing variables
let ballX, ballY, leftScore, rightScore;
let ballAngle = 0;
let ballSpeed = 0;
//this one is an array to choose ball angle when bounced
let ballAngleChoice = [-2, -1, 0, 1, 2];
let rPaddleY = 0;


function setup() {
  createCanvas(windowWidth, windowHeight);
  //slows down the ball and paddle movement to look more like the old games. Optional.
  frameRate(30);
  //sets starting ball position and scores
  ballX = random(50, width - 50);
  ballY = random(50, (height - 50));
  leftScore = 0;
  rightScore = 0;
}

function draw() {
  background('black');
  fill('white'); //will need stroke("white"); if a line is used for the divider in stead of a rectangle
  //divider. made with a rectangle
  rect(width / 2, 0, 2, height);
  //right paddle - follows the arrow keys vertically
  rect(width - 10, rPaddleY, 10, 50);
  //left paddle- follows the mouse vertically
  rect(0, mouseY, 10, 50);
  //ball
  rect(ballX, ballY, 10, 10);

  //score counters
  textFont("Courier New");
  textSize(45);
  textAlign(CENTER);
  text(leftScore, width * 0.25, 50);
  text(rightScore, width * 0.75, 50);

  //instructions
  textSize(12);
  text("The mouse controls the left paddle. The Up and Down Arrows control the right paddle. ", width / 2, height - 50);
  text("Press ESC  to Start!!", width / 2, height - 25);

  //makes the ball move horizontally and vertically. This is the muscle of the project.
  ballX = ballX + ballSpeed;
  ballY = ballY + ballAngle;

  //if statement to control the right paddle
  if (keyIsPressed && keyCode == UP_ARROW) {
    rPaddleY = rPaddleY - 10;
  } else if (keyIsPressed && keyCode == DOWN_ARROW) {
    rPaddleY = rPaddleY + 10;
  } else if (keyIsPressed && keyCode == 27) { //starts the game if ESC is pressed. Also adjusts the ball angle.
    ballSpeed = -4;
    ballAngle = random(ballAngleChoice);
  }
  //keeps right paddle on the screen
  if (rPaddleY < 0) {
    rPaddleY = 0;
  } else if (rPaddleY > (height - 50)) {
    rPaddleY = height - 50;
  }

  //makes the ball bounce off left paddle. This is the brain of the project
  if (ballX < 10) {
    if (ballY > mouseY && ballY < (mouseY + 50)) {
      ballSpeed = 4;
      ballAngle = random(ballAngleChoice);
    }
  }
  //makes the ball bounce off right paddle. This is the brain of the project.
  if (ballX > width - 20) { //needs to be the width of the paddle and the ball
    if (ballY > rPaddleY && ballY < (rPaddleY + 50)) {
      ballSpeed = -4;
      ballAngle = random(ballAngleChoice);
    }
  }

  //resets ball when it moves off of the screen vertically
  if (ballY < 2 || ballY > height - 2) {
    ballY = random(50, (height - 50));
  }

  //update scores and resets ball when ball passes horizontal edges of the screen. flips ball direction as well.
  if (ballX < -75) {
    rightScore++;
    ballX = width / 2;
    ballSpeed = 4
  } else if (ballX > width + 75) {
    leftScore++;
    ballX = width / 2;
    ballSpeed = -4
  }
}
              
            
!
999px

Console