pong game in javascript code example

Example 1: vertical line for pong javascript

// define initial player score
var playerLeft = playerRight = 0

// create text for the score, set font properties
var scoreLeft = draw.text(playerLeft+'').font({
  size: 32,
  family: 'Menlo, sans-serif',
  anchor: 'end',
  fill: '#fff'
}).move(width/2-10, 10)

// cloning rocks!
var scoreRight = scoreLeft.clone()
  .text(playerRight+'')
  .font('anchor', 'start')
  .x(width/2+10)

Example 2: Javascript pong game

// You will need Html, css and sketch.js for this game.
// This is the sketch.js code for pong game.
// Do this in the p5.js 
// You will get HTML and CSS code there, it will be already coded there for you you just have to write the js code.

let xpos = 200
let ypos = 200
let dx = 5;
let dy = 3;
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background('blue');
  rect(10,ypos,10,80);
  rect(380,mouseY,10,80);
  ellipse(xpos,ypos,20,20);
  if (xpos>=width-20 || xpos==20)
    {
      dx = -dx
    }
  if (ypos>=height-20 || ypos==20)
    {
      dy = -dy
    }
  fill('black')
  text('PONG GAME',163,20);
  for (var i=0; i< 400; i+=20) {
    line(200,i,200,i+10);
  }
  xpos = xpos + dx;
  ypos = ypos + dy;
}