Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.08 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=3.0"/>
  5. <style>
  6. canvas {
  7.     border:1px solid #d3d3d3;
  8.     background-color: black;
  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, "blue", 10, 120);
  21.     myGamePiece.gravity = 0.80;
  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 = 960;
  30.         this.canvas.height = 540;
  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, 8);
  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.     this.score = 0;
  44.     this.width = width;
  45.     this.height = height;
  46.     this.speedX = 0;
  47.     this.speedY = 0;    
  48.     this.x = x;
  49.     this.y = y;
  50.     this.gravity = 0;
  51.     this.gravitySpeed = 0;
  52.     this.update = function() {
  53.         ctx = myGameArea.context;
  54.         if (this.type == "text") {
  55.             ctx.font = this.width + " " + this.height;
  56.             ctx.fillStyle = color;
  57.             ctx.fillText(this.text, this.x, this.y);
  58.         } else {
  59.             ctx.fillStyle = color;
  60.             ctx.fillRect(this.x, this.y, this.width, this.height);
  61.         }
  62.     }
  63.     this.newPos = function() {
  64.         this.gravitySpeed += this.gravity;
  65.         this.x += this.speedX;
  66.         this.y += this.speedY + this.gravitySpeed;
  67.         this.hitBottom();
  68.     }
  69.     this.hitBottom = function() {
  70.         var rockbottom = myGameArea.canvas.height - this.height;
  71.         if (this.y > rockbottom) {
  72.             this.y = rockbottom;
  73.             this.gravitySpeed = 0;
  74.         }
  75.     }
  76.     this.crashWith = function(otherobj) {
  77.         var myleft = this.x;
  78.         var myright = this.x + (this.width);
  79.         var mytop = this.y;
  80.         var mybottom = this.y + (this.height);
  81.         var otherleft = otherobj.x;
  82.         var otherright = otherobj.x + (otherobj.width);
  83.         var othertop = otherobj.y;
  84.         var otherbottom = otherobj.y + (otherobj.height);
  85.         var crash = true;
  86.         if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
  87.             crash = false;
  88.         }
  89.         return crash;
  90.     }
  91. }
  92.  
  93. function updateGameArea() {
  94.     var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  95.     for (i = 0; i < myObstacles.length; i += 1) {
  96.        if (myGamePiece.crashWith(myObstacles[i])) {
  97.            return;
  98.        }
  99.    }
  100.    myGameArea.clear();
  101.    myGameArea.frameNo += 1;
  102.    if (myGameArea.frameNo == 1 || everyinterval(150)) {
  103.        x = myGameArea.canvas.width;
  104.        minHeight = 20;
  105.        maxHeight = 200;
  106.        height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  107.        minGap = 50;
  108.        maxGap = 200;
  109.        gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  110.        myObstacles.push(new component(10, height, "white", x, 0));
  111.        myObstacles.push(new component(10, x - height - gap, "white", x, height + gap));
  112.    }
  113.    for (i = 0; i < myObstacles.length; i += 1) {
  114.        myObstacles[i].x += -1;
  115.        myObstacles[i].update();
  116.    }
  117.    myScore.text="PUNKTACJA: " + myGameArea.frameNo;
  118.    myScore.update();
  119.    myGamePiece.newPos();
  120.    myGamePiece.update();
  121. }
  122.  
  123. function everyinterval(n) {
  124.    if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  125.    return false;
  126. }
  127.  
  128. function accelerate(n) {
  129.    myGamePiece.gravity = n;
  130. }
  131. </script>
  132. <br>
  133. <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">PRZYŚPIESZENIE</button>
  134. <p>Użyj przycisku PRZYŚPIESZENIE by wzlecieć w powietrze</p>
  135. <p>Jak długo przetrwasz?</p>
  136. </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement