Tetris written in pure JavaScript

From , 3 Years ago, written in JavaScript, viewed 51 times.
URL https://pastebin.vip/view/c5ad7d5c
  1. <html>
  2. <style>.c {margin :1px;width:19px;height:19px;background:red;position:absolute;}
  3. .d {margin :1px;width:19px;height:19px;background:gray;position:absolute;}
  4. .f {top:0px;left:0px;background:black;position:absolute;}
  5. </style>
  6. <body></body><html>
  7. <script>
  8.     var over=false,shapes=("0,1,1,1,2,1,3,1;1,0,1,1,1,2,2,2;2,0,2,1,2,2,1,2;0,1,1,1,1,2,2,2;1,2,2,2,2,1,3,1;1,1,2,1,1,2,2,2;0,2,1,2,1,1,2,2").split(";");
  9.     function create(tag,css){
  10.         var elm=document.createElement(tag);
  11.         elm.className = css;
  12.         document.body.appendChild(elm);
  13.         return elm;}
  14.     function Tetris(c, t, x, y){
  15.         var c=c?c:"c";
  16.         this.divs = [create("div",c),create("div",c),create("div",c),create("div",c)];
  17.         this.reset = function(){
  18.             this.x = typeof x != 'undefined'?x:3;
  19.             this.y = typeof y != 'undefined'?y:0;
  20.             this.shape = t?t:shapes[Math.floor(Math.random()*(shapes.length-0.00001))].split(",");
  21.             this.show();
  22.             if(this.field&&this.field.check(this.shape,this.x,this.y,'v')=='D'){
  23.                 over=true;
  24.                 this.field.fixShape(this.shape,this.x,this.y);
  25.                 alert('game over');}}
  26.         this.show = function(){
  27.             for(var i in this.divs){
  28.                 this.divs[i].style.left = (this.shape[i*2]*1+this.x)*20+'px';
  29.                 this.divs[i].style.top = (this.shape[i*2+1]*1+this.y)*20+'px';}}
  30.         this.field=null;
  31.         this.hMove = function(step){
  32.             var r = this.field.check(this.shape,this.x- -step,this.y,'h');
  33.             if(r!='N'&&r==0){
  34.                 this.x-=-step;
  35.                 this.show();}}
  36.         this.vMove = function(){
  37.             if(this.field.check(this.shape,this.x,this.y- -1,'v')=='N'){
  38.                 this.y++;
  39.                 this.show();}
  40.             else{
  41.                 this.field.fixShape(this.shape,this.x,this.y);
  42.                 this.field.findFull();
  43.                 this.reset();}}
  44.         this.rotate = function(){
  45.             var s=this.shape;
  46.             var newShape=[3-s[1],s[0],3-s[3],s[2],3-s[5],s[4],3-s[7],s[6]];
  47.             var r = this.field.check(newShape,this.x,this.y,'h');
  48.             if(r=='D')return;
  49.             if(r==0){
  50.                 this.shape=newShape;
  51.                 this.show();}
  52.             else if(this.field.check(newShape,this.x-r,this.y,'h')==0){
  53.                 this.x-=r;
  54.                 this.shape=newShape;
  55.                 this.show();}}
  56.         this.reset();}
  57.     function Field(w,h){
  58.         this.width = w?w:10;
  59.         this.height = h?h:20;
  60.         this.show = function(){
  61.             var f = create("div","f")
  62.             f.style.width=this.width*20+'px';
  63.             f.style.height=this.height*20+'px';}
  64.         this.findFull = function(){
  65.             for(var l=0;l<this.height;l++){
  66.                 var s=0;
  67.                 for(var i=0;i<this.width;i++){
  68.                     s+=this[l*this.width+i]?1:0;}
  69.                 if(s==this.width){
  70.                     this.removeLine(l);}}}
  71.         this.removeLine = function(line){
  72.             for(var i=0;i<this.width;i++){
  73.                 document.body.removeChild(this[line*this.width+i]);}
  74.             for(var l=line;l>0;l--){
  75.                 for(var i=0;i<this.width;i++){
  76.                     this[l*this.width- -i]=this[(l-1)*this.width- -i];
  77.                     if(this[l*this.width- -i])this[l*this.width- -i].style.top = l*20+'px';}}}
  78.         this.check = function(shape, x, y, d){
  79.             var r1=0,r2='N';
  80.             for(var i=0;i<8;i+=2){
  81.                 if(shape[i]- -x < 0 && shape[i]- -x <r1)
  82.                     {r1 = shape[i]- -x;}
  83.                 else if(shape[i]- -x>=this.width && shape[i]- -x>r1)
  84.                     {r1 = shape[i]- -x;}
  85.                 if(shape[i+1]- -y>=this.height || this[shape[i]- -x- -(shape[i+1]- -y)*this.width])
  86.                     {r2='D'}}
  87.             if(d=='h'&&r2=='N')return r1>0?r1-this.width- -1:r1;
  88.             else return r2;}
  89.         this.fixShape = function(shape,x,y){
  90.             var d=new Tetris("d",shape,x,y);
  91.             d.show();
  92.             for(var i=0;i<8;i+=2){
  93.                 this[shape[i]- -x- -(shape[i+1]- -y)*this.width]=d.divs[i/2];}}}
  94.     var f = new Field();
  95.     f.show();
  96.     var s = new Tetris();
  97.     s.field = f;
  98.     s.show();
  99.     window.setInterval("if(!over)s.vMove();",500);
  100.     document.onkeydown = function(e){
  101.         if(over)return;
  102.         var e = window.event ? window.event : e;
  103.         switch(e.keyCode){
  104.         case 38: //up
  105.             s.rotate();
  106.             break;
  107.         case 40: //down
  108.             s.vMove();
  109.             break;
  110.         case 37: //left
  111.             s.hMove(-1);
  112.             break;
  113.         case 39: //right
  114.             s.hMove(1);
  115.             break;}}
  116. </script>
  117.  
  118. //javascript/277

Reply to "Tetris written in pure JavaScript"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website