Skip to content

Instantly share code, notes, and snippets.

@rogeriochaves
Created May 22, 2013 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogeriochaves/5628653 to your computer and use it in GitHub Desktop.
Save rogeriochaves/5628653 to your computer and use it in GitHub Desktop.
JavaScript do jogo Quadrados
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