Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 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: #66ffff;
  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.png", 10, 120, "image");
  21. myGamePiece.gravity = 0.05;
  22. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  23. myGameArea.start();
  24. }
  25.  
  26. var myGameArea = {
  27. canvas : document.createElement("canvas"),
  28. start : function() {
  29. this.canvas.width = 480;
  30. this.canvas.height = 270;
  31. this.context = this.canvas.getContext("2d");
  32. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  33. this.frameNo = 0;
  34. this.interval = setInterval(updateGameArea, 20);
  35. },
  36. clear : function() {
  37. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  38. }
  39. }
  40.  
  41. function component(width, height, color, x, y, type) {
  42. this.type = type;
  43. if (type == "image") {
  44. this.image = new Image();
  45. this.image.src = color;
  46. }
  47. this.type = type;
  48. this.score = 0;
  49. this.width = width;
  50. this.height = height;
  51. this.speedX = 0;
  52. this.speedY = 0;
  53. this.x = x;
  54. this.y = y;
  55. this.gravity = 0;
  56. this.gravitySpeed = 0;
  57. this.update = function() {
  58. ctx = myGameArea.context;
  59. if (type == "image") {
  60. ctx.drawImage(this.image,
  61. this.x,
  62. this.y,
  63. this.width, this.height);
  64. } else {
  65. ctx.fillStyle = color;
  66. ctx.fillRect(this.x, this.y, this.width, this.height);
  67. }
  68. }
  69. this.newPos = function() {
  70. this.gravitySpeed += this.gravity;
  71. this.x += this.speedX;
  72. this.y += this.speedY + this.gravitySpeed;
  73. this.hitBottom();
  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, "yellow", x, 0));
  117. myObstacles.push(new component(10, x - height - gap, "yellow", 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. </script>
  138. <br>
  139. <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
  140. <p>Use the ACCELERATE button to stay in the air</p>
  141. <p>How long can you stay alive?</p>
  142. </body>
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement