⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tetris.java

📁 一个JAVA的俄罗斯方块游戏 呵呵
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;

class myTimer extends Thread {
    int time;
    Tetris tetris;

    public myTimer(Tetris t,int delay)
    {
        time=delay;
        tetris=t;
    }

    public void setTimer(int t)
    {
        time=t;
    }

    public void run() {
        while (true) {
            try {
                sleep(time);
            }
            catch (InterruptedException e) {}

            tetris.ondown();
        }
    }
}




public class Tetris extends Applet {

    Button bStart,bNew;
    Button bTurn,bLeft,bRight,bDown;
    Label label1,label2;
    TextField tLevel,tScore;
    Panel panel1,panel2,panel3,panel4;
    Board board;
    Preview preview;
    Label label3;

    myTimer timer;
    boolean IsStart;
    Random r;

    /*********************************/

    final int LINE=20;
    final int ROW =10;
    final int NUM =7;  // Num=10;
    final int COLORS =6;

    int[][] box=new int[LINE][ROW];
    int[][] pview=new int[4][4];
    int[][] bl1=new int[4][4];
    int[][] bl2=new int[4][4];

    int x,y;
    int total,score;
    int nn,ss,n1,s1;
    //boolean IsQuit;

    int[] size={4,3,2,3,3,3,3,3,3,3};

    int[] shape=
       {0,1,0,0,
        0,1,0,0,
        0,1,0,0,
        0,1,0,0, /*1*/
        0,0,0,0,
	    1,1,1,0,
		0,1,0,0,
		0,0,0,0, /*2*/
		1,1,0,0,
		1,1,0,0,
		0,0,0,0,
		0,0,0,0, /*3*/
		0,0,0,0,
		1,1,1,0,
		1,0,0,0,
		0,0,0,0, /*4*/
		0,0,0,0,
		1,1,1,0,
		0,0,1,0,
        0,0,0,0, /*5*/
		1,1,0,0,
		0,1,1,0,
		0,0,0,0,
		0,0,0,0, /*6*/
		0,1,1,0,
		1,1,0,0,
		0,0,0,0,
		0,0,0,0, /*7*/
		0,1,0,0,
		1,1,1,0,
		0,1,0,0,
		0,0,0,0, /*8*/
		1,1,1,0,
		1,0,1,0,
		0,0,0,0,
		0,0,0,0, /*9*/
		1,1,0,0,
		0,1,0,0,
		0,1,1,0,
		0,0,0,0, /*10*/
       };

    /*********************************/

    public void init()
    {


        bStart=new Button("Pause");
        bNew  =new Button("New Game");
        bTurn =new Button("T");
        bLeft =new Button("<");
        bRight=new Button(">");
        bDown =new Button("V");
        label1=new Label("Level:");
        label2=new Label("Score:");
        tLevel=new TextField();
        tLevel.disable();
        tScore=new TextField();
        tScore.disable();

        preview=new Preview(pview,4,4);
        //preview.setBackground(Color.cyan);
        board=new Board(box,ROW,LINE);
        //board.setBackground(Color.black);



        panel1=new Panel();
        panel1.setLayout(new GridLayout(2,3));
        panel1.add(bLeft);
        panel1.add(bTurn);
        panel1.add(bRight);
        panel1.add(new Panel());
        panel1.add(bDown);
        panel1.add(new Panel());

        panel2=new Panel();
        panel2.setLayout(new GridLayout(2,1,10,10));
        panel2.add(bNew);
        panel2.add(bStart);

        panel3=new Panel();
        panel3.setLayout(new GridLayout(4,1));
        panel3.add(label1);
        panel3.add(tLevel);
        panel3.add(label2);
        panel3.add(tScore);

        panel4=new Panel();
        panel4.setLayout(new GridLayout(4,1));
        panel4.add(panel2);
        panel4.add(preview);
        panel4.add(panel1);
        panel4.add(panel3);

        setLayout(new BorderLayout());
        setBackground(Color.lightGray);
        add("Center",board);
        add("East",panel4);
        label3=new Label("Tetris 1.0 made by Chen Shijie (9824053)");
        add("South",label3);

    }

    public void start()
    {
        timer=null;
        r=new Random(new Date().getTime());
        onNew();
    }

    public boolean action(Event evt,Object o)
    {
        if (evt.target==bNew) {onNew(); return true;}
        if (evt.target==bStart) {onStart(); return true;}
        if (IsStart) {
            if (evt.target==bTurn) {onturn(); return true;}
            if (evt.target==bLeft) {onleft(); return true;}
            if (evt.target==bRight) {onright(); return true;}
            if (evt.target==bDown) {ondown2(); return true;}
        }

        return super.action(evt,o);
    }

    public boolean keyDown(Event evt,int key)
    {
        if (!IsStart) return true;
        switch (key) {
            case Event.UP   : onturn(); break;
            case Event.DOWN : ondown(); break;
            case Event.LEFT : onleft(); break;
            case Event.RIGHT: onright();break;
        }
        return true;
    }

    public void CleanBox(int[][] b)
    {
        int i,j;
        for (i=0;i<LINE;i++)
          for (j=0;j<ROW;j++) b[i][j]=0;
    }

    public void onNew()
    {
        if (timer!=null) {timer.stop(); timer=null;}
        CleanBox(box);
        board.repaint();
        total=0;
        score=0;
        IsStart=false;
        bStart.setLabel("Start");
        bStart.enable();
        tLevel.setText("0");
        tScore.setText("0");



        nn=Math.abs(r.nextInt())%NUM; //////
        ss=size[nn];

        blcopy2(bl1,shape,nn);
        blcopy2(bl2,shape,nn);
        setBlockColor(bl1);

        x=0;y=(ROW-ss)/2;

        n1=Math.abs(r.nextInt())%NUM; ///////
        s1=size[n1];
        blcopy2(pview,shape,n1);
        setBlockColor(pview);

        preview.repaint();

    }

    public void onStart()
    {
        if (IsStart) {
            if (timer!=null) {timer.stop();timer=null;}
            IsStart=false;
            bStart.setLabel("Start");
        }
        else {
            timer=new myTimer(this,500);
            timer.start();
            IsStart=true;
            bStart.setLabel("Pause");
        }

    }



    void turn(int[][] a,int[][] b,int s)
    {
        int i,j;
        for (i=0;i<s;i++)
          for (j=0;j<s;j++) b[i][j]=a[s-j-1][i];
    }

    void blcopy(int[][] a,int[][] b)
    {
        int i,j;
        for (i=0;i<4;i++)
          for (j=0;j<4;j++) a[i][j]=b[i][j];
    }

    void blcopy2(int[][] a,int[] b,int n)
    {
        int i,j,k=0;
        for (i=0;i<4;i++)
          for (j=0;j<4;j++) {
            a[i][j]=b[n*16+k];
            k++;
          }
    }

    boolean canput(int[][] a,int x,int y,int[][] box)
    {
        int i,j,x1,y1;
        for (i=0;i<4;i++)
          for (j=0;j<4;j++) {
            if (a[i][j]==0) continue;
            x1=x+i; y1=y+j;
            if (y1<0||y1>=ROW||x1>=LINE) return false;
            if (x1>=0&&box[x1][y1]!=0) return false;
          }
        return true;
    }

    void blput(int[][] a,int x,int y,int[][] box)
    {
        int i,j;
        for (i=0;i<4;i++)
          for (j=0;j<4;j++)
            if (a[i][j]!=0&&x+i>=0) box[x+i][y+j]=a[i][j];
    }

    void bltake(int[][] a,int x,int y,int[][] box)
    {
        int i,j;
        for (i=0;i<4;i++)
          for (j=0;j<4;j++)
            if (a[i][j]!=0&&x+i>=0) box[x+i][y+j]=0;
    }





    void filled(int[][] b)
    {
        int i,j,k,l;
        l=0;
        for (i=0;i<LINE;i++) {
          for (j=0;j<ROW&&b[i][j]!=0;j++);
            if (j>=ROW) {b[i][0]=100;l++;}
        }

        total+=l;
        score+=l*100;
        tLevel.setText(""+total);
        tScore.setText(""+score);

        if (l==0) return;

        k=LINE-1; i=LINE-1;
        while(b[k][0]==100) k--;
        while (k>=0)
        {
            for (j=0;j<ROW;j++) b[i][j]=b[k][j];
            i--; k--;
            while(k>=0&&b[k][0]==100) k--;
        }
        while (i>=0)
        {
         for (j=0;j<ROW;j++) b[i][j]=0;
         i--;
        }
        board.repaint();
    }

    synchronized void onturn()
    {
        bltake(bl1,x,y,box);
        turn(bl1,bl2,ss);
        if (canput(bl2,x,y,box)) {
            blcopy(bl1,bl2);
            blput(bl1,x,y,box);
            board.repaint();
        }
        else blput(bl1,x,y,box);
    }

    synchronized void onleft()
    {
        bltake(bl1,x,y,box);
        if (canput(bl1,x,y-1,box)) {
            y--;
            blput(bl1,x,y,box);
            board.repaint();
        }
        else blput(bl1,x,y,box);
    }

    synchronized void onright()
    {
        bltake(bl1,x,y,box);
        if (canput(bl1,x,y+1,box)) {
            y++;
            blput(bl1,x,y,box);
            board.repaint();
        }
        else blput(bl1,x,y,box);
    }


    synchronized void ondown()
    {
        bltake(bl1,x,y,box);
        if (canput(bl1,x+1,y,box)) {
            x++;
            blput(bl1,x,y,box);
            board.repaint();
        }
        else {
            blput(bl1,x,y,box);
            filled(box);
            if (x<0) {
                IsStart=false;
                bStart.disable();
                bStart.setLabel("Start");
                if (timer!=null) {timer.stop();timer=null;}
            }
            else {
                nn=n1;
                ss=s1;
                blcopy(bl1,pview);
                blcopy(bl2,pview);
                x=-ss;y=(ROW-ss)/2;

                n1=Math.abs(r.nextInt())%NUM; /////
                s1=size[n1];
                blcopy2(pview,shape,n1);
                setBlockColor(pview);
                preview.repaint();
            }
        }
    }

    synchronized void ondown2()
    {
        bltake(bl1,x,y,box);
        if (canput(bl1,x+1,y,box)) {
            x++;
            while (canput(bl1,x+1,y,box)) x++;
            blput(bl1,x,y,box);
            board.repaint();
        }
        else {
            blput(bl1,x,y,box);
            filled(box);
            if (x<0) {
                IsStart=false;
                bStart.disable();
                bStart.setLabel("Start");
                if (timer!=null) {timer.stop();timer=null;}
            }
            else {
                nn=n1;
                ss=s1;
                blcopy(bl1,pview);
                blcopy(bl2,pview);
                x=-ss;y=(ROW-ss)/2;

                n1=Math.abs(r.nextInt())%NUM; /////
                s1=size[n1];
                blcopy2(pview,shape,n1);
                setBlockColor(pview);
                preview.repaint();

            }
        }
    }

    void setBlockColor(int[][] b)
    {
        int c,i,j;
        c=Math.abs(r.nextInt())%COLORS+1;
        for (i=0;i<4;i++)
          for (j=0;j<4;j++)
            if (b[i][j]!=0) b[i][j]=c;
        /*showStatus(c+"");*/
    }










//The End!

}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -