pong game tutorial with javascript code example

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

Tags:

Html Example