俄罗斯方块——Java版


  1. package Karl.Doenitz;  
  2.   
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5.   
  6. import javax.swing.*;  
  7.   
  8. @SuppressWarnings("serial")  
  9. public class Tetris extends JFrame implements KeyListener {  
  10.     int pos[] = {0,1};  
  11.     boolean bottom = false;  
  12. int n = 20;  
  13. int m = 10;  
  14. JButton b[][];  
  15. Color tmp[][] = new Color[m][n];  
  16. int rand = 0;  
  17. int centralx = 0;  
  18. int centraly = 0;  
  19. int deltax = 0;  
  20. int perim[][] = new int[m+4][n+4];  
  21. or[][][] prof = new or[4][4][7];  
  22. Color rnd[] = {Color.red, Color.yellow, Color.cyan, Color.green, Color.white, Color.blue, Color.orange};  
  23. int rowsclrd = 0;  
  24. public Tetris(){  
  25.     this.setTitle("俄罗斯方块KarlDoenitz制作");  
  26.     for(int a = 0;a<4;a++){  
  27.         for(int b = 0;b<4;b++){  
  28.             for(int c = 0;c<7;c++){  
  29.                 prof[a][b][c] = new or();  
  30.             }  
  31.         }  
  32.     }  
  33.     this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  34.     prof[0][0][0].x = -1;  
  35.     prof[0][1][0].x = 0;  
  36.     prof[0][2][0].x = 0;  
  37.     prof[0][3][0].x = 1;  
  38.     prof[1][0][0].x = 0;  
  39.     prof[1][1][0].x = 0;  
  40.     prof[1][2][0].x = -1;  
  41.     prof[1][3][0].x = -1;  
  42.     prof[2][0][0].x = 1;  
  43.     prof[2][1][0].x = 0;  
  44.     prof[2][2][0].x = 0;  
  45.     prof[2][3][0].x = -1;  
  46.     prof[3][0][0].x = 0;  
  47.     prof[3][1][0].x = 0;  
  48.     prof[3][2][0].x = 1;  
  49.     prof[3][3][0].x = 1;  
  50.     prof[0][0][1].x = -1;  
  51.     prof[0][1][1].x = -1;  
  52.     prof[0][2][1].x = 0;  
  53.     prof[0][3][1].x = 1;  
  54.     prof[1][0][1].x = -1;  
  55.     prof[1][1][1].x = 0;  
  56.     prof[1][2][1].x = 0;  
  57.     prof[1][3][1].x = 0;  
  58.     prof[2][0][1].x = 1;  
  59.     prof[2][1][1].x = 1;  
  60.     prof[2][2][1].x = 0;  
  61.     prof[2][3][1].x = -1;  
  62.     prof[3][0][1].x = 1;  
  63.     prof[3][1][1].x = 0;  
  64.     prof[3][2][1].x = 0;  
  65.     prof[3][3][1].x = 0;  
  66.     prof[0][0][2].x = -1;  
  67.     prof[0][1][2].x = 0;  
  68.     prof[0][2][2].x = 1;  
  69.     prof[0][3][2].x = 1;  
  70.     prof[1][0][2].x = 0;  
  71.     prof[1][1][2].x = 0;  
  72.     prof[1][2][2].x = 0;  
  73.     prof[1][3][2].x = -1;  
  74.     prof[2][0][2].x = 1;  
  75.     prof[2][1][2].x = 0;  
  76.     prof[2][2][2].x = -1;  
  77.     prof[2][3][2].x = -1;  
  78.     prof[3][0][2].x = 0;  
  79.     prof[3][1][2].x = 0;  
  80.     prof[3][2][2].x = 0;  
  81.     prof[3][3][2].x = 1;  
  82.     prof[0][0][3].x = -1;  
  83.     prof[0][1][3].x = 0;  
  84.     prof[0][2][3].x = 0;  
  85.     prof[0][3][3].x = 1;  
  86.     prof[1][0][3].x = -1;  
  87.     prof[1][1][3].x = -1;  
  88.     prof[1][2][3].x = 0;  
  89.     prof[1][3][3].x = 0;  
  90.     prof[2][0][3].x = 1;  
  91.     prof[2][1][3].x = 0;  
  92.     prof[2][2][3].x = 0;  
  93.     prof[2][3][3].x = -1;  
  94.     prof[3][0][3].x = 1;  
  95.     prof[3][1][3].x = 1;  
  96.     prof[3][2][3].x = 0;  
  97.     prof[3][3][3].x = 0;  
  98.     prof[0][0][4].x = -1;  
  99.     prof[0][1][4].x = 0;  
  100.     prof[0][2][4].x = 0;  
  101.     prof[0][3][4].x = 1;  
  102.     prof[1][0][4].x = 0;  
  103.     prof[1][1][4].x = 0;  
  104.     prof[1][2][4].x = -1;  
  105.     prof[1][3][4].x = 0;  
  106.     prof[2][0][4].x = 1;  
  107.     prof[2][1][4].x = 0;  
  108.     prof[2][2][4].x = 0;  
  109.     prof[2][3][4].x = -1;  
  110.     prof[3][0][4].x = 0;  
  111.     prof[3][1][4].x = 0;  
  112.     prof[3][2][4].x = 1;  
  113.     prof[3][3][4].x = 0;  
  114.     prof[0][0][5].x = 0;  
  115.     prof[0][1][5].x = 0;  
  116.     prof[0][2][5].x = 1;  
  117.     prof[0][3][5].x = 1;  
  118.     prof[1][0][5].x = 0;  
  119.     prof[1][1][5].x = 0;  
  120.     prof[1][2][5].x = 1;  
  121.     prof[1][3][5].x = 1;  
  122.     prof[2][0][5].x = 0;  
  123.     prof[2][1][5].x = 0;  
  124.     prof[2][2][5].x = 1;  
  125.     prof[2][3][5].x = 1;  
  126.     prof[3][0][5].x = 0;  
  127.     prof[3][1][5].x = 0;  
  128.     prof[3][2][5].x = 1;  
  129.     prof[3][3][5].x = 1;  
  130.     prof[0][0][6].x = -1;  
  131.     prof[0][1][6].x = 0;  
  132.     prof[0][2][6].x = 1;  
  133.     prof[0][3][6].x = 2;  
  134.     prof[1][0][6].x = 0;  
  135.     prof[1][1][6].x = 0;  
  136.     prof[1][2][6].x = 0;  
  137.     prof[1][3][6].x = 0;  
  138.     prof[2][0][6].x = 1;  
  139.     prof[2][1][6].x = 0;  
  140.     prof[2][2][6].x = -1;  
  141.     prof[2][3][6].x = -2;  
  142.     prof[3][0][6].x = 0;  
  143.     prof[3][1][6].x = 0;  
  144.     prof[3][2][6].x = 0;  
  145.     prof[3][3][6].x = 0;  
  146.     prof[0][0][0].y = 0;  
  147.     prof[0][1][0].y = 0;  
  148.     prof[0][2][0].y = 1;  
  149.     prof[0][3][0].y = 1;  
  150.     prof[1][0][0].y = -1;  
  151.     prof[1][1][0].y = 0;  
  152.     prof[1][2][0].y = 0;  
  153.     prof[1][3][0].y = 1;  
  154.     prof[2][0][0].y = 0;  
  155.     prof[2][1][0].y = 0;  
  156.     prof[2][2][0].y = -1;  
  157.     prof[2][3][0].y = -1;  
  158.     prof[3][0][0].y = 1;  
  159.     prof[3][1][0].y = 0;  
  160.     prof[3][2][0].y = 0;  
  161.     prof[3][3][0].y = -1;  
  162.     prof[0][0][1].y = 0;  
  163.     prof[0][1][1].y = 1;  
  164.     prof[0][2][1].y = 0;  
  165.     prof[0][3][1].y = 0;  
  166.     prof[1][0][1].y = -1;  
  167.     prof[1][1][1].y = -1;  
  168.     prof[1][2][1].y = 0;  
  169.     prof[1][3][1].y = 1;  
  170.     prof[2][0][1].y = -1;  
  171.     prof[2][1][1].y = 0;  
  172.     prof[2][2][1].y = 0;  
  173.     prof[2][3][1].y = 0;  
  174.     prof[3][0][1].y = 1;  
  175.     prof[3][1][1].y = 1;  
  176.     prof[3][2][1].y = 0;  
  177.     prof[3][3][1].y = -1;  
  178.     prof[0][0][2].y = 0;  
  179.     prof[0][1][2].y = 0;  
  180.     prof[0][2][2].y = 0;  
  181.     prof[0][3][2].y = 1;  
  182.     prof[1][0][2].y = -1;  
  183.     prof[1][1][2].y = 0;  
  184.     prof[1][2][2].y = 1;  
  185.     prof[1][3][2].y = 1;  
  186.     prof[2][0][2].y = 0;  
  187.     prof[2][1][2].y = 0;  
  188.     prof[2][2][2].y = 0;  
  189.     prof[2][3][2].y = -1;  
  190.     prof[3][0][2].y = 1;  
  191.     prof[3][1][2].y = 0;  
  192.     prof[3][2][2].y = -1;  
  193.     prof[3][3][2].y = -1;  
  194.     prof[0][0][3].y = 1;  
  195.     prof[0][1][3].y = 1;  
  196.     prof[0][2][3].y = 0;  
  197.     prof[0][3][3].y = 0;  
  198.     prof[1][0][3].y = -1;  
  199.     prof[1][1][3].y = 0;  
  200.     prof[1][2][3].y = 0;  
  201.     prof[1][3][3].y = 1;  
  202.     prof[2][0][3].y = -1;  
  203.     prof[2][1][3].y = -1;  
  204.     prof[2][2][3].y = 0;  
  205.     prof[2][3][3].y = 0;  
  206.     prof[3][0][3].y = 1;  
  207.     prof[3][1][3].y = 0;  
  208.     prof[3][2][3].y = 0;  
  209.     prof[3][3][3].y = -1;  
  210.     prof[0][0][4].y = 0;  
  211.     prof[0][1][4].y = 0;  
  212.     prof[0][2][4].y = 1;  
  213.     prof[0][3][4].y = 0;  
  214.     prof[1][0][4].y = -1;  
  215.     prof[1][1][4].y = 0;  
  216.     prof[1][2][4].y = 0;  
  217.     prof[1][3][4].y = 1;  
  218.     prof[2][0][4].y = 0;  
  219.     prof[2][1][4].y = 0;  
  220.     prof[2][2][4].y = -1;  
  221.     prof[2][3][4].y = 0;  
  222.     prof[3][0][4].y = 1;  
  223.     prof[3][1][4].y = 0;  
  224.     prof[3][2][4].y = 0;  
  225.     prof[3][3][4].y = -1;  
  226.     prof[0][0][5].y = 0;  
  227.     prof[0][1][5].y = 1;  
  228.     prof[0][2][5].y = 0;  
  229.     prof[0][3][5].y = 1;  
  230.     prof[1][0][5].y = 0;  
  231.     prof[1][1][5].y = 1;  
  232.     prof[1][2][5].y = 0;  
  233.     prof[1][3][5].y = 1;  
  234.     prof[2][0][5].y = 0;  
  235.     prof[2][1][5].y = 1;  
  236.     prof[2][2][5].y = 0;  
  237.     prof[2][3][5].y = 1;  
  238.     prof[3][0][5].y = 0;  
  239.     prof[3][1][5].y = 1;  
  240.     prof[3][2][5].y = 0;  
  241.     prof[3][3][5].y = 1;  
  242.     prof[0][0][6].y = 0;  
  243.     prof[0][1][6].y = 0;  
  244.     prof[0][2][6].y = 0;  
  245.     prof[0][3][6].y = 0;  
  246.     prof[1][0][6].y = -1;  
  247.     prof[1][1][6].y = 0;  
  248.     prof[1][2][6].y = 1;  
  249.     prof[1][3][6].y = 2;  
  250.     prof[2][0][6].y = 0;  
  251.     prof[2][1][6].y = 0;  
  252.     prof[2][2][6].y = 0;  
  253.     prof[2][3][6].y = 0;  
  254.     prof[3][0][6].y = -1;  
  255.     prof[3][1][6].y = 0;  
  256.     prof[3][2][6].y = 1;  
  257.     prof[3][3][6].y = 2;  
  258.     for (int y = 0;y<2;y++){  
  259.     for (int x = 0;x<m+4;x++){  
  260.     perim[x][y]= 1;  
  261.     }}  
  262.     for (int y = n+2;y<n+4;y++){  
  263.         for (int x = 0;x<m+4;x++){  
  264.         perim[x][y]= 4;  
  265.         }}  
  266.     for (int y = 2;y<n+2;y++){  
  267.         for (int x = 0;x<2;x++){  
  268.         perim[x][y]= 2;  
  269.         }}  
  270.     for (int y = 2;y<n+2;y++){  
  271.         for (int x = m+2;x<m+4;x++){  
  272.         perim[x][y]= 2;  
  273.         }}  
  274.     for(int y = 0;y<n+4;y++){  
  275.         for (int x = 0;x<m+4;x++){  
  276.             System.out.print(perim[x][y]);  
  277.         }  
  278.         System.out.println("");  
  279.     }  
  280.             b = new JButton [m][n];  
  281.             setLayout(new GridLayout(n,m));//Grid layouts x and y are SWAPPED!  
  282.             for (int y = 0;y<n;y++){  
  283.                 for (int x = 0;x<m;x++){  
  284.                         b[x][y] = new JButton(" ");  
  285.                         tmp[x][y] = Color.DARK_GRAY;  
  286.                         b[x][y].setBackground(Color.DARK_GRAY);  
  287.                         add(b[x][y]);  
  288.                         b[x][y].setEnabled(true);  
  289.                 }//end inner for  
  290.         }  
  291.             setFocusable(true);  
  292.             addKeyListener(this);  
  293.             pack();  
  294.             setVisible(true);  
  295.             blockgen();  
  296.              
  297.               
  298.     }//end constructor Mine()  
  299.   
  300.     class or {  
  301.         int x;  
  302.         int y;  
  303.     }  
  304.   
  305.     public void blockgen(){  
  306.         Component temporaryLostComponent = null;  
  307.         pos[0] = 0;  
  308.         pos[1] = 1;  
  309.         rand = (int) (Math.floor(Math.random()*7+1));  
  310.         centralx = 4;  
  311.         centraly = 0;  
  312.          System.out.print(rand);  
  313.         if ((b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].getBackground() == Color.DARK_GRAY) &&  
  314.         (b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].getBackground() == Color.DARK_GRAY) &&  
  315.         (b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].getBackground() == Color.DARK_GRAY) &&  
  316.         (b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].getBackground() == Color.DARK_GRAY)){  
  317.         b[4+prof[pos[0]][0][rand-1].x][prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);  
  318.         b[4+prof[pos[0]][1][rand-1].x][prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);  
  319.         b[4+prof[pos[0]][2][rand-1].x][prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);  
  320.         b[4+prof[pos[0]][3][rand-1].x][prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);  
  321.         go();  
  322.         } else {  
  323.             JOptionPane.showMessageDialog(temporaryLostComponent, "游戏结束! 你一共得了"+rowsclrd+" 分,干得好,继续努力!");  
  324.             System.exit(0);  
  325.         }  
  326.     }  
  327.       
  328.     public void rotate(){  
  329.         if (pos[0] < 3){  
  330.             pos[1] = pos[0];  
  331.         pos[0]++;  
  332.         } else if (pos[0] == 3){  
  333.             pos[0] = 0;  
  334.             pos[1] = 3;  
  335.         } else {  
  336.             System.out.println("error");  
  337.         }  
  338.         if ((perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 4) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 1) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 2) && (perim[2+centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y+2] != 3)  
  339.                  && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y+2] != 3)  
  340.                  && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 4) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 1) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 2) && (perim[centralx+2+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y+2] != 3)  
  341.                  && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 4) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 1) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 2) && (perim[centralx+prof[pos[0]][3][rand-1].x+2][centraly+prof[pos[0]][3][rand-1].y+2] != 3)){  
  342.              b[centralx+prof[pos[1]][0][rand-1].x][centraly+prof[pos[1]][0][rand-1].y].setBackground(Color.DARK_GRAY);  
  343.                 b[centralx+prof[pos[1]][1][rand-1].x][centraly+prof[pos[1]][1][rand-1].y].setBackground(Color.DARK_GRAY);  
  344.                 b[centralx+prof[pos[1]][2][rand-1].x][centraly+prof[pos[1]][2][rand-1].y].setBackground(Color.DARK_GRAY);  
  345.                 b[centralx+prof[pos[1]][3][rand-1].x][centraly+prof[pos[1]][3][rand-1].y].setBackground(Color.DARK_GRAY);  
  346.                 b[centralx+(prof[pos[0]][0][rand-1].x)][centraly+(prof[pos[0]][0][rand-1].y)].setBackground(rnd[rand-1]);  
  347.                 b[centralx+(prof[pos[0]][1][rand-1].x)][centraly+(prof[pos[0]][1][rand-1].y)].setBackground(rnd[rand-1]);  
  348.                 b[centralx+(prof[pos[0]][2][rand-1].x)][centraly+(prof[pos[0]][2][rand-1].y)].setBackground(rnd[rand-1]);  
  349.                 b[centralx+(prof[pos[0]][3][rand-1].x)][centraly+(prof[pos[0]][3][rand-1].y)].setBackground(rnd[rand-1]);  
  350.         } else {  
  351.             if (pos[1] > 0){  
  352.                 pos[0] = pos[1];  
  353.                 pos[1]--;  
  354.             } else if (pos[1] == 0){  
  355.                 pos[0] = 0;  
  356.                 pos[1] = 3;             }  
  357.             }  
  358.     }  
  359.       
  360.       
  361.     public int getxs(){  
  362.            int xs = 0;  
  363.            int[] xf = {-1, -1, -1, -1};  
  364.            for (int d = 0;d<4;d++){  
  365.                if ((xf[0] != prof[pos[0]][d][rand-1].x) || (xf[1] != prof[pos[0]][d][rand-1].x) || (xf[2] != prof[pos[0]][d][rand-1].x) || (xf[3] != prof[pos[0]][d][rand-1].x)){  
  366.                    xf[d] = prof[pos[0]][d][rand-1].x;  
  367.                }  
  368.            }  
  369.            for (int d = 0;d<4;d++){  
  370.                if (xf[d] != -1){  
  371.                    xs++;  
  372.                }  
  373.            }  
  374.            return xs;  
  375.     }  
  376.       
  377.       
  378.    public void movedown(){  
  379.        int[] m2 = {-1, -1, -1, -1};  
  380.        int[] m1 = {-1, -1, -1, -1};  
  381.        int[] zero = {-1, -1, -1, -1};  
  382.        int[] one = {-1, -1, -1, -1};  
  383.        int[] two = {-1, -1, -1, -1};  
  384.         for (int d = 0;d<4;d++){  
  385.             if (prof[pos[0]][d][rand-1].x == -2){  
  386.                 m2[d] = d;  
  387.             } else if (prof[pos[0]][d][rand-1].x == -1){  
  388.                 m1[d] = d;  
  389.             } else if (prof[pos[0]][d][rand-1].x == 0){  
  390.                 zero[d] = d;  
  391.             } else if (prof[pos[0]][d][rand-1].x == 1){  
  392.                 one[d] = d;  
  393.             } else if (prof[pos[0]][d][rand-1].x == 2){  
  394.                 two[d] = d;  
  395.             }  
  396.         }  
  397.         int tmpm2 = -5;  
  398.         int tmpm1 = -5;  
  399.         int tmpzero = -5;  
  400.         int tmpone = -5;  
  401.         int tmptwo = -5;  
  402.         for (int d = 0;d<4;d++){  
  403.             if (m2[d] != -1){  
  404.                 if (tmpm2<prof[pos[0]][m2[d]][rand-1].y){  
  405.                 tmpm2 = prof[pos[0]][m2[d]][rand-1].y;  
  406.                 }  
  407.             }  
  408.             if (m1[d] != -1){  
  409.                 if (tmpm1<prof[pos[0]][m1[d]][rand-1].y){  
  410.                 tmpm1 = prof[pos[0]][m1[d]][rand-1].y;  
  411.             }  
  412.             }  
  413.             if (zero[d] != -1){  
  414.                 if (tmpzero<prof[pos[0]][zero[d]][rand-1].y){  
  415.                 tmpzero = prof[pos[0]][zero[d]][rand-1].y;  
  416.             }  
  417.             }  
  418.             if (one[d] != -1){  
  419.                 if (tmpone<prof[pos[0]][one[d]][rand-1].y){  
  420.                 tmpone = prof[pos[0]][one[d]][rand-1].y;  
  421.             }  
  422.             }  
  423.             if (two[d] != -1){  
  424.                 if (tmptwo<prof[pos[0]][two[d]][rand-1].y){  
  425.                 tmptwo = prof[pos[0]][two[d]][rand-1].y;  
  426.             }  
  427.             }  
  428.         }  
  429.         int total = 0;  
  430.         for (int d = 0;d<4;d++){  
  431.             if (prof[pos[0]][d][rand-1].x == -2){  
  432.                 if (perim[2+centralx+-2][2+centraly+tmpm2+1] != 4){   
  433.                     if(b[centralx+-2][centraly+tmpm2+1].getBackground() == Color.DARK_GRAY){  
  434.                     total++;  
  435.                 }}  
  436.             } else if (prof[pos[0]][d][rand-1].x == -1){  
  437.                 if (perim[2+centralx+-1][2+centraly+tmpm1+1] != 4){  
  438.                         if (b[centralx+-1][centraly+tmpm1+1].getBackground() == Color.DARK_GRAY){  
  439.                     total++;  
  440.                 }}  
  441.             } else if (prof[pos[0]][d][rand-1].x == 0){  
  442.                 if (perim[2+centralx][2+centraly+tmpzero+1] != 4){  
  443.                         if (b[centralx][centraly+tmpzero+1].getBackground() == Color.DARK_GRAY){  
  444.                     total++;  
  445.                 }}  
  446.             } else if (prof[pos[0]][d][rand-1].x == 1){  
  447.                 if (perim[2+centralx+1][2+centraly+tmpone+1] != 4){  
  448.                         if (b[centralx+1][centraly+tmpone+1].getBackground() == Color.DARK_GRAY){  
  449.                     total++;  
  450.                 }}  
  451.             } else if (prof[pos[0]][d][rand-1].x == 2){  
  452.                 if (perim[2+centralx+2][2+centraly+tmptwo+1] != 4){  
  453.                     if (b[centralx+2][centraly+tmptwo+1].getBackground() == Color.DARK_GRAY){  
  454.                     total++;  
  455.                 }}  
  456.             }  
  457.         }  
  458.              if (total == 4){  
  459.                   b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);  
  460.                 b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);  
  461.                 b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);  
  462.                 b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);  
  463.                 centraly++;  
  464.                 b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);  
  465.                 b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);  
  466.                 b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);  
  467.                 b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);  
  468.               } else {  
  469.               
  470. bottom = true;  
  471.                  }  
  472.              }  
  473.          
  474.   
  475.      
  476.    public void go(){  
  477.        do{  
  478.        try {  
  479.         Thread.sleep(1000L);  
  480.     } catch (InterruptedException e) {  
  481.         // TODO Auto-generated catch block  
  482.         e.printStackTrace();  
  483.     }  
  484.        movedown();  
  485.        rowcheck();  
  486.    }  
  487.    while(bottom == false);  
  488.        bottom = false;  
  489.        blockgen();  
  490.        }  
  491.      
  492.    public void rowcheck(){  
  493.        int row = 0;  
  494.        for (int y = 0;y<20;y++){  
  495.            for (int x = 0;x<10;x++){  
  496.                if (b[x][y].getBackground() != Color.DARK_GRAY){  
  497.                    row++;  
  498.                }  
  499.                if (row == 10){  
  500.                    rowsclrd++;  
  501.                    rowclear(y);                  
  502.                }  
  503.            }  
  504.            row = 0;  
  505.        }  
  506.    }  
  507.      
  508.    public void rowclear(int y){  
  509.        int inc = 0;  
  510.        for (int x = 0;x<10;x++){  
  511.            b[x][y].setBackground(Color.DARK_GRAY);  
  512.        }  
  513.        for (int c = y-1;c>-1;c--){  
  514.        for (int x = 0;x<10;x++){  
  515.        tmp[x][y-inc] = b[x][c].getBackground();  
  516.        }inc++;}  
  517.        for (int c = y;c>-1;c--){  
  518.            for (int x = 0;x<10;x++){  
  519.                b[x][c].setBackground(tmp[x][c]);  
  520.            }  
  521.        }  
  522.    }  
  523.      
  524.    public void movelr(){                
  525.        int[] m2 = {-1, -1, -1, -1};  
  526.        int[] m1 = {-1, -1, -1, -1};  
  527.        int[] zero = {-1, -1, -1, -1};  
  528.        int[] one = {-1, -1, -1, -1};  
  529.        int[] two = {-1, -1, -1, -1};  
  530.         for (int d = 0;d<4;d++){  
  531.             if (prof[pos[0]][d][rand-1].y == -2){  
  532.                 m2[d] = d;  
  533.             } else if (prof[pos[0]][d][rand-1].y == -1){  
  534.                 m1[d] = d;  
  535.             } else if (prof[pos[0]][d][rand-1].y == 0){  
  536.                 zero[d] = d;  
  537.             } else if (prof[pos[0]][d][rand-1].y == 1){  
  538.                 one[d] = d;  
  539.             } else if (prof[pos[0]][d][rand-1].y == 2){  
  540.                 two[d] = d;  
  541.             }  
  542.         }  
  543.             int tmpm2 = -5;  
  544.             int tmpm1 = -5;  
  545.             int tmpzero = -5;  
  546.             int tmpone = -5;  
  547.             int tmptwo = -5;  
  548.         if (deltax == 1){  
  549.         for (int d = 0;d<4;d++){  
  550.             if (m2[d] != -1){  
  551.                 if (tmpm2<prof[pos[0]][m2[d]][rand-1].x){  
  552.                 tmpm2 = prof[pos[0]][m2[d]][rand-1].x;  
  553.                 }  
  554.             }  
  555.             if (m1[d] != -1){  
  556.                 if (tmpm1<prof[pos[0]][m1[d]][rand-1].x){  
  557.                 tmpm1 = prof[pos[0]][m1[d]][rand-1].x;  
  558.             }  
  559.             }  
  560.             if (zero[d] != -1){  
  561.                 if (tmpzero<prof[pos[0]][zero[d]][rand-1].x){  
  562.                 tmpzero = prof[pos[0]][zero[d]][rand-1].x;  
  563.             }  
  564.             }  
  565.             if (one[d] != -1){  
  566.                 if (tmpone<prof[pos[0]][one[d]][rand-1].x){  
  567.                 tmpone = prof[pos[0]][one[d]][rand-1].x;  
  568.             }  
  569.             }  
  570.             if (two[d] != -1){  
  571.                 if (tmptwo<prof[pos[0]][two[d]][rand-1].x){  
  572.                 tmptwo = prof[pos[0]][two[d]][rand-1].x;  
  573.             }  
  574.             }  
  575.         }  
  576.         } else if (deltax == -1){  
  577.             tmpm2 = 5;  
  578.             tmpm1 = 5;  
  579.             tmpzero = 5;  
  580.             tmpone = 5;  
  581.             tmptwo = 5;  
  582.             for (int d = 0;d<4;d++){  
  583.                 if (m2[d] != -1){  
  584.                     if (tmpm2>prof[pos[0]][m2[d]][rand-1].x){  
  585.                     tmpm2 = prof[pos[0]][m2[d]][rand-1].x;  
  586.                     }  
  587.                 }  
  588.                 if (m1[d] != -1){  
  589.                     if (tmpm1>prof[pos[0]][m1[d]][rand-1].x){  
  590.                     tmpm1 = prof[pos[0]][m1[d]][rand-1].x;  
  591.                 }  
  592.                 }  
  593.                 if (zero[d] != -1){  
  594.                     if (tmpzero>prof[pos[0]][zero[d]][rand-1].x){  
  595.                     tmpzero = prof[pos[0]][zero[d]][rand-1].x;  
  596.                 }  
  597.                 }  
  598.                 if (one[d] != -1){  
  599.                     if (tmpone>prof[pos[0]][one[d]][rand-1].x){  
  600.                     tmpone = prof[pos[0]][one[d]][rand-1].x;  
  601.                 }  
  602.                 }  
  603.                 if (two[d] != -1){  
  604.                     if (tmptwo>prof[pos[0]][two[d]][rand-1].x){  
  605.                     tmptwo = prof[pos[0]][two[d]][rand-1].x;  
  606.                 }  
  607.                 }  
  608.             }  
  609.         }  
  610.         int total = 0;  
  611.         for (int d = 0;d<4;d++){  
  612.             if (prof[pos[0]][d][rand-1].y == -2){  
  613.                 if (perim[2+centralx+deltax+tmpm2][2+centraly-2] != 2){   
  614.                     if(b[centralx+deltax+tmpm2][centraly-2].getBackground() == Color.DARK_GRAY){  
  615.                     total++;  
  616.                 }}  
  617.             } else if (prof[pos[0]][d][rand-1].y == -1){  
  618.                 if (perim[2+centralx+deltax+tmpm1][2+centraly-1] != 2){  
  619.                         if (b[centralx+deltax+tmpm1][centraly-1].getBackground() == Color.DARK_GRAY){  
  620.                     total++;  
  621.                 }}  
  622.             } else if (prof[pos[0]][d][rand-1].y == 0){  
  623.                 if (perim[2+centralx+deltax+tmpzero][2+centraly] != 2){  
  624.                         if (b[centralx+deltax+tmpzero][centraly].getBackground() == Color.DARK_GRAY){  
  625.                     total++;  
  626.                 }}  
  627.             } else if (prof[pos[0]][d][rand-1].y == 1){  
  628.                 if (perim[2+centralx+deltax+tmpone][2+centraly+1] != 2){  
  629.                         if (b[centralx+deltax+tmpone][centraly+1].getBackground() == Color.DARK_GRAY){  
  630.                     total++;  
  631.                 }}  
  632.             } else if (prof[pos[0]][d][rand-1].y == 2){  
  633.                 if (perim[2+centralx+deltax+tmptwo][2+centraly+2] != 2){  
  634.                     if (b[centralx+deltax+tmptwo][centraly+2].getBackground() == Color.DARK_GRAY){  
  635.                     total++;  
  636.                 }}  
  637.             }  
  638.         } if (total == 4){  
  639.                     b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(Color.DARK_GRAY);  
  640.                     b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(Color.DARK_GRAY);  
  641.                     b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(Color.DARK_GRAY);  
  642.                     b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(Color.DARK_GRAY);  
  643.                     centralx = centralx+deltax;  
  644.                     b[centralx+prof[pos[0]][0][rand-1].x][centraly+prof[pos[0]][0][rand-1].y].setBackground(rnd[rand-1]);  
  645.                     b[centralx+prof[pos[0]][1][rand-1].x][centraly+prof[pos[0]][1][rand-1].y].setBackground(rnd[rand-1]);  
  646.                     b[centralx+prof[pos[0]][2][rand-1].x][centraly+prof[pos[0]][2][rand-1].y].setBackground(rnd[rand-1]);  
  647.                     b[centralx+prof[pos[0]][3][rand-1].x][centraly+prof[pos[0]][3][rand-1].y].setBackground(rnd[rand-1]);  
  648.         }  
  649.                     
  650.                 
  651.    }  
  652.   
  653. public static void main (String[] args){  
  654.     new Tetris();  
  655. }  
  656.   
  657. @Override  
  658. public void keyPressed(KeyEvent e) {  
  659.     if (e.getKeyCode() == KeyEvent.VK_RIGHT){  
  660.         deltax = 1;  
  661.         movelr();  
  662.         }  
  663.     if (e.getKeyCode() == KeyEvent.VK_LEFT){  
  664.         deltax = -1;  
  665.         movelr();  
  666.         }  
  667.     if (e.getKeyCode() == KeyEvent.VK_UP){  
  668.         rotate();  
  669.         }  
  670.     if (e.getKeyCode() == KeyEvent.VK_DOWN){  
  671.         movedown();  
  672.         }  
  673. }  
  674.   
  675.   
  676. @Override  
  677. public void keyReleased(KeyEvent e) {  
  678.     // TODO Auto-generated method stub  
  679.       
  680. }  
  681.   
  682.   
  683. @Override  
  684. public void keyTyped(KeyEvent e) {  
  685.     // TODO Auto-generated method stub  
  686.       
  687. }  
  688. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值