Question
using javascript, make the ball be able to bounce off either the left or right paddles instead of go through, thank you Ping Pong To
using javascript, make the ball be able to bounce off either the left or right paddles instead of go through, thank you
To play the game, move the keys W or S to move up or down for the left paddle. To move the right paddle, simply move the up or down arrow keys
canvas {
padding: 0;
margin: auto;
display: block;
width: 800px;
height: 600px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
var canvas, paddle1, paddle2, ctx;
/* set the canvas size and color */
var canvas = document.getElementById("myCanvas");
//this event will move the paddle
window.addEventListener('keydown', moveIt, false);
function check(e) {
alert(e.keyCode);
}
var ctx = canvas.getContext("2d");
ctx.fillStyle = "green";
ctx.fill();
var x = canvas.width;
var y = canvas.height;
var dx = 2;
var dy = -2;
var yDirection = 100;
var y2Direction = 100;
ctx.beginPath();
ctx.rect(20, 20, 800, 800);
ctx.fill();
/* dimensions of the ball */
var xposball = 200;
var yposball = 300;
/* radius of the ball */
var radiusball = 12;
/* speed of the ball */
var xspeedball = 1;
var yspeedball = 1;
/* direction of the ball */
xdirball = 0.8;
ydirball = 0.8;
/* speed of the game */
var gamespeed = 2;
/* add the ball, paddles, and canvas to game */
function draw() {
ctx.fillStyle = "green";
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fill();
// paddle 1
paddle1 = ctx.rect(18, y2Direction, 20, 150);
ctx.fillStyle = 'black';
ctx.stroke();
// paddle 2
paddle2 = ctx.rect(763, yDirection, 20, 150);
ctx.fillStyle = 'black';
ctx.fill();
ctx.stroke();
/* draw the ball */
ctx.fillStyle = "black";
ctx.beginPath();
ctx.arc(xposball, yposball, radiusball, 0, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
// moving the ball
xposball = xposball + xspeedball * xdirball;
yposball = yposball + yspeedball * ydirball;
// setting out of bounds for the top and bottom of the
// canvas
xposball = xposball + xspeedball * xdirball;
if ((yposball > (canvas.height - radiusball)) || ((yposball - radiusball) < 0)) {
ydirball = -1 * ydirball;
}
yposball = yposball + yspeedball * ydirball;
}
//this funtion will move the paddle based on input
// using javascript character codes
function moveIt(e) {
if (e.keyCode == 38) {
yDirection = yDirection - 10;
} else if (e.keyCode == 40) {
yDirection = yDirection + 10;
}
if (e.keyCode == 87) {
y2Direction = y2Direction - 10;
} else if (e.keyCode == 83) {
y2Direction = y2Direction + 10;
}
}
// calling our draw function, and game speed
setInterval(draw, gamespeed);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started