Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title></title>
  4. <style>
  5. canvas { border: 2px solid black; background: #CD5C5C; display: block; margin: 0 auto; }
  6. </style>
  7. </head>
  8. <body>
  9. <h1><center>Bouncing Obstacles</center></h1>
  10. <canvas id="mycanvas" width="640" height="360" ></canvas>
  11. <script>
  12. window.addEventListener('load',function(){
  13. //constants
  14. var GAME_WIDTH = 640;
  15. var GAME_HEIGHT = 360;
  16.  
  17. //An array of Obstacles. Each Obstacle has its own attributes
  18. var obstacles = [
  19. {
  20. x: 100, //x coordinate
  21. y: 100, //y coordinate
  22. speedY: 1, //speed in Y
  23. radius: 30, //radius of circle
  24. color: "#FF6347"
  25. },
  26. {
  27. x: 260,
  28. y: 100,
  29. speedY: 2,
  30. radius: 20,
  31. color: "#BDB76B"
  32.  
  33. },
  34. {
  35. x: 370,
  36. y: 100,
  37. speedY: 3,
  38. radius: 10,
  39. color: "#4169E1"
  40. },
  41. {
  42. x: 520,
  43. y: 100,
  44. speedY: 5,
  45. radius: 25,
  46. color: "#C71585"
  47. }
  48. ];
  49.  
  50. //Player Object
  51. var player = {
  52. x: 10,
  53. y:150,
  54. speedX:2,
  55. w:35,
  56. h:35,
  57. isMoving: false,
  58. hasFocus: false
  59. }
  60.  
  61. //grab the canvas and context
  62. var canvas = document.getElementById("mycanvas");
  63. var ctx = canvas.getContext("2d");
  64.  
  65. var movePlayer = function(){
  66. if(player.hasFocus) { player.isMoving = true ; }
  67. }
  68.  
  69. var stopPlayer = function(){
  70. player.isMoving = false;
  71. player.hasFocus = false ;
  72. }
  73.  
  74. var mouseOnPlayer = function(event) {
  75. var currpos = getMousePos(canvas, event);
  76. var posx = currpos.x ;
  77. var posy = currpos.y;
  78.  
  79. if(posx >= player.x && posx <= player.x + player.w &&
  80. posy >= player.y && posy <= player.y + player.h)
  81. player.hasFocus = true ;
  82. else
  83. player.hasFocus = false ;
  84. }
  85.  
  86. var getMousePos = function(canvas,event){
  87. var rect = canvas.getBoundingClientRect();
  88. return {
  89. x:(event.clientX - rect.left)/(rect.right-rect.left)*canvas.width ,
  90. y:(event.clientY - rect.top)/(rect.bottom - rect.top)*canvas.height
  91. }
  92. }
  93.  
  94. canvas.addEventListener("mousedown",movePlayer);
  95. canvas.addEventListener("mouseup",stopPlayer);
  96. canvas.addEventListener("mousemove",mouseOnPlayer);
  97.  
  98. // Code for touch event
  99. canvas.addEventListener("touchstart",movePlayer);
  100. canvas.addEventListener ("touchend",stopPlayer)
  101.  
  102. //update the logic
  103. var update = function() {
  104.  
  105. //Update Player location
  106. if(player.isMoving){
  107. player.x+= player.speedX;
  108. }
  109.  
  110. //Update Obstacle location
  111.  
  112. var i = 0;
  113. var n = obstacles.length;
  114.  
  115. obstacles.forEach(function(element, index){
  116. element.y += element.speedY;
  117.  
  118. //check boundary conditions
  119. if(element.y <= 15) {
  120. element.y = 15;
  121. //element.speedY = element.speedY * -1;
  122. element.speedY *= -1;
  123. }
  124. else if(element.y >= GAME_HEIGHT - 25) {
  125. element.y = GAME_HEIGHT - 25;
  126. element.speedY *= -1;
  127. }
  128. });
  129. };
  130.  
  131. //show the game on the screen
  132. var draw = function() {
  133. //clear the canvas
  134. ctx.clearRect(0,0,GAME_WIDTH,GAME_HEIGHT);
  135.  
  136. obstacles.forEach(function(element, index){
  137.  
  138. ctx.beginPath();
  139. ctx.fillStyle = element.color;
  140. ctx.arc(element.x,element.y,element.radius,0,2*Math.PI,true);
  141. ctx.fill();
  142. });
  143.  
  144. ctx.fillStyle = "#00FF00";
  145. ctx.fillRect(player.x,player.y,player.w,player.h);
  146. };
  147.  
  148. //gets executed multiple times per second
  149. var step = function() {
  150.  
  151. update();
  152. draw();
  153.  
  154. window.requestAnimationFrame(step);
  155. };
  156.  
  157. //initial kick
  158. step();
  159. });
  160. </script>
  161. </body>
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement