Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function keyListener(e){
- if(e.keyCode==37 && paddleLeft > 0){
- //keyCode 37 is left arrow
- paddleLeft -= 10;
- paddle.style.left = paddleLeft + 'px';
- }
- if(e.keyCode==39 && paddleLeft < 436){
- //keyCode 39 is right arrow
- paddleLeft += 10;
- paddle.style.left = paddleLeft + 'px';
- }
- //FYI - keyCode 38 is up arrow, keyCode 40 is down arrow
- }
- function start(){
- //game loop
- detectCollisions();
- render();
- difficulty();
- //end conditions
- if(ballTop < 470){
- //still in play - keep the loop going
- timer = setTimeout('start()',50);
- }
- else{
- gameOver();
- }
- }
- function detectCollisions(){
- //just reflect the ball on a collision
- //a more robust engine could change trajectory of ball based
- //on where the ball hits the paddle
- if(collisionX())
- dx = dx * -1;
- if(collisionY())
- dy = dy * -1;
- }
- function collisionX(){
- //check left and right boundaries
- if(ballLeft < 4 || ballLeft > 285)
- return true;
- return false;
- }
- function collisionY(){
- //check if at top of playing area
- if(ballTop < 4)
- return true;
- //check to see if ball collided with paddle
- if(ballTop > 400){
- if(ballLeft > paddleLeft && ballLeft < paddleLeft + 64)
- return true;
- }
- return false;
- }
Add Comment
Please, Sign In to add comment