Guest User

Untitled

a guest
Jul 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. function keyListener(e){
  2.  
  3. if(e.keyCode==37 && paddleLeft > 0){
  4. //keyCode 37 is left arrow
  5. paddleLeft -= 10;
  6. paddle.style.left = paddleLeft + 'px';
  7. }
  8. if(e.keyCode==39 && paddleLeft < 436){
  9. //keyCode 39 is right arrow
  10. paddleLeft += 10;
  11. paddle.style.left = paddleLeft + 'px';
  12. }
  13. //FYI - keyCode 38 is up arrow, keyCode 40 is down arrow
  14. }
  15.  
  16. function start(){
  17. //game loop
  18. detectCollisions();
  19. render();
  20. difficulty();
  21.  
  22. //end conditions
  23. if(ballTop < 470){
  24. //still in play - keep the loop going
  25. timer = setTimeout('start()',50);
  26. }
  27. else{
  28. gameOver();
  29. }
  30. }
  31.  
  32. function detectCollisions(){
  33. //just reflect the ball on a collision
  34. //a more robust engine could change trajectory of ball based
  35. //on where the ball hits the paddle
  36. if(collisionX())
  37. dx = dx * -1;
  38. if(collisionY())
  39. dy = dy * -1;
  40. }
  41.  
  42. function collisionX(){
  43. //check left and right boundaries
  44. if(ballLeft < 4 || ballLeft > 285)
  45. return true;
  46. return false;
  47. }
  48.  
  49. function collisionY(){
  50. //check if at top of playing area
  51. if(ballTop < 4)
  52. return true;
  53. //check to see if ball collided with paddle
  54. if(ballTop > 400){
  55. if(ballLeft > paddleLeft && ballLeft < paddleLeft + 64)
  56. return true;
  57. }
  58. return false;
  59. }
Add Comment
Please, Sign In to add comment