Advertisement
callmejeki

game gaje

May 28th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>GAME</title>
  7. <style>
  8. canvas {
  9. border: 1px solid #d3d3d3;
  10. background-color: #f1f1f1;
  11. }
  12. </style>
  13. </head>
  14. <body onload="startGame()">
  15. <br />
  16. <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
  17. <p>Use the ACCELERATE button to stay in the air</p>
  18. <p>How long can you stay alive?</p>
  19. <script>
  20. var myGamePiece;
  21. var myObstacles = [];
  22. var myScore;
  23.  
  24. function startGame() {
  25. myGamePiece = new component(30, 30, "red", 10, 120);
  26. myGamePiece.gravity = 1;
  27. myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  28. myGameArea.start();
  29. }
  30.  
  31. var myGameArea = {
  32. canvas: document.createElement("canvas"),
  33. start: function () {
  34. this.canvas.width = 480;
  35. this.canvas.height = 270;
  36. this.context = this.canvas.getContext("2d");
  37. document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  38. this.frameNo = 0;
  39. this.interval = setInterval(updateGameArea, 20);
  40. },
  41. clear: function () {
  42. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  43. },
  44. };
  45.  
  46. function component(width, height, color, x, y, type) {
  47. this.type = type;
  48. this.text = "";
  49. this.width = width;
  50. this.height = height;
  51. this.speedX = 0;
  52. this.speedY = 0.05;
  53. this.x = x;
  54. this.y = y;
  55. this.gravity = 0.2;
  56. this.gravitySpeed = 0.1;
  57. this.update = function () {
  58. ctx = myGameArea.context;
  59. if (this.type == "text") {
  60. ctx.font = this.width + " " + this.height;
  61. ctx.fillStyle = color;
  62. ctx.fillText(this.text, this.x, this.y);
  63. } else {
  64. ctx.fillStyle = color;
  65. ctx.fillRect(this.x, this.y, this.width, this.height);
  66. }
  67. };
  68. this.newPos = function() {
  69. this.gravitySpeed += this.gravity;
  70. this.x += this.speedX;
  71. this.y += this.speedY + this.gravitySpeed;
  72. this.hitBottom();
  73. };
  74. this.hitBottom = function () {
  75. var rockbottom = myGameArea.canvas.height - this.height;
  76. if (this.y > rockbottom) {
  77. this.y = rockbottom;
  78. this.gravitySpeed = 0;
  79. }
  80. };
  81. this.crashWith = function(otherobj) {
  82. var myleft = this.x;
  83. var myright = this.x + this.width;
  84. var mytop = this.y;
  85. var mybottom = this.y + this.height;
  86. var otherleft = otherobj.x;
  87. var otherright = otherobj.x + otherobj.width;
  88. var othertop = otherobj.y;
  89. var otherbottom = otherobj.y + otherobj.height;
  90. var crash = true;
  91. if (mybottom < othertop || mytop > otherbottom || myright < otherleft || myleft > otherright) {
  92. crash = false;
  93. }
  94. return crash;
  95. };
  96. }
  97.  
  98. function updateGameArea() {
  99. var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  100. for (i = 0; i < myObstacles.length; i += 1) {
  101. if (myGamePiece.crashWith(myObstacles[i])) {
  102. return;
  103. }
  104. }
  105. myGameArea.clear();
  106. myGameArea.frameNo += 1;
  107. if (myGameArea.frameNo == 1 || everyinterval(150)) {
  108. x = myGameArea.canvas.width;
  109. minHeight = 20;
  110. maxHeight = 200;
  111. height = Math.floor(Math.random() * (maxHeight - minHeight + 1) + minHeight);
  112. minGap = 50;
  113. maxGap = 200;
  114. gap = Math.floor(Math.random() * (maxGap - minGap + 1) + minGap);
  115. myObstacles.push(new component(10, height, "pink", x, 0));
  116. myObstacles.push(new component(10, x - height - gap, "pink", x, height + gap));
  117. }
  118. for (i = 0; i < myObstacles.length; i += 1) {
  119. myObstacles[i].x += -1;
  120. myObstacles[i].update();
  121. }
  122. myScore.text = "SCORE: " + myGameArea.frameNo;
  123. myScore.update();
  124. myGamePiece.newPos();
  125. myGamePiece.update();
  126. }
  127.  
  128. function everyinterval(n) {
  129. if ((myGameArea.frameNo / n) % 1 == 0) {
  130. return true;
  131. }
  132. return false;
  133. }
  134.  
  135. function accelerate(n) {
  136. myGamePiece.gravity = n;
  137. }
  138. </script>
  139. </body>
  140. </html>
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement