Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.22 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: #f1f1f1;
  9. }
  10. </style>
  11. </head>
  12. <body onload="startGame()">
  13. <script>
  14. var myGamePiece;
  15. var myObstacles = [];
  16. var myScore;
  17. var myBackground;
  18.  
  19. function startGame() {
  20.     myGamePiece = new component(30, 30, "red", 10, 120);
  21.     myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  22.     myBackground = new component(656, 270, "myGameBackground.jpg", 0, 0, "image");
  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.         window.addEventListener('keydown', function (e) {
  36.             myGameArea.keys = (myGameArea.keys || []);
  37.             myGameArea.keys[e.keyCode] = (e.type == "keydown");
  38.         })
  39.         window.addEventListener('keyup', function (e) {
  40.             myGameArea.keys[e.keyCode] = (e.type == "keydown");
  41.         })
  42.     },
  43.     clear : function() {
  44.         this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  45.     },
  46.     stop : function() {
  47.         clearInterval(this.interval);
  48.     }
  49. }
  50.  
  51. function component(width, height, color, x, y, txtType, imgType) {
  52.     this.txtType = txtType;
  53.     this.imgType = imgType;
  54.     if (imgType == "image") {
  55.         this.image = new Image();
  56.         this.image.src = color;
  57.     }
  58.     this.width = width;
  59.     this.height = height;
  60.     this.x = x;
  61.     this.y = y;
  62.     this.update = function() {
  63.         ctx = myGameArea.context;
  64.         if (imgType == "image") {
  65.             ctx.drawImage(this.image,
  66.             this.x,
  67.             this.y,
  68.             this.width, this.height);
  69.         } else {
  70.         if (this.txtType == "text") {
  71.             ctx.font = this.width + " " + this.height;
  72.             ctx.fillStyle = color;
  73.             ctx.fillText(this.text, this.x, this.y);
  74.         } else {
  75.         ctx.fillStyle = color;
  76.         ctx.fillRect(this.x, this.y, this.width, this.height);
  77.         }
  78.     }
  79.     this.newPos = function() {
  80.         this.x += this.speedX;
  81.         this.y += this.speedY;
  82.     }
  83.     this.crashWith = function(otherobj) {
  84.         var myleft = this.x;
  85.         var myright = this.x + (this.width);
  86.         var mytop = this.y;
  87.         var mybottom = this.y + (this.height);
  88.         var otherleft = otherobj.x;
  89.         var otherright = otherobj.x + (otherobj.width);
  90.         var othertop = otherobj.y;
  91.         var otherbottom = otherobj.y + (otherobj.height);
  92.         var crash = true;
  93.         if ((mybottom < othertop) ||
  94.             (mytop > otherbottom) ||
  95.             (myright < otherleft) ||
  96.             (myleft > otherright)) {
  97.         crash = false;
  98.         }
  99.             return crash;
  100.     }
  101.     }
  102. }
  103.  
  104. function updateGameArea() {
  105.     var x, y;
  106.     for (i = 0; i < myObstacles.length; i += 1) {
  107.        if (myGamePiece.crashWith(myObstacles[i])) {
  108.            myGameArea.stop();
  109.            return;
  110.        }
  111.    }
  112.    myGameArea.clear();
  113.    myGameArea.frameNo += 1;
  114.    if (myGameArea.frameNo == 1 || everyinterval(150)) {
  115.        x = myGameArea.canvas.width;
  116.        minHeight = 20;
  117.        maxHeight = 200;
  118.        height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
  119.        minGap = 50;
  120.        maxGap = 200;
  121.        gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
  122.        myObstacles.push(new component(10, height, "green", x, 0));
  123.        myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  124.    }
  125.    for (i = 0; i < myObstacles.length; i += 1) {
  126.        myObstacles[i].x += -1;
  127.        myObstacles[i].update();
  128.    }
  129.     myGamePiece.speedX = 0;
  130.     myGamePiece.speedY = 0;
  131.     if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -1;}
  132.     if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 1;}
  133.     if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -1;}
  134.     if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 1;}
  135.    myScore.text="SCORE: " + myGameArea.frameNo;
  136.     myBackground.newPos();
  137.     myBackground.update();
  138.    myScore.update();
  139.     myGamePiece.newPos();
  140.     myGamePiece.update();
  141. }
  142.  
  143. function everyinterval(n) {
  144.    if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
  145.    return false;
  146. }
  147. </script>
  148.  
  149. </body>
  150. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement