Super Dodger

by Nathan Piper

HTML

<canvas id="player" width=500 height=400 style="border: 1px solid black" />
<canvas id="gui" width=500 height=400 style="border: 1px solid black"/>

CSS

canvas {
    background: black;
}

JavaScript

var fps = 60;
var width = 500;
var height = 400;
var mines = [];
var gameState = "menu";
var time = 0;
var countDown = 3;
var round = 1;
var mineCount = 1;

var pG = document.getElementById("player").getContext('2d');
var GUI = document.getElementById("gui").getContext('2d');

var player = {
    x: width / 2 - 20,
    y: height - 20,
    speed: 5,
    width: 10,
    height: 10,
    render: function () {
        pG.fillStyle = "aqua";
        pG.fillRect(this.x, this.y, this.width, this.height);
              pG.fillStyle = "red";
        pG.fillRect(this.x, 20, this.width, this.height);
    },
    tick: function () {
        if (Key.up && this.y > 0) this.y -= this.speed;
        if (Key.down && this.y < height - 20) this.y += this.speed;
        if (Key.left && this.x > 0) this.x -= this.speed;
        if (Key.right && this.x < width - 20) this.x += this.speed;
    }
}


var healthBar = {
    x: 100,
    y: 100,
    width: 120,
    heigth: 10,
    render: function () {
        pG.fillStyle = "red";
        pG.fillRect(this.x, this.y, this.width, this.height);
    },
    tick: function () {}
}


var Key = {
    up: false,
    down: false,
    right: false,
    left: false,
    space: false
}

addEventListener("keydown", function (e) {
    var keyCode = (e.keyCode) ? e.keyCode : e.which;

    switch (keyCode) {
        case 38:
            Key.up = true;
            break;
        case 40:
            Key.down = true;
            break;
        case 39:
            Key.right = true;
            break;
        case 37:
            Key.left = true;
            break;
        case 32:
            Key.space = true;
            break;
    }
}, false);


addEventListener("keyup", function (e) {
    var keyCode = (e.keyCode) ? e.keyCode : e.which;

    switch (keyCode) {
        case 38:
            Key.up = false;
            break;
        case 40:
            Key.down = false;
            break;
        case 39:
            Key.right = false;
            break;
      ...