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

📄 mycanvas.java

📁 手机版俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 2005-5-18
 * Author: Liao Xuefeng, asklxf@163.com
 * Copyright (C) 2005, Liao Xuefeng.
 */
package com.crackj2ee.j2me.game.russia;

import javax.microedition.rms.*;
import javax.microedition.lcdui.*;

/**
 * The main canvas for game rendering.
 * 
 * @author Xuefeng
 */
public final class MyCanvas extends Canvas implements Runnable, CommandListener {

    // static:
    private static final int STATE_PREPARE  = 0;
    private static final int STATE_PLAYING  = 1;
    private static final int STATE_PAUSED   = 2;
    private static final int STATE_GAMEOVER = 3;
    private static final int STATE_INFO     = 4;

    private static final int NEXT_NO_ACTION = 0;
    private static final int NEXT_GO_LEFT   = 1;
    private static final int NEXT_GO_RIGHT  = 2;
    private static final int NEXT_GO_BOTTOM = 3;

    public static final int BORDER_COLOR = 0xeeeeee;
    public static final int INIT_ROWS = 20;
    public static final int INIT_COLS = 12;
    public static final int INIT_LEFT = 4;

    //-- member variables --
    // screen size:
    private int screen_width;
    private int screen_height;
    // box width & height:
    private int box_width;
    private int box_height;
    // reserved right's width:
    private int reserved_width;
    // game control:
    private long speed = 1000; // 1 second
    private boolean running = true;
    private int state = STATE_PREPARE;
    private int score = 0;
    private Thread thread = null;
    private Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    private String message;

    // controll full repaint:
    private boolean part_repaint = false;

    // main classes:
    private Background background;
    private ActiveShape active;

    // commands:
    private Command exitCommand = new Command("Exit", Command.EXIT, 0);

    private Command startCommand = new Command("Start", Command.ITEM, 0);
    private Command pauseCommand = new Command("Pause", Command.ITEM, 0);
    private Command resumeCommand = new Command("Resume", Command.ITEM, 0);

    private Command storeCommand = new Command("Store", Command.ITEM, 0);
    private Command top10Command = new Command("TOP 10", Command.ITEM, 0);

    public MyCanvas(boolean loadGame) {
        setCommandListener(this);
        //setFullScreenMode(true); // MIDP 2.0
        this.screen_width = getWidth();
        this.screen_height = getHeight();
        this.reserved_width = font.stringWidth("123456");
        // get box width & height:
        this.box_width = (screen_width-reserved_width) / INIT_COLS;
        this.box_height = screen_height / INIT_ROWS;
        // make box_width==box_height:
        box_width = Math.min(box_width, box_height);
        box_height = box_width;
        if(box_width<3)
            throw new IllegalArgumentException("Sorry, the screen of your cell phone is too small, and the game cannot continue.");
        // init background & activeShape:
        background = new Background(INIT_COLS, INIT_ROWS);
        active = new ActiveShape(INIT_LEFT);
        // set state:
        if(loadGame) {
            if(load()) { // load successfully
                // start the thread:
                thread = new Thread(this);
                thread.start();
                // waiting for thread to start:
                while(!thread.isAlive());
                pauseGame();
            }
            else prepareGame(); // load failed
        }
        else
            prepareGame();
    }

    public void addScore(int rows) {
        score += (rows * 10);
        if(rows==4) score+=10;
    }

    private void removeCommands() {
        removeCommand(exitCommand);
        removeCommand(startCommand);
        removeCommand(pauseCommand);
        removeCommand(resumeCommand);
        removeCommand(top10Command);
        removeCommand(storeCommand);
    }

    public void prepareGame() {
        // set commands:
        removeCommands();
        addCommand(exitCommand);
        addCommand(startCommand);
        // init:
        state = STATE_PREPARE;
        background.init();
        active.init();
        active.reset(background);
        repaint();
    }

    public void startGame() {
        // set commands:
        removeCommands();
        addCommand(exitCommand);
        addCommand(pauseCommand);
        addCommand(storeCommand);
        // init:
        state = STATE_PLAYING;
        repaint();
        // start the thread:
        thread = new Thread(this);
        thread.start();
        // waiting for thread to start:
        while(!thread.isAlive());
    }

    public void pauseGame() {
        removeCommands();
        addCommand(exitCommand);
        addCommand(storeCommand);
        addCommand(resumeCommand);
        state = STATE_PAUSED;
        // draw:
        repaint();
    }

    public void resumeGame() {
        removeCommands();
        addCommand(exitCommand);
        addCommand(pauseCommand);
        addCommand(storeCommand);
        state = STATE_PLAYING;
        repaint();
    }

    public void gameOver() {
        removeCommands();
        addCommand(exitCommand);
        addCommand(top10Command);
        state = STATE_GAMEOVER;
        repaint();
    }

    public void exitGame() {
        running = false;
        // waiting for thread to end:
        if(thread!=null) while(thread.isAlive());
        RussiaMIDlet.quitApp();
    }

    public void speedUp() { // min = 100
        if(speed>=200)
            speed-=100;
    }

    public void speedDown() { // max = 2000
        if(speed<=1900)
            speed+=100;
    }

    private void setPartRepaint() {
        part_repaint = true;
    }

    protected void paint(Graphics g) {
        if(!part_repaint) {
            // clear screen:
            g.translate(0 - g.getTranslateX(), 0 - g.getTranslateY());
            g.setColor(0); // black
            g.fillRect(0, 0, screen_width, screen_height);
            // paint background:
            background.paint(g, box_width);
            // show score:
            g.translate(INIT_COLS*box_width+6 - g.getTranslateX(), box_height*4+font.getHeight()*2 - g.getTranslateY());
            g.setColor(0xffffff);
            g.drawString("SCORE", 0, 0, Graphics.LEFT|Graphics.TOP);
            g.translate(0, font.getHeight()+2);
            g.setColor(0xff00);
            g.drawString(" "+score, 0, 0, Graphics.LEFT|Graphics.TOP);
        }
        part_repaint=false;
        if(state==STATE_GAMEOVER) {
            // show "GAME OVER":
            showInfo(g, "GAME OVER");
        }
        if(state==STATE_PLAYING) {
            // draw active shape:
            g.translate(1 - g.getTranslateX(), 1 - g.getTranslateY()); // (1,1)
            active.paint(g, box_width);
            // draw next shape:
            g.translate(INIT_COLS*box_width+6 - g.getTranslateX(), 0 - g.getTranslateY());
            g.setColor(0xffffff);
            g.drawString("NEXT", 0, 0, Graphics.LEFT|Graphics.TOP);
            g.translate(0, font.getHeight()+2);
            active.getNextShape().paint(g, box_width, 0xffffff);
        }
        if(state==STATE_PREPARE) {
            // show start:
            showInfo(g, "START");
        }
        if(state==STATE_PAUSED) {
            showInfo(g, "PAUSED");
        }

⌨️ 快捷键说明

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