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

📄 mycanvas.java

📁 手机版俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if(state==STATE_INFO) {
            showInfo(g, message);
        }
    }

    private void showInfo(Graphics g, String str) {
        g.translate(0 - g.getTranslateX(), 0 - g.getTranslateY());
        int str_width = g.getFont().stringWidth(str);
        int str_height = g.getFont().getHeight();
        int center_x = box_width*INIT_COLS / 2;
        int center_y = box_height*INIT_ROWS / 2;
        int left = center_x - str_width / 2;
        int top = center_y - str_height / 2;
        g.setColor(0xffffff);
        g.drawRect(left-4, top-2, str_width+8, str_height+4);
        g.setColor(0);
        g.fillRect(left-3, top-1, str_width+6, str_height+2);
        g.setColor(0xffffff);
        g.drawString(str, left, top, Graphics.LEFT|Graphics.TOP);
    }

    public void run() {
        long startTime = 0;
        long interruptedTime = 0;
        long sleepTime = 0;
        int shapeCount = Shape.SHAPES.length;
        while(running) {
            if(state==STATE_PLAYING) {
                startTime = System.currentTimeMillis();
                if(sleepTime<=0) // last cycle run out!
                    sleepTime=speed;
                try {
                    Thread.sleep(sleepTime);
                    sleepTime = 0;
                }
                catch(InterruptedException ie) {
                    interruptedTime = System.currentTimeMillis();
                    // should continue sleep:
                    sleepTime = speed - (interruptedTime - startTime);
                    if(sleepTime>0) continue;
                }
                // ok, should move 1 step down:
                boolean b = active.moveDown(background);
                if(b) {
                    repaint();
                }
                else { // cannot move down more!
                    background.merge(active); // merge in to background.
                    // try to remove some full rows:
                    background.markRemovingRows();
                    int removedRows = background.doRemove();
                    if(removedRows>0) {
                        System.out.println("Removed " + removedRows + " rows.");
                        addScore(removedRows);
                    }
                    // start a new shape:
                    boolean ok = active.reset(background);
                    if(!ok) {
                        // game over!
                        gameOver();
                    }
                    repaint();
                }
            }
        }
    }

    protected void keyRepeated(int keyCode) {
        keyPressed(keyCode);
    }

    protected void keyPressed(int keyCode) {
        if(state==STATE_PLAYING) {
            int action = getGameAction(keyCode);
            switch(action) {
            case Canvas.UP:
                // change immediately:
                if(active.change(background)) {
                    setPartRepaint();
                    repaint();
                }
                break;
            case Canvas.LEFT:
                // move left immediately:
                if(active.moveLeft(background)) {
                    setPartRepaint();
                    repaint();
                }
                break;
            case Canvas.RIGHT:
                // move right immediately:
                if(active.moveRight(background)) {
                    setPartRepaint();
                    repaint();
                }
                break;
            case Canvas.DOWN:
                // move down immediately:
                active.fallDown(background);
                repaint();
                break;
            default:
                if(keyCode==Canvas.KEY_NUM0)
                    pauseGame();
            }
        }
        else if(state==STATE_PAUSED) {
            if(keyCode==Canvas.KEY_NUM0)
                resumeGame();
        }
        else if(state==STATE_PREPARE) {
            startGame();
        }
        else if(state==STATE_GAMEOVER) {
            //show top 10
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c==exitCommand) exitGame();
        if(c==pauseCommand) pauseGame();
        if(c==resumeCommand) resumeGame();
        if(c==startCommand) startGame();
        if(c==top10Command) {
            // show top 10
        }
        if(c==storeCommand) {
            int currentState = this.state;
            state = STATE_INFO;
            message = "Please wait...";
            repaint();
            message = store() ? "OK" : "FAILED";
            repaint();
            try {
                Thread.sleep(1000);
            }
            catch(Exception e) {
                System.out.println("interrupted");
            }
            this.state = currentState;
        }
    }

    private RecordStore openRecordStore() {
        try {
            return RecordStore.openRecordStore("R_LXF_V10", true);
        }
        catch(RecordStoreException re) {
            return null;
        }
    }

    private boolean load() {
        // open:
        RecordStore rs = openRecordStore();
        if(rs==null) return false;
        // read 1 records:
        RecordEnumeration re = null;
        try {
            re = rs.enumerateRecords(null, null, true);
            if(re.hasNextElement()) {
                byte[] data = re.nextRecord();
                int n1 = active.setRecordData(data, 0);
                if(n1==(-1)) return false;
                int n2 = background.setRecordData(data, n1);
                if(n2==(-1)) return false;
                if(data.length<=n1+n2) return false;
                try {
                    String s = new String(data, n1+n2, data.length-n1-n2);
                    score = Integer.parseInt(s);
                    return true;
                }
                catch(NumberFormatException ne) {
                    return false;
                }
            }
        }
        catch(Exception e) {}
        finally {
            try { re.destroy(); } catch(Exception e) {}
            try {rs.closeRecordStore(); } catch(Exception e) {}
        }
        return false;
    }

    private boolean store() {
        // open:
        RecordStore rs = openRecordStore();
        if(rs==null) return false;

        try {
            byte[] data1 = active.getRecordData();
            byte[] data2 = background.getRecordData();
            byte[] data3 = (""+score).getBytes();
            byte[] data = new byte[data1.length + data2.length + data3.length];
            // data = data1+data2+data3:
            for(int i=0; i<data1.length; i++)
                data[i] = data1[i];
            int offset=data1.length;
            for(int i=0; i<data2.length; i++)
                data[i+offset] = data2[i];
            offset = data1.length+data2.length;
            for(int i=0; i<data3.length; i++)
                data[i+offset] = data3[i];
            
            // add or update:
            if(rs.getNumRecords()==0) {
                rs.addRecord(data, 0, data.length);
                return true;
            }
            else {
                RecordEnumeration re = null;
                try {
                    re = rs.enumerateRecords(null, null, true);
                    int id = re.nextRecordId();
                    rs.setRecord(id, data, 0, data.length);
                    return true;
                }
                catch(Exception e) {}
                finally {
                    try { re.destroy(); } catch(Exception e) {}
                }
            }
        }
        catch(Exception e) {}
        finally {
            try {rs.closeRecordStore(); } catch(Exception e) {}
        }
        return false;
    }
}

⌨️ 快捷键说明

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