Tetris.java
Upload User: yadi2000
Upload Date: 2015-12-03
Package Size: 17k
Code Size: 10k
Category:

Games

Development Platform:

Java

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4. class myTimer extends Thread {
  5.     int time;
  6.     Tetris tetris;
  7.     public myTimer(Tetris t,int delay)
  8.     {
  9.         time=delay;
  10.         tetris=t;
  11.     }
  12.     public void setTimer(int t)
  13.     {
  14.         time=t;
  15.     }
  16.     public void run() {
  17.         while (true) {
  18.             try {
  19.                 sleep(time);
  20.             }
  21.             catch (InterruptedException e) {}
  22.             tetris.ondown();
  23.         }
  24.     }
  25. }
  26. public class Tetris extends Applet {
  27.     Button bStart,bNew;
  28.     Button bTurn,bLeft,bRight,bDown;
  29.     Label label1,label2;
  30.     TextField tLevel,tScore;
  31.     Panel panel1,panel2,panel3,panel4;
  32.     Board board;
  33.     Preview preview;
  34.     Label label3;
  35.     myTimer timer;
  36.     boolean IsStart;
  37.     Random r;
  38.     /*********************************/
  39.     final int LINE=20;
  40.     final int ROW =10;
  41.     final int NUM =7;  // Num=10;
  42.     final int COLORS =6;
  43.     int[][] box=new int[LINE][ROW];
  44.     int[][] pview=new int[4][4];
  45.     int[][] bl1=new int[4][4];
  46.     int[][] bl2=new int[4][4];
  47.     int x,y;
  48.     int total,score;
  49.     int nn,ss,n1,s1;
  50.     //boolean IsQuit;
  51.     int[] size={4,3,2,3,3,3,3,3,3,3};
  52.     int[] shape=
  53.        {0,1,0,0,
  54.         0,1,0,0,
  55.         0,1,0,0,
  56.         0,1,0,0, /*1*/
  57.         0,0,0,0,
  58.     1,1,1,0,
  59. 0,1,0,0,
  60. 0,0,0,0, /*2*/
  61. 1,1,0,0,
  62. 1,1,0,0,
  63. 0,0,0,0,
  64. 0,0,0,0, /*3*/
  65. 0,0,0,0,
  66. 1,1,1,0,
  67. 1,0,0,0,
  68. 0,0,0,0, /*4*/
  69. 0,0,0,0,
  70. 1,1,1,0,
  71. 0,0,1,0,
  72.         0,0,0,0, /*5*/
  73. 1,1,0,0,
  74. 0,1,1,0,
  75. 0,0,0,0,
  76. 0,0,0,0, /*6*/
  77. 0,1,1,0,
  78. 1,1,0,0,
  79. 0,0,0,0,
  80. 0,0,0,0, /*7*/
  81. 0,1,0,0,
  82. 1,1,1,0,
  83. 0,1,0,0,
  84. 0,0,0,0, /*8*/
  85. 1,1,1,0,
  86. 1,0,1,0,
  87. 0,0,0,0,
  88. 0,0,0,0, /*9*/
  89. 1,1,0,0,
  90. 0,1,0,0,
  91. 0,1,1,0,
  92. 0,0,0,0, /*10*/
  93.        };
  94.     /*********************************/
  95.     public void init()
  96.     {
  97.         bStart=new Button("Pause");
  98.         bNew  =new Button("New Game");
  99.         bTurn =new Button("T");
  100.         bLeft =new Button("<");
  101.         bRight=new Button(">");
  102.         bDown =new Button("V");
  103.         label1=new Label("Level:");
  104.         label2=new Label("Score:");
  105.         tLevel=new TextField();
  106.         tLevel.disable();
  107.         tScore=new TextField();
  108.         tScore.disable();
  109.         preview=new Preview(pview,4,4);
  110.         //preview.setBackground(Color.cyan);
  111.         board=new Board(box,ROW,LINE);
  112.         //board.setBackground(Color.black);
  113.         panel1=new Panel();
  114.         panel1.setLayout(new GridLayout(2,3));
  115.         panel1.add(bLeft);
  116.         panel1.add(bTurn);
  117.         panel1.add(bRight);
  118.         panel1.add(new Panel());
  119.         panel1.add(bDown);
  120.         panel1.add(new Panel());
  121.         panel2=new Panel();
  122.         panel2.setLayout(new GridLayout(2,1,10,10));
  123.         panel2.add(bNew);
  124.         panel2.add(bStart);
  125.         panel3=new Panel();
  126.         panel3.setLayout(new GridLayout(4,1));
  127.         panel3.add(label1);
  128.         panel3.add(tLevel);
  129.         panel3.add(label2);
  130.         panel3.add(tScore);
  131.         panel4=new Panel();
  132.         panel4.setLayout(new GridLayout(4,1));
  133.         panel4.add(panel2);
  134.         panel4.add(preview);
  135.         panel4.add(panel1);
  136.         panel4.add(panel3);
  137.         setLayout(new BorderLayout());
  138.         setBackground(Color.lightGray);
  139.         add("Center",board);
  140.         add("East",panel4);
  141.         label3=new Label("Tetris 1.0 made by Chen Shijie (9824053)");
  142.         add("South",label3);
  143.     }
  144.     public void start()
  145.     {
  146.         timer=null;
  147.         r=new Random(new Date().getTime());
  148.         onNew();
  149.     }
  150.     public boolean action(Event evt,Object o)
  151.     {
  152.         if (evt.target==bNew) {onNew(); return true;}
  153.         if (evt.target==bStart) {onStart(); return true;}
  154.         if (IsStart) {
  155.             if (evt.target==bTurn) {onturn(); return true;}
  156.             if (evt.target==bLeft) {onleft(); return true;}
  157.             if (evt.target==bRight) {onright(); return true;}
  158.             if (evt.target==bDown) {ondown2(); return true;}
  159.         }
  160.         return super.action(evt,o);
  161.     }
  162.     public boolean keyDown(Event evt,int key)
  163.     {
  164.         if (!IsStart) return true;
  165.         switch (key) {
  166.             case Event.UP   : onturn(); break;
  167.             case Event.DOWN : ondown(); break;
  168.             case Event.LEFT : onleft(); break;
  169.             case Event.RIGHT: onright();break;
  170.         }
  171.         return true;
  172.     }
  173.     public void CleanBox(int[][] b)
  174.     {
  175.         int i,j;
  176.         for (i=0;i<LINE;i++)
  177.           for (j=0;j<ROW;j++) b[i][j]=0;
  178.     }
  179.     public void onNew()
  180.     {
  181.         if (timer!=null) {timer.stop(); timer=null;}
  182.         CleanBox(box);
  183.         board.repaint();
  184.         total=0;
  185.         score=0;
  186.         IsStart=false;
  187.         bStart.setLabel("Start");
  188.         bStart.enable();
  189.         tLevel.setText("0");
  190.         tScore.setText("0");
  191.         nn=Math.abs(r.nextInt())%NUM; //////
  192.         ss=size[nn];
  193.         blcopy2(bl1,shape,nn);
  194.         blcopy2(bl2,shape,nn);
  195.         setBlockColor(bl1);
  196.         x=0;y=(ROW-ss)/2;
  197.         n1=Math.abs(r.nextInt())%NUM; ///////
  198.         s1=size[n1];
  199.         blcopy2(pview,shape,n1);
  200.         setBlockColor(pview);
  201.         preview.repaint();
  202.     }
  203.     public void onStart()
  204.     {
  205.         if (IsStart) {
  206.             if (timer!=null) {timer.stop();timer=null;}
  207.             IsStart=false;
  208.             bStart.setLabel("Start");
  209.         }
  210.         else {
  211.             timer=new myTimer(this,500);
  212.             timer.start();
  213.             IsStart=true;
  214.             bStart.setLabel("Pause");
  215.         }
  216.     }
  217.     void turn(int[][] a,int[][] b,int s)
  218.     {
  219.         int i,j;
  220.         for (i=0;i<s;i++)
  221.           for (j=0;j<s;j++) b[i][j]=a[s-j-1][i];
  222.     }
  223.     void blcopy(int[][] a,int[][] b)
  224.     {
  225.         int i,j;
  226.         for (i=0;i<4;i++)
  227.           for (j=0;j<4;j++) a[i][j]=b[i][j];
  228.     }
  229.     void blcopy2(int[][] a,int[] b,int n)
  230.     {
  231.         int i,j,k=0;
  232.         for (i=0;i<4;i++)
  233.           for (j=0;j<4;j++) {
  234.             a[i][j]=b[n*16+k];
  235.             k++;
  236.           }
  237.     }
  238.     boolean canput(int[][] a,int x,int y,int[][] box)
  239.     {
  240.         int i,j,x1,y1;
  241.         for (i=0;i<4;i++)
  242.           for (j=0;j<4;j++) {
  243.             if (a[i][j]==0) continue;
  244.             x1=x+i; y1=y+j;
  245.             if (y1<0||y1>=ROW||x1>=LINE) return false;
  246.             if (x1>=0&&box[x1][y1]!=0) return false;
  247.           }
  248.         return true;
  249.     }
  250.     void blput(int[][] a,int x,int y,int[][] box)
  251.     {
  252.         int i,j;
  253.         for (i=0;i<4;i++)
  254.           for (j=0;j<4;j++)
  255.             if (a[i][j]!=0&&x+i>=0) box[x+i][y+j]=a[i][j];
  256.     }
  257.     void bltake(int[][] a,int x,int y,int[][] box)
  258.     {
  259.         int i,j;
  260.         for (i=0;i<4;i++)
  261.           for (j=0;j<4;j++)
  262.             if (a[i][j]!=0&&x+i>=0) box[x+i][y+j]=0;
  263.     }
  264.     void filled(int[][] b)
  265.     {
  266.         int i,j,k,l;
  267.         l=0;
  268.         for (i=0;i<LINE;i++) {
  269.           for (j=0;j<ROW&&b[i][j]!=0;j++);
  270.             if (j>=ROW) {b[i][0]=100;l++;}
  271.         }
  272.         total+=l;
  273.         score+=l*100;
  274.         tLevel.setText(""+total);
  275.         tScore.setText(""+score);
  276.         if (l==0) return;
  277.         k=LINE-1; i=LINE-1;
  278.         while(b[k][0]==100) k--;
  279.         while (k>=0)
  280.         {
  281.             for (j=0;j<ROW;j++) b[i][j]=b[k][j];
  282.             i--; k--;
  283.             while(k>=0&&b[k][0]==100) k--;
  284.         }
  285.         while (i>=0)
  286.         {
  287.          for (j=0;j<ROW;j++) b[i][j]=0;
  288.          i--;
  289.         }
  290.         board.repaint();
  291.     }
  292.     synchronized void onturn()
  293.     {
  294.         bltake(bl1,x,y,box);
  295.         turn(bl1,bl2,ss);
  296.         if (canput(bl2,x,y,box)) {
  297.             blcopy(bl1,bl2);
  298.             blput(bl1,x,y,box);
  299.             board.repaint();
  300.         }
  301.         else blput(bl1,x,y,box);
  302.     }
  303.     synchronized void onleft()
  304.     {
  305.         bltake(bl1,x,y,box);
  306.         if (canput(bl1,x,y-1,box)) {
  307.             y--;
  308.             blput(bl1,x,y,box);
  309.             board.repaint();
  310.         }
  311.         else blput(bl1,x,y,box);
  312.     }
  313.     synchronized void onright()
  314.     {
  315.         bltake(bl1,x,y,box);
  316.         if (canput(bl1,x,y+1,box)) {
  317.             y++;
  318.             blput(bl1,x,y,box);
  319.             board.repaint();
  320.         }
  321.         else blput(bl1,x,y,box);
  322.     }
  323.     synchronized void ondown()
  324.     {
  325.         bltake(bl1,x,y,box);
  326.         if (canput(bl1,x+1,y,box)) {
  327.             x++;
  328.             blput(bl1,x,y,box);
  329.             board.repaint();
  330.         }
  331.         else {
  332.             blput(bl1,x,y,box);
  333.             filled(box);
  334.             if (x<0) {
  335.                 IsStart=false;
  336.                 bStart.disable();
  337.                 bStart.setLabel("Start");
  338.                 if (timer!=null) {timer.stop();timer=null;}
  339.             }
  340.             else {
  341.                 nn=n1;
  342.                 ss=s1;
  343.                 blcopy(bl1,pview);
  344.                 blcopy(bl2,pview);
  345.                 x=-ss;y=(ROW-ss)/2;
  346.                 n1=Math.abs(r.nextInt())%NUM; /////
  347.                 s1=size[n1];
  348.                 blcopy2(pview,shape,n1);
  349.                 setBlockColor(pview);
  350.                 preview.repaint();
  351.             }
  352.         }
  353.     }
  354.     synchronized void ondown2()
  355.     {
  356.         bltake(bl1,x,y,box);
  357.         if (canput(bl1,x+1,y,box)) {
  358.             x++;
  359.             while (canput(bl1,x+1,y,box)) x++;
  360.             blput(bl1,x,y,box);
  361.             board.repaint();
  362.         }
  363.         else {
  364.             blput(bl1,x,y,box);
  365.             filled(box);
  366.             if (x<0) {
  367.                 IsStart=false;
  368.                 bStart.disable();
  369.                 bStart.setLabel("Start");
  370.                 if (timer!=null) {timer.stop();timer=null;}
  371.             }
  372.             else {
  373.                 nn=n1;
  374.                 ss=s1;
  375.                 blcopy(bl1,pview);
  376.                 blcopy(bl2,pview);
  377.                 x=-ss;y=(ROW-ss)/2;
  378.                 n1=Math.abs(r.nextInt())%NUM; /////
  379.                 s1=size[n1];
  380.                 blcopy2(pview,shape,n1);
  381.                 setBlockColor(pview);
  382.                 preview.repaint();
  383.             }
  384.         }
  385.     }
  386.     void setBlockColor(int[][] b)
  387.     {
  388.         int c,i,j;
  389.         c=Math.abs(r.nextInt())%COLORS+1;
  390.         for (i=0;i<4;i++)
  391.           for (j=0;j<4;j++)
  392.             if (b[i][j]!=0) b[i][j]=c;
  393.         /*showStatus(c+"");*/
  394.     }
  395. //The End!
  396. }