Advertisement
Guest User

Untitled

a guest
May 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  5. <style>
  6. canvas {
  7. border:1px solid #d3d3d3;
  8. background-color: green;
  9. }
  10. </style>
  11. </head>
  12. <body onload="startGame()">
  13. <script>
  14.  
  15. var myGamePiece;
  16. var myObstacles = [];
  17. var myScore;
  18.  
  19. function startGame() {
  20. myGamePiece = new component(30, 30, "smiley.gif", 10, 120, "image");
  21. myScore = new component("30px", "Consolas", "red", 280, 40, "text");
  22. myGameArea.start();
  23. }
  24.  
  25. var myGameArea = {
  26. canvas : document.createElement("canvas"),
  27. start : function() {
  28. this.canvas.width = 480;
  29. this.canvas.height = 270;
  30. this.context = this.canvas.getContext("2d");
  31. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  32. this.frameNo = 0;
  33. this.interval = setInterval(updateGameArea, 20);
  34. window.addEventListener('keydown', function (e) {
  35. myGameArea.keys = (myGameArea.keys || []);
  36. myGameArea.keys[e.keyCode] = (e.type == "keydown");
  37. })
  38. window.addEventListener('keyup', function (e) {
  39. myGameArea.keys[e.keyCode] = (e.type == "keydown");
  40. })
  41. },
  42. clear : function() {
  43. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  44. },
  45. stop : function() {
  46. clearInterval(this.interval);
  47. }
  48. }
  49.  
  50. function component(width, height, color, x, y, type) {
  51. this.type = type;
  52. if (type == "image") {
  53. this.image = new Image();
  54. this.image.src = color;
  55. }
  56. this.width = width;
  57. this.height = height;
  58. this.speedX = 0;
  59. this.speedY = 0;
  60. this.x = x;
  61. this.y = y;
  62. this.update = function() {
  63. ctx = myGameArea.context;
  64. if (this.type == "text") {
  65. ctx.font = this.width + " " + this.height;
  66. ctx.fillStyle = color;
  67. ctx.fillText(this.text, this.x, this.y);
  68. }
  69. else if (type == "image") {
  70. ctx.drawImage(this.image,
  71. this.x,
  72. this.y,
  73. this.width, this.height);
  74. }
  75. else {
  76. ctx.fillStyle = color;
  77. ctx.fillRect(this.x, this.y, this.width, this.height);
  78. }
  79. }
  80. this.newPos = function() {
  81. this.x += this.speedX;
  82. this.y += this.speedY;
  83. }
  84. this.crashWith = function(otherobj) {
  85. var myleft = this.x;
  86. var myright = this.x + (this.width);
  87. var mytop = this.y;
  88. var mybottom = this.y + (this.height);
  89. var otherleft = otherobj.x;
  90. var otherright = otherobj.x + (otherobj.width);
  91. var othertop = otherobj.y;
  92. var otherbottom = otherobj.y + (otherobj.height);
  93. var crash = true;
  94. if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  95. crash = false;
  96. }
  97. return crash;
  98. }
  99. }
  100.  
  101. function updateGameArea() {
  102. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  103. for (i = 0; i < myObstacles.length; i += 1) {
  104. if (myGamePiece.crashWith(myObstacles[i])) {
  105. myGameArea.stop();
  106. return;
  107. }
  108. }
  109. myGameArea.clear();
  110. myGameArea.frameNo += 1;
  111. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  112. x = myGameArea.canvas.width;
  113. minHeight = 20;
  114. maxHeight = 200;
  115. height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  116. minGap = 50;
  117. maxGap = 200;
  118. gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  119. myObstacles.push(new component(10, height, "blue", x, 0));
  120. myObstacles.push(new component(10, x - height - gap, "blue", x, height + gap));
  121. }
  122. for (i = 0; i < myObstacles.length; i += 1) {
  123. myObstacles[i].speedX = -1;
  124. myObstacles[i].newPos();
  125. myObstacles[i].update();
  126. }
  127. if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -1; }
  128. if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 1; }
  129. if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -1; }
  130. if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 1; }
  131. myScore.text="SCORE: " + myGameArea.frameNo;
  132. myScore.update();
  133. myGamePiece.newPos();
  134. myGamePiece.update();
  135. }
  136.  
  137. function everyinterval(n) {
  138. if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  139. return false;
  140. }
  141.  
  142. function moveup() {
  143. myGamePiece.speedY = -1;
  144. }
  145.  
  146. function movedown() {
  147. myGamePiece.speedY = 1;
  148. }
  149.  
  150. function moveleft() {
  151. myGamePiece.speedX = -1;
  152. }
  153.  
  154. function moveright() {
  155. myGamePiece.speedX = 1;
  156. }
  157.  
  158. function clearmove() {
  159. myGamePiece.speedX = 0;
  160. myGamePiece.speedY = 0;
  161. }
  162. </script>
  163. <div style="text-align:center;width:480px;">
  164. <button onmousedown="moveup()" onmouseup="clearmove()" ontouchstart="moveup()">UP</button><br><br>
  165. <button onmousedown="moveleft()" onmouseup="clearmove()" ontouchstart="moveleft()">LEFT</button>
  166. <button onmousedown="moveright()" onmouseup="clearmove()" ontouchstart="moveright()">RIGHT</button><br><br>
  167. <button onmousedown="movedown()" onmouseup="clearmove()" ontouchstart="movedown()">DOWN</button>
  168. </div>
  169.  
  170. <p>The score will count one point for each frame you manage to "stay alive".</p>
  171. </body>
  172. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement