Skip to content

Instantly share code, notes, and snippets.

@NDR149
Created December 8, 2016 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NDR149/9332ed6add3c83eed280609036b6aad7 to your computer and use it in GitHub Desktop.
Save NDR149/9332ed6add3c83eed280609036b6aad7 to your computer and use it in GitHub Desktop.
//Helper Functions
//Display
function Display(width, height) {
this.canvas = document.createElement("canvas");
this.canvas.width = this.width = width;
this.canvas.height = this.height = height;
this.ctx = this.canvas.getContext("2d");
document.body.appendChild(this.canvas);
}
Display.prototype.clear = function() {
this.ctx.clearRect(0, 0, this.width, this.height);
}
Display.prototype.drawSprite = function(sp, x, y) {
this.ctx.drawImage(sp.img, sp.x, sp.y, sp.w, sp.h, x, y, sp.w, sp.h);
};
//Sprite
function Sprite(img, x, y, w, h) {
this.img = img;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
//InputHandler
function InputHandler() {
this.down = {};
this.pressed = {};
var _this = this;
document.addEventListener("keydown", function(evt) {
_this.down[evt.keyCode] = true;
});
document.addEventListener("keyup", function(evt) {
delete _this.down[evt.keyCode];
delete _this.pressed[evt.keyCode];
});
}
InputHandler.prototype.isDown = function() {
return this.down[code];
};
InputHandler.prototype.isPressed = function() {
if (this.pressed[code]) {
return false;
} else if (this.down[code]) {
return this.pressed[code] = true;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment