This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
loadImages('images', ['player.png', 'ufo.png'], initialize); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
loadImages('images', ['player.png', 'ufo.png'], initialize); | |
$(document).keydown(function(e) { | |
key = {37: 'left', 38: 'up', 39:'right', 40:'down'}[e.keyCode]; | |
player.input(key) | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player.input = function(key) { | |
switch(key){ | |
case 'left': this.x -= this.speed; break; | |
case 'right': this.x += this.speed; break; | |
case 'up': this.y -= this.speed; break; | |
case 'down': this.y += this.speed; break; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var player; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player = createEntity('player', {x:300,y:280, speed:4}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gameLoop() { | |
ctx.clearRect(0,0,canvas.width, canvas.height); | |
entities.forEach(function (entity) { | |
entity.move(); | |
}); | |
entities.forEach(function (entity) { | |
entity.draw(ctx); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setInterval(gameLoop, 1000/60); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
entities.forEach(function (entity) { | |
entity.draw(ctx); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createEntity('player', {x:300,y:280}) | |
createEntity('ufo', {x:0, y:20, vx:1, vy:0.1}) | |
createEntity('ufo', {x:120, y:80, vx:1.1, vy:0}) | |
createEntity('ufo', {x:40, y:130, vx:1.05, vy:0}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initCanvas(); |