javascript pong game code example
Example 1: vertical line for pong javascript
var playerLeft = playerRight = 0
var scoreLeft = draw.text(playerLeft+'').font({
size: 32,
family: 'Menlo, sans-serif',
anchor: 'end',
fill: '#fff'
}).move(width/2-10, 10)
var scoreRight = scoreLeft.clone()
.text(playerRight+'')
.font('anchor', 'start')
.x(width/2+10)
Example 2: Javascript pong game
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;
}