Skip to content

Instantly share code, notes, and snippets.

@kindy61
Created January 24, 2011 08:52
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 kindy61/792978 to your computer and use it in GitHub Desktop.
Save kindy61/792978 to your computer and use it in GitHub Desktop.
//俄罗斯方块 http://js1k.com/2010-first/demo/663
w=document.body.children.c,
E=4099,
C=8191,
e=l=n=L=0;
R=eval(localStorage['x'])||[];
with(w.getContext('2d')){
// Get block
I=function(a,c,d){return(i=[[[2,7],[2,6,2],[0,7,2],[2,3,2]],[[1,3,2],[6,3]],[[2,3,1],[3,6]],[[2,2,6],[0,7,1],[3,2,2],[4,7]],[[2,2,3],[1,7],[6,2,2],[0,7,4]],[[2,2,2,2],[0,15]],[[3,3]]][a%7])[(a+c)%i.length]};
// Draw
P=function(a,c,d){
clearRect(0,0,1,w.height=171);
v(m,0,0);
v(I(l,0),14,1);
v(I(k,r),x,y);
fillText('Level '+(L/10|0),117,60);
fillText('Lines '+L,117,70);
fillText('Points '+n,117,80);
for(i in R)fillText(R[i][1]+' '+R[i][0],117,99+i*9)
};
// Next block
h=function(a,c,d){
k=l;
l=Math.random(r=0)*E|0;x=6;y=-1
};
// Fix block
M=function(a,c,d){
for(i in (o=I(k,r)))
m[y+(i|0)]|=o[i]<<x;
for(i=o=18,a=0;--o;m[o]-C?m[--i]=m[o]:a++);
for(h();--i;m[i]=E)L++;
a-->0&&(n+=100*1<<a);
t(0,0,0)||s()
};
// Test move
t=function(a,c,d){
for(i in (o=I(k,r+d)))
if(o[i]<<x+a&m[y+c+i++])
return c&&M();
r+=d;
x+=a;
P(y+=c);
return 1
};
// Draw block
v=function(a,c,d){
for(i in a){
for(i=a[i],o=c-1;i;i%2&&fillRect(o*9,d*9,8,8),o++,i>>=1);
d++
}
};
// Update high score
B=function(a,c,d){
for(i=R.length;i--&&R[i][1]<n;)R[i+1]=R[i];R[i+1]=[prompt('Name'),n];
localStorage['x']=JSON.stringify(R);
};
// Start new game
s=function(a,c,d){
n&&B(L=0);
m=[];
for(m[n=18]=C;n;m[--n]=E)h()
};
document.onkeydown=function(a,c,d){
t((a=a.keyCode-37)?a-2?0:1:-1,a-3?0:1,a==1&1)
};
setInterval('if(++e>16-L/10){t(0,1,e=0)}',40);
s()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment