javascript - how do i make the ball collide and bounce off my paddle -
im uni student having make kind of breakout clone in html. have ball bounce off rectangles ive drawn , paddle. problem im still new html , cant seem find solution works code have work off of. taking time read.
var canvas = document.getelementbyid("pong"); var ctx = canvas.getcontext("2d"); var ball = { x: canvas.width / 2, //pixels y: canvas.height / 2, //pixels xspeed: 50, //pixels per second (you can cahnge this) yspeed: 40, //pixels per second (you can change this) radius: 10 //pixels } var paddle = { x: 100, //pixels y: canvas.height / 2, //pixels xspeed: 10, //pixels per second (you can increase this) yspeed: 10, //pixels per second (you can increase this) halfwidth: 10, //pixels halfheight: 40 //pixels } // todo: add data top, bottom, left , right rectangles var topwall = { x:0, y:0, width:800, height:10, area:8000 } var bottomwall = { x:0, y:400, width:800, height:10, area:8000 } var rightwall = { x:800, y:0, width:10, height:400, area:4000 } var leftwall = { x:0, y:0, width:10, height:400, area:4000 } // function draw function render() { //clear canvas ctx.clearrect(0, 0, canvas.width, canvas.height); // draw ball ctx.fillstyle = "white"; ctx.beginpath(); ctx.arc(ball.x, ball.y, ball.radius, 0, math.pi * 2); ctx.fill(); // draw paddle ctx.beginpath(); ctx.moveto(paddle.x - paddle.halfwidth, paddle.y - paddle.halfheight); ctx.lineto(paddle.x - paddle.halfwidth, paddle.y + paddle.halfheight); ctx.lineto(paddle.x + paddle.halfwidth, paddle.y + paddle.halfheight); ctx.lineto(paddle.x + paddle.halfwidth, paddle.y - paddle.halfheight); ctx.fill(); // todo: draw top, bottom, left , right rectangles // drawing right wall ctx.strokestyle = "white"; ctx.fillstyle = "white"; ctx.beginpath(); ctx.moveto(rightwall.x, rightwall.y); ctx.lineto(rightwall.x - rightwall.width, rightwall.y); ctx.lineto(rightwall.x - rightwall.width, rightwall.y + rightwall.height); ctx.lineto(rightwall.x, rightwall.y + rightwall.height); ctx.closepath(); ctx.stroke(); ctx.fill(); //top wall ctx.strokestyle = "white"; ctx.fillstyle = "white"; ctx.beginpath(); ctx.moveto(topwall.x, topwall.y); ctx.lineto(topwall.x + topwall.width, topwall.y); ctx.lineto(topwall.x + topwall.width, topwall.y + topwall.height); ctx.lineto(topwall.x, topwall.y + topwall.height); ctx.closepath(); ctx.stroke(); ctx.fill(); //bottom wall ctx.strokestyle = "white"; ctx.fillstyle = "white"; ctx.beginpath(); ctx.moveto(bottomwall.x, bottomwall.y); ctx.lineto(bottomwall.x + bottomwall.width, bottomwall.y); ctx.lineto(bottomwall.x + bottomwall.width, bottomwall.y - bottomwall.height); ctx.lineto(bottomwall.x, bottomwall.y - bottomwall.height); ctx.closepath(); ctx.stroke(); ctx.fill(); //left wall ctx.strokestyle = "white"; ctx.fillstyle = "white"; ctx.beginpath(); ctx.moveto(leftwall.x, leftwall.y); ctx.lineto(leftwall.x + leftwall.width, leftwall.y); ctx.lineto(leftwall.x + leftwall.width, leftwall.y + leftwall.height); ctx.lineto(leftwall.x, leftwall.y + leftwall.height); ctx.closepath(); ctx.stroke(); ctx.fill(); } // function updates game physics function update(elapsed) { //update ball position according elapsed time ball.y += ball.yspeed * elapsed; ball.x += ball.xspeed * elapsed; // todo: bounce ball of top, bottom , right rectangles // todo: end game (stop ball moving) if ball hits left rectangle // todo: bounce ball off paddle } // handle keyboard input function keydown_handler(e) { // todo: modify code paddle goes , down not outside area of play. switch (event.keycode){ case 38: //up key (move paddle up) if (paddle.y - 30 > 49) { paddle.y -= 30; } break; case 40: // down key (move paddle down) if (paddle.y + 30 < 360) { paddle.y += 30; } break; } } window.addeventlistener("keydown", keydown_handler); var previous; function run(timestamp) { if (!previous) previous = timestamp; //start no elapsed time var elapsed = (timestamp - previous) / 1000; //work out elapsed time update(elapsed); //update game elapsed time render(); //render scene previous = timestamp; //set (globally defined) previous timestamp ready next time window.requestanimationframe(run); //ask browser call function again, when it's ready } //trigger game loop window.requestanimationframe(run);
body { font-family: "century gothic"; } main, header { max-width: 60%; margin: 0 auto; } header h1 { font-size: 6em; margin: 0; } canvas#pong { background: #45484d; /* old browsers */ background: radial-gradient(ellipse @ center, #45484d 0%,#000000 100%); /* w3c */ border-radius: 10px; border: 4px solid #d5282d; width: 100%; }
<!doctype html> <html> <head> <title>pong - resit cw</title> <link rel="stylesheet" href="pong.css" media="screen" title="no title" charset="utf-8"> </head> <body style="overflow:hidden;"> <header> <h1>pong</h1> </header> <main> <canvas id="pong" width="800" height="400"></canvas> <p> task complete following: <ul> <li>add data 4 rectangles around edge of play area.</li> <li>draw these 4 rectangles.</li> <li>alter game physics ball bounces off these rectangles , paddle.</li> <li>add keyboard controls paddle moves , down when , down arrow keys pressed.</li> <li>end game (stop ball moving) when ball hits left rectangle.</li> </ul> </p> <p> of these alterations need made in pong.js file. code marked //todo: give hints functionality needs added. </p> </main> <script charset="utf-8" src="pong.js"></script> </body> </html>
Comments
Post a Comment