Coder Social home page Coder Social logo

astroman's Introduction

AstromanJsGame

Astroman is a spacebased sidescroller implemented with vanilia javascript and HTML 5 Canvas.

Feature Highlights

  • Use Object Oriented Design to modularize display class for rendering on HTML 5 Canvas
  root.CanvasDisplay = function(parent, level) {
    this.canvas = document.createElement('canvas');
    this.canvas.width = Math.min(600, level.width * scale);
    this.canvas.height = Math.min(450, level.height * scale);

    this.context = this.canvas.getContext('2d');
    parent.appendChild(this.canvas);
    this.level = level;
    this.animationTime = 0;
    this.fireTimer = 0;
    this.flippedPlayer = false;
  ....
  • Implements sprite animation for character movement
// User Sprits and canvas drawImage to animate player character 
// Give default sprite for jump animation and no movement 
  CanvasDisplay.prototype.drawPlayer = function(x, y, height, width) {
    var spriteIdx = 5;
    var player = this.level.player;
    var jump = 6;
    var playerImage = document.createElement('img');
    playerImage.src = 'sprites/spaceman.png';
    if (player.speed.x != 0)
      this.flippedPlayer = player.speed.x < 0;

    if (player.speed.y != 0) {
      spriteIdx = jump;
    }

    else if (player.speed.x != 0) {
      var spriteIdx = Math.floor(this.animationTime * 12) % 10;
    }

    this.context.save();
    if (this.flippedPlayer) {
      flipHorizontally(this.context, x + (width + 8) / 2);
    }
    this.context.drawImage(playerImage, 
                      spriteIdx * (width + 8), 0, width + 8, height,
                      x,                 y, width + 8, height);

    this.context.restore();
  };
  • Takes advantue requestAnimationFrame to keep animations smooth and synced with browser refresh
// Keeps track of time between current animation and last animation frames
// and passes in this timestep to the animateLevel method, which throttles  
// the maximum timestep to keep accurate collision detections 
function runAnimation(animateLevel) {
var lastTime = null;
function frame(time) {
  var stop = false;
  if (lastTime != null) {
    var timeStep = Math.min(time - lastTime, 100)/1000;
    stop = animateLevel(timeStep);
  }
  lastTime = time;
  if (!stop)
    requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
}

##TODOS

  • Implement sound
  • Implement other NPC enemy characters
  • Implement lasers to destroy said NPCs

astroman's People

Contributors

jmenestr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.