Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. <html>
  2. <style>
  3.  
  4. canvas
  5. {
  6. border: 1px solid #d3d3d3;
  7. background-image: url("https://ak0.picdn.net/shutterstock/videos/22492090/thumb/1.jpg");
  8. }
  9.  
  10. </style>
  11.  
  12. <body onload = "startGame()">
  13. <script>
  14. //-----------------------------------------Variables
  15. var gamePieceRed;
  16. var gamePieceBorder;
  17. var gameObstacle;
  18. //-----------------------------------------
  19.  
  20. //-----------------------------------------Main game function
  21. function startGame()
  22. {
  23. gamePieceRed = new component(22, 22, "rgb(255, 132, 156)", 10, 120);
  24. gamePieceBorder = new component(24, 24, "black", 9, 119);
  25.  
  26. obstacle = new component(10, 200, "rgb(64, 0 ,12)", 300, 120)
  27.  
  28. gameArea.start();
  29. }
  30. //-----------------------------------------
  31.  
  32. //-----------------------------------------Creating game area and applying controls
  33. var gameArea =
  34. {
  35. canvas : document.createElement("canvas"), start : function()
  36. {
  37. this.canvas.width = 510;
  38. this.canvas.height = 280;
  39. this.context = this.canvas.getContext("2d");
  40.  
  41. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  42.  
  43. this.interval = setInterval(gameUpdate, 20);
  44.  
  45. window.EventListener("keyDown", function (e)
  46. {
  47. gameArea.keys = (gameArea.keys || []);
  48. gameArea.keys[e.keyCode] = true;
  49. })
  50.  
  51. window.EventListener("keyUp", function (e)
  52. {
  53. gameArea.keys[e.keyCode] = false;
  54. })
  55. },
  56.  
  57. clear : function()
  58. {
  59. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  60. },
  61.  
  62. stop : function()
  63. {
  64. clearInterval(this.interval);
  65. }
  66. }
  67. //-----------------------------------------
  68.  
  69. //-----------------------------------------Game component
  70. function component(width, height, color, x, y)
  71. {
  72. this.width = width;
  73. this.height = height;
  74.  
  75. this.x = x;
  76. this.y = y;
  77.  
  78. this.speedX = 0;
  79. this.speedY = 0;
  80.  
  81. this.update = function()
  82. {
  83. ctx = gameArea.context;
  84. ctx.fillStyle = color;
  85. ctx.fillRect(this.x, this.y, this.width, this.height)
  86. }
  87.  
  88. this.pos = function()
  89. {
  90. this.x += this.speedX;
  91. this.y += this.speedY;
  92. }
  93.  
  94. this.move = function()
  95. {
  96. gamePieceBorder.speedX = 0;
  97. gamePieceRed.speedX = 0;
  98.  
  99. gamePieceBorder.speedY = 0;
  100. gamePieceRed.speedY = 0;
  101.  
  102. if (gameArea.keys && gameArea.keys[37]) {gamePieceBorder.speedX = -2; gamePieceRed.speedX = -2;}
  103. if (gameArea.keys && gameArea.keys[39]) {gamePieceBorder.speedX = 2; gamePieceRed.speedX = 2;}
  104. if (gameArea.keys && gameArea.keys[38]) {gamePieceBorder.speedY = -2; gamePieceRed.speedY = -2;}
  105. if (gameArea.keys && gameArea.keys[40]) {gamePieceBorder.speedY = 2; gamePieceRed.speedY = 2;}
  106. }
  107.  
  108. this.crashGame = function(obj)
  109. {
  110. var left = this.x;
  111. var right = this.x + (this.width);
  112. var top = this.y;
  113. var bottom = this.y + (this.height);
  114.  
  115. var otherLeft = obj.x;
  116. var otherRight = obj.x + (obj.width);
  117. var otherTop = obj.y;
  118. var otherBottom = obj.y + (obj.height);
  119.  
  120. var crash = true;
  121.  
  122. if (bottom < otherTop || top > otherBottom || right < otherLeft || left > otherRight)
  123. {
  124. crash = false;
  125. }
  126.  
  127. return crash;
  128. }
  129. }
  130. //-----------------------------------------
  131.  
  132. //-----------------------------------------Game area updater
  133. function gameUpdate()
  134. {
  135. if(gamePieceBorder.crashGame(gameObstacle) || gamePieceRed.crashGame(gameObstacle))
  136. {
  137. gameArea.stop();
  138. }
  139.  
  140. else
  141. {
  142. obstacle.update();
  143.  
  144. gamePieceBorder.pos();
  145. gamePieceBorder.move();
  146. gamePieceBorder.update();
  147.  
  148. gamePieceRed.pos();
  149. gamePieceRed.move();
  150. gamePieceRed.update();
  151.  
  152. gameArea.clear();
  153. }
  154. }
  155. //-----------------------------------------
  156. </script>
  157. </body>
  158. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement