index.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: #f1f1f1;
  9. }
  10. </style>
  11. </head>
  12. <body onload="startGame()">
  13. <script>
  14. var myGamePiece;
  15. var myObstacles = [];
  16. var myScore;
  17. function startGame() {
  18. myGamePiece = new component(30, 30, "red", 10, 120);
  19. myGamePiece.gravity = 0.05;
  20. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  21. myGameArea.start();
  22. }
  23. var myGameArea = {
  24. canvas : document.createElement("canvas"),
  25. start : function() {
  26. this.canvas.width = 480;
  27. this.canvas.height = 270;
  28. this.context = this.canvas.getContext("2d");
  29. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  30. this.frameNo = 0;
  31. this.interval = setInterval(updateGameArea, 20);
  32. },
  33. clear : function() {
  34. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  35. }
  36. }
  37. function component(width, height, color, x, y, type) {
  38. this.type = type;
  39. this.score = 0;
  40. this.width = width;
  41. this.height = height;
  42. this.speedX = 0;
  43. this.speedY = 0;
  44. this.x = x;
  45. this.y = y;
  46. this.gravity = 0;
  47. this.gravitySpeed = 0;
  48. this.update = function() {
  49. ctx = myGameArea.context;
  50. if (this.type == "text") {
  51. ctx.font = this.width + " " + this.height;
  52. ctx.fillStyle = color;
  53. ctx.fillText(this.text, this.x, this.y);
  54. } else {
  55. ctx.fillStyle = color;
  56. ctx.fillRect(this.x, this.y, this.width, this.height);
  57. }
  58. }
  59. this.newPos = function() {
  60. this.gravitySpeed += this.gravity;
  61. this.x += this.speedX;
  62. this.y += this.speedY + this.gravitySpeed;
  63. this.hitBottom();
  64. }
  65. this.hitBottom = function() {
  66. var rockbottom = myGameArea.canvas.height - this.height;
  67. if (this.y > rockbottom) {
  68. this.y = rockbottom;
  69. this.gravitySpeed = 0;
  70. }
  71. }
  72. this.crashWith = function(otherobj) {
  73. var myleft = this.x;
  74. var myright = this.x + (this.width);
  75. var mytop = this.y;
  76. var mybottom = this.y + (this.height);
  77. var otherleft = otherobj.x;
  78. var otherright = otherobj.x + (otherobj.width);
  79. var othertop = otherobj.y;
  80. var otherbottom = otherobj.y + (otherobj.height);
  81. var crash = true;
  82. if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  83. crash = false;
  84. }
  85. return crash;
  86. }
  87. }
  88. function updateGameArea() {
  89. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  90. for (i = 0; i < myObstacles.length; i += 1) {
  91. if (myGamePiece.crashWith(myObstacles[i])) {
  92. return;
  93. }
  94. }
  95. myGameArea.clear();
  96. myGameArea.frameNo += 1;
  97. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  98. x = myGameArea.canvas.width;
  99. minHeight = 20;
  100. maxHeight = 200;
  101. height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  102. minGap = 50;
  103. maxGap = 200;
  104. gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  105. myObstacles.push(new component(10, height, "green", x, 0));
  106. myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  107. }
  108. for (i = 0; i < myObstacles.length; i += 1) {
  109. myObstacles[i].x += -1;
  110. myObstacles[i].update();
  111. }
  112. myScore.text="SCORE: " + myGameArea.frameNo;
  113. myScore.update();
  114. myGamePiece.newPos();
  115. myGamePiece.update();
  116. }
  117. function everyinterval(n) {
  118. if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  119. return false;
  120. }
  121. function accelerate(n) {
  122. myGamePiece.gravity = n;
  123. }
  124. </script>
  125. <br>
  126. <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
  127. <p>Use the ACCELERATE button to stay in the air</p>
  128. <p>How long can you stay alive?</p>
  129. </body>
  130. </html>