cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
  <head>
  	<script type="text/javascript" src="game.js"></script>
  </head>

  <body>
		<canvas id="canvas"></canvas>
  </body>
</html>
            
          
!
            
              @import compass

canvas
  position: absolute
  top: 50%
  left: 50%
  margin: -200px
  border: 8px solid black
            
          
!
            
              // Mary Rose Cook, Hacker School Seminar W14
// https://github.com/maryrosecook/gamelivecode

;(function (exports) {
  var Game = function(canvasId) {
    this.WIDTH = 400;
    this.HEIGHT = 400;
    this.boxX = 20;

    var canvas = getCanvas(canvasId);
    sizeCanvas(canvas, this.WIDTH, this.HEIGHT);
    var drawingContext = getContext(canvas);

    this.createEntities();

    var self = this;
    this.startTick(function() {
      self.update();
      self.draw(drawingContext);
    });
  };

  Game.prototype = {
    x: 20,
    startTick: function(fn) {
      var tick = function() {
        fn();
        requestAnimationFrame(tick);
      };

      requestAnimationFrame(tick);
    },

    createEntities: function() {
      this.bullets = [];
      this.ship = new Ship(this);

      this.invaders = [];
      for (var i = 0; i < 24; i++) {
        this.invaders.push(new Invader(this, {
          x: 40 + 40 * (i % 8),
          y: 40 + 40 * (i % 3)
        }));
      }
    },

    shoot: function(center) {
      this.bullets.push(new Bullet(this.game, center));
    },

    update: function() {
      this.ship.update();

      for (var i = 0; i < this.invaders.length; i++) {
        this.invaders[i].update();
      }

      for (var i = 0; i < this.bullets.length; i++) {
        this.bullets[i].update();
      }

      for (var i = 0; i < this.bullets.length; i++) {
        if (entitiesColliding(this.ship, this.bullets[i])) {
          this.ship.color = "#f00";
        }
      }
    },

    draw: function(drawingContext) {
      drawingContext.clearRect(0, 0, this.WIDTH, this.HEIGHT);
      this.ship.draw(drawingContext);

      for (var i = 0; i < this.invaders.length; i++) {
        this.invaders[i].draw(drawingContext);
      }

      for (var i = 0; i < this.bullets.length; i++) {
        this.bullets[i].draw(drawingContext);
      }
    }
  };

  var sizeCanvas = function(canvas, width, height) {
    canvas.width = width;
    canvas.height = height;
  };

  var getContext = function(canvas) {
    return canvas.getContext('2d');
  };

  var getCanvas = function(canvasId) {
    return document.getElementById(canvasId);
  };

  var Ship = function(game) {
    this.game = game;
    this.size = { x: 20, y: 20 };
    this.color = "#000";
    this.center = { x: game.WIDTH / 2, y: game.HEIGHT - this.size.y };

    this.inputter = new Inputter();
  };

  Ship.prototype = {
    update: function() {
      if (this.inputter.isDown(37)) { // left
        this.center.x -= 2;
      } else if (this.inputter.isDown(39)) {
        this.center.x += 2;
      }
    },

    draw: function(drawingContext) {
      drawRectangleForObject(this, drawingContext);
    }
  };

  var drawRectangleForObject = function(obj, drawingContext) {
    drawingContext.fillStyle = obj.color;
    drawingContext.fillRect(obj.center.x, obj.center.y, obj.size.x, obj.size.y);
  };

  var Invader = function(game, center) {
    this.game = game;
    this.size = { x: 20, y: 20 };
    this.center = center;
    this.color = "#000";
  };

  Invader.prototype = {
    patrolSpeedX: 0,
    patrolPositionX: 0,
    update: function() {
      if (this.patrolPositionX <= 0) {
        this.patrolSpeedX = 0.5;
      } else if (this.patrolPositionX >= 20) {
        this.patrolSpeedX = -0.5;
      }

      this.patrolPositionX += this.patrolSpeedX;
      this.center.x += this.patrolSpeedX;

      if (Math.random() < 0.007) {
        this.game.shoot({ x: this.center.x, y: this.center.y });
      }
    },

    draw: function(drawingContext) {
      drawRectangleForObject(this, drawingContext);
    }
  };

  var Inputter = function() {
    var keyState = {};

    window.onkeydown = function(e) {
      keyState[e.keyCode] = true;
    };

    window.onkeyup = function(e) {
      keyState[e.keyCode] = false;
    };

    this.isDown = function(keyCode) {
      return keyState[keyCode] === true;
    };
  };

  var Bullet = function(game, center) {
    this.game = game;
    this.size = { x: 8, y: 8 };
    this.color = "#000";
    this.center = center;
    this.vector = { x: Math.random() - 0.5, y: Math.random() };
  };

  Bullet.prototype = {
    update: function() {
      this.center.x += this.vector.x;
      this.center.y += this.vector.y;
    },

    draw: function(drawingContext) {
      drawRectangleForObject(this, drawingContext);
    }
  };

  var entitiesColliding = function(e1, e2) {
    return !(e1.center.x + e1.size.x / 2 < e2.center.x - e2.size.x / 2 ||
             e1.center.y + e1.size.y / 2 < e2.center.y - e2.size.y / 2 ||
             e1.center.x - e1.size.x / 2 > e2.center.x + e2.size.x / 2 ||
             e1.center.y - e1.size.y / 2 > e2.center.y + e2.size.y / 2);
  };

  window.onload = function () {
    new Game("canvas");
  };
})(this);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console