Advertisement
26F

paddle wars

26F
Sep 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1.  
  2. <html>
  3.  
  4. <center>
  5.  
  6. <canvas id="gamecanvas" width="800" height="600"></canvas>
  7.  
  8. <script>
  9. var canvas;
  10. var canvasContext;
  11. var ballx = 100;
  12. var bally = 100;
  13. var paddle_length = 70;
  14. var paddle_width = 10;
  15. var ball_speedx = 6;
  16. var ball_speedy = 5
  17. var player_y = 100;
  18. var enemy_y = 100;
  19. var game_state = 0;
  20. var timer = 80;
  21. var max_ball_speed = 17;
  22.  
  23. enemy_accuracy = 1
  24.  
  25. var player_hits = 0;
  26. var enemy_hits = 0;
  27.  
  28. function enemy() {
  29. enemy_y = bally * enemy_accuracy;
  30. }
  31.  
  32. function reset_ball() {
  33. timer = 80;
  34. enemy_accuracy = 1;
  35. game_state = 0;
  36. if (Math.floor(Math.random() * 2) == 1) {
  37. enemy_accuracy += 0.07;
  38. }
  39. else
  40. {
  41. enemy_accuracy -= 0.07;
  42. }
  43. ballx = canvas.width / 2;
  44. bally = canvas.height / 2;
  45. ball_speedy = Math.floor(Math.random() * 7) + 2;
  46. ball_speedx = -ball_speedx;
  47. if (Math.floor(Math.random() * 2) == 1) ball_speedy *= -1;
  48.  
  49. }
  50.  
  51. function mouse_xy(evt) {
  52. var rect = canvas.getBoundingClientRect();
  53. var root = document.documentElement;
  54. var mouseX = evt.clientX - rect.left - root.scrollLeft;
  55. var mouseY = evt.clientY - rect.top - root.scrollTop;
  56. return { x:mouseX, y:mouseY };
  57. }
  58.  
  59. window.onload = function() { // onces window finishes loading, run function...
  60.  
  61. canvas = document.getElementById("gamecanvas");
  62. canvasContext = canvas.getContext("2d");
  63.  
  64. setInterval(function() {
  65. motion();
  66. draw_all()
  67. }, 17);
  68.  
  69. canvas.addEventListener("mousemove", function(evt) {
  70.  
  71. var mousepos = mouse_xy(evt);
  72. player_y = mousepos.y;
  73. });
  74. }
  75.  
  76. function motion() {
  77.  
  78. if (game_state == 0) {
  79. ballx += ball_speedx;
  80. bally += ball_speedy;
  81.  
  82. if (ballx < 10) {
  83. if (bally >= player_y - paddle_length / 2 && bally <= player_y + paddle_length / 2) {
  84. ball_speedx = -ball_speedx;
  85. if (ball_speedx < max_ball_speed && ball_speedx > -max_ball_speed) { ball_speedx *= 1.05 }
  86.  
  87. var deltaY = bally - player_y;
  88. ball_speedy = deltaY * 0.35;
  89. }
  90. else
  91. {
  92. if (game_state == 0) enemy_hits += 1;
  93. game_state = -1;
  94.  
  95. }
  96. }
  97. if (ballx >= canvas.width - 10) {
  98. if (bally >= enemy_y - paddle_length / 2 && bally < enemy_y + paddle_length / 2) {
  99. ball_speedx = -ball_speedx;
  100. if (ball_speedx < max_ball_speed && ball_speedx > -max_ball_speed) { ball_speedx *= 1.05; }
  101. var deltaY_enemy = bally - enemy_y;
  102. ball_speedy = deltaY_enemy * 0.35;
  103. }
  104. else
  105. {
  106. if (game_state == 0) player_hits += 1;
  107. game_state = 1;
  108. }
  109.  
  110. }
  111. if (bally < 10) {
  112. ball_speedy *= -1;
  113. }
  114. if (bally >= canvas.height - 10) {
  115. ball_speedy *= -1;
  116. }
  117. enemy();
  118. }
  119. else
  120. {
  121. ballx = -24;
  122. if (timer == 0) {
  123. reset_ball();
  124.  
  125. }
  126. timer--;
  127. }
  128. }
  129.  
  130. function draw_all() {
  131. var bg_color = "black";
  132. if (game_state == -1) {
  133. bg_color = "#460000";
  134. }
  135. else if (game_state == 1) {
  136. bg_color = "#001b00";
  137. }
  138. Rect(0,0, canvas.width, canvas.height, bg_color);
  139. Rect(0,(player_y) - (paddle_length / 2), paddle_width,paddle_length, "#66ff99");
  140. Rect(canvas.width - paddle_width,(enemy_y) - (paddle_length / 2), paddle_width,paddle_length, "#66ff99");
  141. Circle(ballx, bally, 5, "#66ff99");
  142. }
  143.  
  144. function Rect(x, y, Width, Height, color) {
  145. canvasContext.fillStyle = color;
  146. canvasContext.fillRect(x,y, Width, Height);
  147. }
  148.  
  149. function Circle(x, y, radius, color) {
  150. // The ball
  151. // Rect(ballx,canvas.height/2 - (10),20,20,"red");
  152. canvasContext.fillStyle = color;
  153. canvasContext.beginPath();
  154. canvasContext.arc(x, y, radius, 0, Math.PI * 2, true);
  155. canvasContext.fill();
  156.  
  157. canvasContext.fillStyle = "white";
  158. canvasContext.fillText("Player : " + player_hits, 10, 20);
  159. canvasContext.fillText("Enemy : " + enemy_hits, canvas.width - 60, 20);
  160. }
  161.  
  162.  
  163.  
  164.  
  165. </script>
  166. </center>
  167.  
  168. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement