Created
May 22, 2013 15:51
-
-
Save rogeriochaves/5628653 to your computer and use it in GitHub Desktop.
JavaScript do jogo Quadrados
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
Quadrados = new Meteor.Collection("quadrados"); | |
if (Meteor.isClient) { | |
Template.quadrados.quadrados = function () { | |
return Quadrados.find({}); | |
}; | |
Meteor.startup(function () { | |
var quadrado = Quadrados.insert({x: Math.random() * 500, y: Math.random() * 500}); | |
Session.set("quadrado", quadrado); | |
}); | |
$(document).keydown(function (e) { | |
var keyCode = e.keyCode || e.which; | |
var arrow = {37: 'left', 38: 'up', 39: 'right', 40: 'down'}; | |
var key = arrow[keyCode]; | |
if(key === 'right'){ | |
Quadrados.update(Session.get("quadrado"), {$inc: {x: 5}}); | |
}else if(key === 'left'){ | |
Quadrados.update(Session.get("quadrado"), {$inc: {x: -5}}); | |
}else if(key === 'up'){ | |
Quadrados.update(Session.get("quadrado"), {$inc: {y: -5}}); | |
}else if(key === 'down'){ | |
Quadrados.update(Session.get("quadrado"), {$inc: {y: 5}}); | |
} | |
}); | |
} | |
if (Meteor.isServer) { | |
Meteor.startup(function () { | |
Quadrados.remove({}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment