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

📄 frame1.java

📁 JAVA俄罗斯方块源码,复制到java即可运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            ++accumulativeMark;  //删除多行时的分数加值;
            tempMark = Integer.parseInt(mark.getText().toString())+(2*accumulativeMark)-1;  //当前分数的值+1
            mark.setText("0000"+ tempMark);
            //级别累加器,分数每过100升一级,每加一级下落速度间隔减90毫秒,速度间隔到200以下后每级减30毫秒
            tempLevel = Integer.parseInt(level.getText().toString());
            if ( tempLevel < (tempMark/100)+1 ){
                level.setText( Integer.toString((tempMark/100)+1) );
                if ( thread.timeInterval>200)
                    thread.timeInterval -= 90;
                else
                    thread.timeInterval -= 30;
            }
            shapeLocation[i] = 0 ; //删除行以后把坐标清零
        }
    }

    //游戏结束方法
    private void gameOver(){
        begin.setText("  开始 ");
        GameOver.setVisible(true);
        t2.suspend();
    }

    //文本框的键盘事件
    public void jTextField1_keyPressed(KeyEvent e) {
        try{
            if (e.getKeyChar() == 52 && begin.getText() != "  继续") { //小键盘的“4”键,下落中的图形往左移一格
                //先判断左面的位置是否到边界,再判断左面的位置是否已经有图形
                if (shape[0].getX() <= 0 || shape[1].getX() <= 0 ||
                    shape[2].getX() <= 0 || shape[3].getX() <= 0)
                    return;
                System.out.print((shape[3].getX() / 18) + ":" +
                                 ((shape[3].getY() / 18) * 10 - 1) + "  ;  ");
                if ((backShape[(shape[0].getX() / 18) +
                     (shape[0].getY() / 18) * 10 - 1].getIcon() != null)
                    ||
                    (backShape[(shape[1].getX() / 18) +
                     (shape[1].getY() / 18) * 10 - 1].getIcon() != null)
                    ||
                    (backShape[(shape[2].getX() / 18) +
                     (shape[2].getY() / 18) * 10 - 1].getIcon() != null)
                    ||
                    (backShape[(shape[3].getX() / 18) +
                     (shape[3].getY() / 18) * 10 - 1].getIcon() != null)) {
                    return;
                }
                shape[0].setBounds(new Rectangle(shape[0].getX() - 18,
                                                 shape[0].getY(), 18, 18));
                shape[1].setBounds(new Rectangle(shape[1].getX() - 18,
                                                 shape[1].getY(), 18, 18));
                shape[2].setBounds(new Rectangle(shape[2].getX() - 18,
                                                 shape[2].getY(), 18, 18));
                shape[3].setBounds(new Rectangle(shape[3].getX() - 18,
                                                 shape[3].getY(), 18, 18));
            } else if (e.getKeyChar() == 54 && begin.getText() != "  继续") { //小键盘的“6”键,下落中的图形往右移一格
                //先判断右面是否到边界,再判断右面是否已经有图形
                if (shape[0].getX() >= 162 || shape[1].getX() >= 162 ||
                    shape[2].getX() >= 162 || shape[3].getX() >= 162)
                    return;
                if ((backShape[(shape[0].getX() / 18) +
                     (shape[0].getY() / 18) * 10 + 1].getIcon() != null)
                    ||
                    (backShape[(shape[1].getX() / 18) +
                     (shape[1].getY() / 18) * 10 + 1].getIcon() != null)
                    ||
                    (backShape[(shape[2].getX() / 18) +
                     (shape[2].getY() / 18) * 10 + 1].getIcon() != null)
                    ||
                    (backShape[(shape[3].getX() / 18) +
                     (shape[3].getY() / 18) * 10 + 1].getIcon() != null)) {
                    return;
                }
                shape[0].setBounds(new Rectangle(shape[0].getX() + 18,
                                                 shape[0].getY(), 18, 18));
                shape[1].setBounds(new Rectangle(shape[1].getX() + 18,
                                                 shape[1].getY(), 18, 18));
                shape[2].setBounds(new Rectangle(shape[2].getX() + 18,
                                                 shape[2].getY(), 18, 18));
                shape[3].setBounds(new Rectangle(shape[3].getX() + 18,
                                                 shape[3].getY(), 18, 18));
            } else if (e.getKeyChar() == 53 && begin.getText() != "  继续") { //小键盘的“5”键,下落中的图形往下移一格
                //先判断下面是否到边界,再判断下面是否已经有图形
                if (shape[0].getY() >= 342 || shape[1].getY() >= 342 ||
                    shape[2].getY() >= 342 || shape[3].getY() >= 342)
                    return;
                if ((backShape[(shape[0].getX() / 18) +
                     (shape[0].getY() / 18) * 10 + 10].getIcon() != null)
                    ||
                    (backShape[(shape[1].getX() / 18) +
                     (shape[1].getY() / 18) * 10 + 10].getIcon() != null)
                    ||
                    (backShape[(shape[2].getX() / 18) +
                     (shape[2].getY() / 18) * 10 + 10].getIcon() != null)
                    ||
                    (backShape[(shape[3].getX() / 18) +
                     (shape[3].getY() / 18) * 10 + 10].getIcon() != null)) {
                    return;
                }
                shape[0].setBounds(new Rectangle(shape[0].getX(),
                                                 shape[0].getY() + 18, 18, 18));
                shape[1].setBounds(new Rectangle(shape[1].getX(),
                                                 shape[1].getY() + 18, 18, 18));
                shape[2].setBounds(new Rectangle(shape[2].getX(),
                                                 shape[2].getY() + 18, 18, 18));
                shape[3].setBounds(new Rectangle(shape[3].getX(),
                                                 shape[3].getY() + 18, 18, 18));
            } else if (e.getKeyChar() == 56 && begin.getText() != "  继续") {
                rol.Rolling(); //旋转,调用Action类中的Rolling()方法;
            }
        }catch(ArrayIndexOutOfBoundsException out){ //如果出现下标越界,也就是块的坐标超出0-199之间,则忽略此次事件
            return;
        }
    }

    //开始按钮的点标事件
    public void begin_mouseClicked(MouseEvent e) {
        if(begin.getText()=="  开始 "){      //判断游戏是否在还没开始的状态
            GameOver.setVisible(false);  //“游戏结束!”不显示
            //清空背景
            for (int i=0; i<20; i++){
                for (int j=0; j<10; j++){
                    backShape[i*10+j].setIcon(null);
                }
            }
            //给形状加图形
            Random ran = new Random();
            int imgNum = ran.nextInt(5);
            imgicon = createImageIcon(imgNum + ".jpg");
            shapeView[0].setIcon(imgicon);
            shapeView[1].setIcon(imgicon);
            shapeView[2].setIcon(imgicon);
            shapeView[3].setIcon(imgicon);
            viewGraphicNum = ran.nextInt(7);  //得到形状的编号
            graphic.paint();  //设置形状
            paintGraphic();
            thread = new customThread(this);
            t2 = new Thread(thread);
            t2.start();  //线程开始
            begin.setText("  暂停");
        }else if (begin.getText()=="  暂停"){   //判断游戏是否在运行的状态
            t2.suspend();   //线程暂停
            begin.setText("  继续");
        }else if(begin.getText()=="  继续"){   //判断游戏是否在暂停的状态
            t2.resume();   //线程继续
            begin.setText("  暂停");
        }
    }
}

class Frame1_begin_mouseAdapter extends MouseAdapter {
    private Frame1 adaptee;
    Frame1_begin_mouseAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseClicked(MouseEvent e) {
        adaptee.begin_mouseClicked(e);
    }
}

class Frame1_jTextField1_keyAdapter extends KeyAdapter {
    private Frame1 adaptee;
    Frame1_jTextField1_keyAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void keyPressed(KeyEvent e) {
        adaptee.jTextField1_keyPressed(e);
    }
}

//鼠标的监听类
class customThread implements Runnable{ //接口来实现testThread类
    Frame1 frame;
    int timeInterval = 800;

    public customThread(Frame1 frame){  //有参构造方法
        this.frame = frame;
    }

    public void run(){  //重新定义run方法
        for(int i=0;i<17;i++){  //每个线程只允许跑17次
            try{
                Thread.sleep((int)(timeInterval));  //线程休眠timeInterval毫秒,表示每下落一格的间格
                if (frame.shape[0].getY()>=342 || frame.shape[1].getY()>=342 || frame.shape[2].getY()>=342 || frame.shape[3].getY()>=342 ){
                    frame.copy();
                    return;
                }else if ( (frame.backShape[(frame.shape[0].getX()/18) + (frame.shape[0].getY()/18)*10 +10].getIcon() != null)
                          ||(frame.backShape[(frame.shape[1].getX()/18) + (frame.shape[1].getY()/18)*10 +10].getIcon() != null)
                          ||(frame.backShape[(frame.shape[2].getX()/18) + (frame.shape[2].getY()/18)*10 +10].getIcon() != null)
                          ||(frame.backShape[(frame.shape[3].getX()/18) + (frame.shape[3].getY()/18)*10 +10].getIcon() != null) ){
                    frame.copy();
                    return;
                }
                frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX(), frame.shape[0].getY()+18, 18, 18));
                frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX(), frame.shape[1].getY()+18, 18, 18));
                frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX(), frame.shape[2].getY()+18, 18, 18));
                frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX(), frame.shape[3].getY()+18, 18, 18));

            }catch(InterruptedException e){
                System.out.println(e.toString());
            }
        }
        frame.copy();
    }
}

⌨️ 快捷键说明

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