Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. <html>
  2. <canvas id="gc" width="640" height="480"></canvas>
  3. <script>
  4. var ymover= 0;
  5. var score = 0;
  6. var enemyscore = 0;
  7. var velocityx = 1;
  8. var velocityy = 1;
  9. var ballx = 320;
  10. var speedy = 0;
  11. var speedx= -5;
  12. var center = ymover -50;
  13. var hitboxtop = ymover+ 50;
  14. var hitboxbottom = ymover- 50;
  15. var bally = 240
  16. window.onload= function() {
  17. c=document.getElementById('gc');
  18. cc=c.getContext('2d');
  19. document.addEventListener("keydown",keyPush)
  20. setInterval(update,1000/30);
  21. };
  22. function keyPush (evt) {
  23. switch(evt.keyCode) {
  24. case 83:
  25. ymover += 10;
  26. break;
  27. case 87:
  28. ymover -= 10;
  29. break;
  30. }
  31. }
  32. function update() {
  33. cc.fillStyle='black';
  34. cc.fillRect(0,0,c.width,c.height);
  35. cc.fillStyle='red';
  36. cc.fillRect(20,ymover,10,100);
  37. cc.fillStyle ='white';
  38. cc.fillText(score, 320, 240, 500);
  39. cc.fillRect(ballx, bally, 20, 20,);
  40. center = ymover-50;
  41.  
  42. // Starting the paddle Code
  43.  
  44. if (ymover>480) {
  45. ymover= 0;
  46. }
  47. else if (ymover<0) {
  48. ymover = 480;
  49. }
  50. else {
  51. }
  52.  
  53. // Starting the Ball Code
  54.  
  55. if (ballx >640) {
  56. score++;
  57. ballx = 320;
  58. speedy = 0;
  59. bally = 240;
  60. speedx = -speedx;
  61. }
  62. else if (ballx<0) {
  63. enemyscore++;
  64. ballx = 320;
  65. speedy = 0;
  66. bally = 240;
  67. speedx = -speedx;
  68. }
  69. if (ballx> 0 && ballx <40 && bally>hitboxbottom && bally<hitboxtop) {
  70. speedx = -speedx;
  71. console.log('collision');
  72. if (bally> hitboxbottom && bally< ymover) {
  73. speedy -= Math.floor(Math.random() * 5) + 2;
  74. }
  75. else if (bally<hitboxtop && bally> ymover) {
  76. speedy -= Math.floor(Math.random() * 5) + 2;
  77. }
  78. }
  79. if (bally>470) {
  80. bally = 469;
  81. speedy = -speedy;
  82. }
  83. else if (bally<10) {
  84. bally = 11;
  85. speedy = -speedy;
  86. }
  87. ballx += speedx*velocityx;
  88. bally += speedy;
  89. hitboxbottom = center-50;
  90. hitboxtop = center+50;
  91. }
  92. </script>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement