Skip to content

Latest commit

 

History

History
64 lines (48 loc) · 1012 Bytes

README.md

File metadata and controls

64 lines (48 loc) · 1012 Bytes

game-engine

A lightweight JavaScript Game Engine

Example


import GameEngine from '...';

const {Game, Surface, Shape} = GameEngine;

class Entity extends Surface {

    constructor(x, y) {
        super(x, y, new Shape.Rect(60, 60));
    }
    
    update() {
        // ...
    }
    
    render(g) {
        const {x, y, shape} = this;
        g.fillRect(x, y, shape.width, shape.height);
    }
    
}

class ExampleGame extends Game {

    constructor() {
        super(window.innerWidth, window.innerHeight);
        this.bindEvent(window, 'resize');
        this.entity = new Entity(100, 100);
    }
    
    onresize() {
        this.resize(window.innerWidth, window.innerHeight);
    }
    
    update() {
        this.entity.update();
    }
    
    render(g) {
        super.render(g);
        this.entity.render(g);
    }
    
}

const game = new ExampleGame();
game.start();

document.body.appendChild(game.canvas);