Advertisement
andruhovski

JS

Jan 26th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let canvas, ctx, requestId;
  2. let gameObjects = [];
  3. let board;
  4.  
  5. class Point {
  6.     constructor(x, y) {
  7.         this.x = x;
  8.         this.y = y;
  9.     }
  10. }
  11.  
  12. class Vector2D {
  13.     constructor(velocityX, velocityY) {
  14.         this.vX = velocityX;
  15.         this.vY = velocityY;
  16.     }
  17. }
  18.  
  19. class GameObject {
  20.     constructor(spriteName, startPoint, velocity, bounceSound, shootSound) {
  21.         this.spriteImage = new Image();
  22.         this.spriteImage.src = spriteName;
  23.         this.currentPoint = startPoint;
  24.         this.velocity = velocity;
  25.         this.bounceSound = bounceSound;
  26.         this.shootSound = shootSound;
  27.         this.firinDelay = 2; //seconds
  28.         this.active = true;
  29.     };
  30.     Draw(ctx) {
  31.         ctx.drawImage(this.spriteImage, this.currentPoint.x, this.currentPoint.y);
  32.     };
  33.     Update() {
  34.         this.currentPoint.y += this.velocity.vY;
  35.         this.currentPoint.x += this.velocity.vX;
  36.  
  37.         if ((this.currentPoint.x > 400 - this.spriteImage.width) || (this.currentPoint.x < 0)) {
  38.             this.velocity.vX = -this.velocity.vX;
  39.         }
  40.         if ((this.currentPoint.y > 400 - this.spriteImage.height) || (this.currentPoint.y < 0)) {
  41.             this.velocity.vY = -this.velocity.vY;
  42.         }
  43.     };
  44. }
  45.  
  46. class TeddyBear extends GameObject {
  47.     constructor(startPoint, velocity, bounceSound, shootSound) {
  48.         super("teddybear.png", startPoint, velocity, bounceSound, shootSound);
  49.     };
  50. }
  51.  
  52. class Board {
  53.     constructor(rows, cols) {
  54.         this.rows = rows;
  55.         this.cols = cols;
  56.         this.labirinth = [];
  57.         for (let i = 0; i < rows; i++) {
  58.             this.labirinth[i] = new Array(cols);
  59.             for (let j = 0; j < cols; j++) {
  60.                 this.labirinth[i][j] = Math.random() * 100;
  61.             }
  62.         }
  63.     }
  64.  
  65.     draw(context) {
  66.  
  67.  
  68.         for (let i = 0; i < this.rows; i++) {
  69.             for (let j = 0; j < this.cols; j++) {
  70.                 context.beginPath();
  71.                 context.lineWidth = 1;
  72.                 context.strokeStyle = "black";
  73.                 if (i < j) {
  74.                     context.fillStyle = "yellow";
  75.                 } else {
  76.                     context.fillStyle = "red";
  77.                 }
  78.                 context.rect(i * 40, j * 40, 40, 40);
  79.                 context.fill();
  80.                 context.stroke();
  81.             }
  82.         }
  83.  
  84.     }
  85. }
  86.  
  87. function init() {
  88.     // This function is called after the page is loaded
  89.     // Get the canvas
  90.     canvas = document.getElementById('myCanvas');
  91.     // Get the context
  92.     ctx = canvas.getContext('2d');
  93.     // Init gameTime
  94.     //gameTime = 0;
  95.     board = new Board(10, 10);
  96.     board.draw(ctx);
  97.     gameObjects.push(new TeddyBear(new Point(10, 10), new Vector2D(1, 3), null, null));
  98.     gameObjects.push(new TeddyBear(new Point(70, 70), new Vector2D(-1, -2), null, null));
  99. }
  100.  
  101. function animationLoop(timestamp) {
  102.     // 1 - Clear
  103.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  104.  
  105.     // 2 - Draw
  106.     for (let gameObject of gameObjects) {
  107.         gameObject.Draw(ctx);
  108.         gameObject.Update();
  109.     }
  110.  
  111.     // 3 - Check collision
  112.  
  113.     // call again mainloop after 16.6 ms (60 frames/s)
  114.     requestId = requestAnimationFrame(animationLoop);
  115. }
  116.  
  117. function start() {
  118.     // Start the animation loop, targets 60 frames/s
  119.     requestId = requestAnimationFrame(animationLoop);
  120. }
  121.  
  122. function stop() {
  123.     if (requestId) {
  124.         cancelAnimationFrame(requestId);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement