Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 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: white;
  9. }
  10. </style>
  11. </head>
  12. <body onload="startGame()">
  13. <script>
  14.  
  15. var myGamePiece;
  16. var myObstacles = [];
  17. var myScore;
  18. var img = new Image;
  19. img.src = "nathanos.png";
  20.  
  21. function startGame() {
  22. myGamePiece = new component(10, 10,"White", 10, 120,"image");
  23. myGamePiece.gravity = 0.7;
  24. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  25. myGameArea.start();
  26. }
  27.  
  28. var myGameArea = {
  29. canvas : document.createElement("canvas"),
  30. start : function() {
  31. this.canvas.width = 480;
  32. this.canvas.height = 270;
  33. this.context = this.canvas.getContext("2d");
  34. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  35. this.frameNo = 0;
  36. this.interval = setInterval(updateGameArea, 20);
  37. },
  38. clear : function() {
  39. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  40. }
  41. }
  42.  
  43. function component(width, height, color, x, y, type) {
  44. this.type = type;
  45. this.score = 0;
  46. this.width = width;
  47. this.height = height;
  48. this.speedX = 0;
  49. this.speedY = 0;
  50. this.x = x;
  51. this.y = y;
  52. this.gravity = 0;
  53. this.gravitySpeed = 0;
  54. this.update = function() {
  55. ctx = myGameArea.context;
  56. if (this.type == "text") {
  57. ctx.font = this.width + " " + this.height;
  58. ctx.fillStyle = color;
  59. ctx.fillText(this.text, this.x, this.y);
  60. } else {
  61. ctx.fillStyle = color;
  62. ctx.fillRect(this.x, this.y, this.width, this.height);
  63. }
  64. }
  65. this.newPos = function() {
  66. this.gravitySpeed += this.gravity;
  67. this.gravity += 0.001;
  68. this.x += this.speedX;
  69. if( this.y > 1)
  70. this.y += this.speedY + this.gravitySpeed;
  71. else this.y = 1.1;
  72. this.hitBottom();
  73. ctx.drawImage(img, this.x, this.y);
  74. }
  75. this.hitBottom = function() {
  76. var rockbottom = myGameArea.canvas.height - this.height;
  77. if (this.y > rockbottom) {
  78. this.y = rockbottom;
  79. this.gravitySpeed = 0;
  80. }
  81. }
  82. this.crashWith = function(otherobj) {
  83. var myleft = this.x;
  84. var myright = this.x + (this.width);
  85. var mytop = this.y;
  86. var mybottom = this.y + (this.height);
  87. var otherleft = otherobj.x;
  88. var otherright = otherobj.x + (otherobj.width);
  89. var othertop = otherobj.y;
  90. var otherbottom = otherobj.y + (otherobj.height);
  91. var crash = true;
  92. if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  93. crash = false;
  94. }
  95. return crash;
  96. }
  97. }
  98.  
  99. function updateGameArea() {
  100. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  101. for (i = 0; i < myObstacles.length; i += 1) {
  102. if (myGamePiece.crashWith(myObstacles[i])) {
  103. return;
  104. }
  105. }
  106. myGameArea.clear();
  107. myGameArea.frameNo += 1;
  108. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  109. x = myGameArea.canvas.width;
  110. minHeight = 20;
  111. maxHeight = 200;
  112. height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  113. minGap = 50;
  114. maxGap = 200;
  115. gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  116. myObstacles.push(new component(10, height, "blue", x, 0));
  117. myObstacles.push(new component(10, x - height - gap, "purple", x, height + gap));
  118. }
  119. for (i = 0; i < myObstacles.length; i += 1) {
  120. myObstacles[i].x += -1;
  121. myObstacles[i].update();
  122. }
  123. myScore.text="SCORE: " + myGameArea.frameNo;
  124. myScore.update();
  125. myGamePiece.newPos();
  126. myGamePiece.update();
  127. }
  128.  
  129. function everyinterval(n) {
  130. if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  131. return false;
  132. }
  133.  
  134. function accelerate(n) {
  135. myGamePiece.gravity = n;
  136. }
  137. document.onkeydown = function(e) {
  138. switch (e.keyCode) {
  139. case 38:
  140. accelerate(-0.5);
  141. break;
  142. case 40:
  143. accelerate(0.5);
  144. break;
  145. }
  146. };
  147. document.onkeyup = function(e) {
  148. switch (e.keyCode) {
  149. case 38:
  150. accelerate(0.2);
  151. break;
  152. case 40:
  153. accelerate(-0.2);
  154. break;
  155. }
  156. };
  157. </script>
  158. </body>
  159. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement