Advertisement
LegendSujay2019

Making game using html

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