Skip to content

Instantly share code, notes, and snippets.

@logsol
Created September 20, 2012 17:25
Show Gist options
  • Save logsol/3757202 to your computer and use it in GitHub Desktop.
Save logsol/3757202 to your computer and use it in GitHub Desktop.
its invisible
var gume = (function($){
var can, ctx, plas, me, ks, ysp;
var con={lk:65,rk:68,uk:87,sth:20,stw:200, grav: .3}
function init(){
plas=[];
ks={};
ysp=con.grav;
if($('#gume')) $('#gume').remove();
can=$('<canvas>').attr('id', 'gume').attr('width', con.stw).attr('height', con.sth);
can.css('position', 'absolute').css('top', 0).css('left', 0).css('z-index', 100000);
$('body').append(can);
ctx=can.get(0).getContext('2d');
me=new Pla("rgba(255, 0, 0, 0.5)");
plas.push(me);
skb();
setInterval(up, 1000/50);
}
function skb(){
var ooku=(typeof window.onkeyup === 'function') ? window.onkeyup : function(){};
var ookd=(typeof window.onkeydown === 'function') ? window.onkeydown : function(){};
window.onkeyup=function(e){ if(e){ks[''+e.keyCode]=false;}ooku();}
window.onkeydown=function(e){if(e){ks[''+e.keyCode]=true;}ookd();}
}
function Pla(c){
this.col=c;
this.pos=[Math.floor(Math.random(5)*con.stw),0/*con.sth-1*/];
}
function ju(){
ysp=-2.5;
}
function ren(){
var x;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
for(x in plas){
ctx.fillStyle=plas[x].col;
ctx.fillRect(plas[x].pos[0],plas[x].pos[1],1,1);
}
}
function up(){
switch(true){
case ks[con.lk] && ks[con.rk]:
break;
case ks[con.uk] && me.pos[1] == con.sth-1:
ju(); break;
case ks[con.lk] && me.pos[0]>0:
--me.pos[0]; break;
case ks[con.rk] && me.pos[0]<con.stw-1:
++me.pos[0]; break;
default:
break;
}
if(me.pos[1] < con.sth-1) ysp+=con.grav;
me.pos[1]=Math.round(me.pos[1]+ysp);
if(me.pos[1] > con.sth-1) {
me.pos[1] = con.sth-1;
ysp=con.grav;
}
ren();
}
init();
}
)(jQuery);
$(document).ready(function(){ $('body').append($('<script>').attr('type', 'text/javascript').attr('src', 'gume.js')); })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment