@davetayls
I work at Pogo Kid
the controller
function logicFrame(){
// update players position, listen for collisions etc
// process the game logic at a target of 60fps
setTimeout(logicFrame, 1000/60);
}
function drawFrame(){
// kick ass circles and squares
window.requestAnimationFrame(drawFrame);
}
the browser can optimise concurrent animations
together into a single reflow and repaint cycle
takes the current state and applies
moves a ship / fires a bullet
function Ship(){...}
Ship.prototype = {
// we need to be able to position it
x:0, y:0, w:28, h:16
// it needs to do stuff
shoot: function(){...}
jumpLeft: function(){...}
jumpRight: function(){...}
// we need to see it
draw: function(){
this.sprite.draw(0, this.x, this.y);
}
};
function Invader(){...}
Invader.prototype = {
// we need to be able to position it
x:0, y:0, w:28, h:16,
// how eeeeeeevil is this invader?
isHit: false, // not at all if it's hit
points: 10, // more points for tougher invaders
// it needs to do stuff
checkHit: function(x, y){
if (distance(this.x, this.y, x, y) < 10){
this.isHit = true; // KAPOOOOOWWWWW
}
},
// we need to see it
draw: function(){
this.sprite.draw(0, this.x, this.y);
}
};
keep state
shootKey = {
keyCode: 32, // space
down: false };
window.addEventListener('keydown', function(e){
if (shootKey.keyCode === e.which){
shootKey.down = true;
e.preventDefault();
}
});
function logicFrame(){
if (shootKey.down){
// trigger shot!
}
// other game logic
setTimeout(logicFrame, 1000/60);
}
<canvas id="canvas" width="500" height="500"></canvas>
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d')
;
context.clearRect(0, 0, w, h);
context.fillStyle = '#000';
context.fillRect(0,0,w,h);
var img = new Image();
img.src = 'http://davetayls.me/space-invaders/sprites.png';
var spriteWidth = 350,
spriteHeight = 170,
pixelsLeft = 170,
pixelsTop = 10,
// Where are we going to draw
// the sprite on the canvas canvasPosX = 20,
canvasPosY = 20
;
context.drawImage(img,
pixelsLeft, pixelsTop,
spriteWidth, spriteHeight,
canvasPosX, canvasPosY,
spriteWidth, spriteHeight
);
function Sprite(img, width, height, positions){
this.img = img;
this.width = width; this.height = height;
this.positions = positions;
}
Sprite.prototype = {
draw: function(position, x, y){
var pos = this.positions[position];
context.drawImage(
this.img,
pos[0], pos[1], // xy position of sprite
this.width, this.height,
x, y // xy position to draw on canvas
this.width, this.height
);
}
};
var sprite = new Sprite(img, 32, 16, [
// specify a few sprite locations
[10, 523], // green
[131, 523], // pink
[191, 523] // hit
]);
sprite.draw(0, 10, 200);
sprite.draw(1, 50, 200);
sprite.draw(2, 90, 200);