-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.min.js
29 lines (29 loc) · 4 KB
/
game.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const initTetris=({width:a,height:b,pixelSize:c})=>{let d=Math.floor(a/2)+a+a,f=d,g=0,h=0,i=!1,j=!1;const k=()=>{document.querySelector(".tetris-score").innerHTML=h},l=()=>{document.querySelectorAll(".pixel-fill").forEach(a=>{a.classList.remove("pixel-fill")})},m=(a,b,c)=>{l(),a[b](c).forEach(a=>{document.querySelectorAll(".tetris-pixel")[a].classList.add("pixel-fill")})},n=(a,b,c)=>{l(),a[b](c).forEach(a=>{document.querySelectorAll(".tetris-pixel")[a].classList.add("pixel-stop")})},o=(a,b,c)=>!a[b](c).some(a=>document.querySelectorAll(".tetris-pixel")[a].classList.contains("pixel-stop")),p=()=>{for(var b=document.querySelectorAll(".tetris-pixel")[a],c=0;c<a;c++){let d=document.createElement("div");d.className="tetris-pixel"+(0==c||c==a-1?" pixel-stop":""),b.parentNode.insertBefore(d,b)}b.parentNode.insertBefore(document.createElement("br"),b)},q=()=>{let c=document.querySelectorAll(".tetris-pixel");const d=(a,b)=>-1<(" "+a.className+" ").indexOf(" "+b+" ");let e=0;for(var f=b-2;0<f;f--){let b=f*a+1,h=b+a-2,i=[b-1,h];for(var g=b;g<h;g++){let a=c[g];if(!d(a,"pixel-stop"))break;i.push(g)}i.length===a&&(i.forEach(a=>{let b=c[a];b.parentNode.removeChild(b)}),e++,p())}h+={0:0,1:40,2:100,3:300,4:1200}[e],k()};(()=>{let c=document.createElement("div");c.className="tetris-game",document.body.appendChild(c);for(var d=0;d<b;d++){for(var e=0;e<a;e++){let f=document.createElement("div");f.className="tetris-pixel"+(0==d||d==b-1||0==e||e==a-1?" pixel-stop":""),c.appendChild(f)}c.appendChild(document.createElement("br"))}let f=document.createElement("div");f.className="tetris-score",f.innerHTML="0",c.appendChild(f)})(),(()=>{var a=document.createElement("style");a.innerHTML=`
.tetris-pixel{
width:${c}px;
height:${c}px;
display:inline-block;
box-sizing: border-box;
}
.tetris-score{
color: #37a48b;
font-size: 3em;
margin-top: 1em;
text-align: center;
font-family: monospace;
}
.tetris-pixel.pixel-stop{
background: #37a48b;
opacity:0.3
}
.tetris-pixel.pixel-fill{
background: #37a48b;
}
.tetris-game {
line-height: 0;
position: absolute;
top: 50%;
left: 50%;
z-index: 99999;
transform: translate(-50%, -50%);
}`,document.body.appendChild(a)})();const r=[[b=>[b-a,b,b+a,b+a-1],b=>(b=0==(b-2)%a?b+1:b,[b-2,b-1,b,b+a]),b=>[b-a,b-a-1,b-1,b+a-1],b=>(b=0==(b-2)%a?b+1:b,[b-a-2,b-2,b-1,b])],[b=>(b=0==(b-1)%a?b+1:b,[b-1,b,b+a,b+a+1]),b=>[b,b+1,b-a+1,b+a]],[b=>(b=0==(b-2)%a?b+1:b,[b,b-1,b+a-1,b+a-2]),b=>[b-1,b,b+a,b-a-1]],[b=>[b-a,b,b+a,b+a+1],b=>(b=0==(b-1)%a?b+1:b,[b-1,b,b+1,b-a+1]),b=>[b-a,b-a+1,b+1,b+a+1],b=>(b=0==(b-1)%a?b+1:b,[b-1+a,b-1,b,b+1])],[b=>(b=0==(b-2)%a?b+1:b,[b-2,b-1,b,b-a-1]),b=>[b-a-1,b-1,b,b+a-1],b=>(b=0==(b-2)%a?b+1:b,[b-2,b-1,b,b+a-1]),b=>[b-1,b,b-a,b+a]],[b=>[b-1,b,b+a,b+a-1]],[b=>[b-a,b,b+a,b+a+a],b=>(b=0==(b-1)%a||0==(b-2)%a||0==(b-3)%a?b+3:b,[b-3,b-2,b-1,b])]];let s=setInterval(()=>{i=i||r[Math.floor(Math.random()*r.length)],j=j||r[Math.floor(Math.random()*r.length)];let b={rotation:g,position:f};b.position+=a,o(i,b.rotation,b.position)?(f+=a,m(i,g,f)):o(j,0,d)?(n(i,g,f),f=d,g=0,i=j,j=r[Math.floor(Math.random()*r.length)],q()):(clearInterval(s),n(i,g,f))},1e3);document.onkeydown=b=>{let c={rotation:g,position:f};switch(b.keyCode){case 37:c={rotation:g,position:f},o(i,c.rotation,--c.position)&&m(i,g,--f),b.preventDefault();break;case 38:c={rotation:g,position:f},c.rotation=c.rotation>=i.length-1?0:c.rotation+1,o(i,c.rotation,c.position)&&(g=g>=i.length-1?0:g+1,m(i,g,f)),b.preventDefault();break;case 39:c={rotation:g,position:f},o(i,c.rotation,++c.position)&&m(i,g,++f),b.preventDefault();break;case 40:c={rotation:g,position:f},c.position+=a,o(i,c.rotation,c.position)&&(f+=a,m(i,g,f)),b.preventDefault();break;case 27:clearInterval(s);let d=document.querySelector(".tetris-game");d.parentNode.removeChild(d),b.preventDefault();}}};initTetris({width:10,height:20,pixelSize:35});