俄罗斯方块源码(1)

类别:Java 点击:0 评论:0 推荐:

package Tetris;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class TetrisMIDlet extends MIDlet  implements CommandListener{
  Display display;
  TetrisGameCanvas tetrisGame;

  Command newCommand = new Command("New Game", Command.SCREEN, 1);
  Command levelCommand = new Command("Level", Command.SCREEN, 2);
  Command changeCommand = new Command("Change", Command.OK, 1);
  Command cancelCommand = new Command("Cancel", Command.CANCEL, 2);
  Command exitCommand = new Command("Exit", Command.EXIT, 3);

  public TetrisMIDlet() {
    display = Display.getDisplay(this);
    if (tetrisGame == null) {
      tetrisGame = new TetrisGameCanvas();
    }
    tetrisGame.addCommand(newCommand);
    tetrisGame.addCommand(levelCommand);
    tetrisGame.addCommand(exitCommand);
    tetrisGame.setCommandListener(this);
  }

  /**
   * 应用程序开始运行
   * @throws javax.microedition.midlet.MIDletStateChangeException
   */
  protected void startApp() throws javax.microedition.midlet.MIDletStateChangeException {
    /**@todo Implement this javax.microedition.midlet.MIDlet abstract method*/
    display.setCurrent(tetrisGame);
    try {
      Thread thread = new Thread(tetrisGame);
      thread.start();
    } catch(Error error) {
      destroyApp(false);
      notifyDestroyed();
    }
  }

  /**
   * 开始游戏等菜单命令的处理
   * @param command
   * @param displayable
   */
  public void commandAction(Command command, Displayable displayable) {
    if (command == newCommand) {
      tetrisGame.newGame();
    }
    else if (command == levelCommand) {
/*      Item [] levelItem = {new Gauge("Level", true, 400, tetrisGame.getLevel())};
      Form form = new Form("Change Level", levelItem);
      form.addCommand(changeCommand);
      form.addCommand(cancelCommand);
      form.setCommandListener(this);
      display.setCurrent(form);
*/
      tetrisGame.pauseGame();
      Form form = new Form("Change Level");
      ChoiceGroup cg = new ChoiceGroup("Level", Choice.EXCLUSIVE);
      for (int i = 1; i <= 10; i++) {
        cg.append("Level " + String.valueOf(i), null);
      }
      cg.setSelectedIndex(10 - tetrisGame.getLevel() / 35, true);
      form.append(cg);
      form.addCommand(changeCommand);
      form.addCommand(cancelCommand);
      form.setCommandListener(this);
      display.setCurrent(form);
    }
    else if (command == exitCommand) {
      tetrisGame.exit();
      destroyApp(false);
      notifyDestroyed();
    }
    else if (command == changeCommand) {
      Form form = (Form)displayable;
      ChoiceGroup cg = (ChoiceGroup)form.get(0);
      tetrisGame.setLevel(350 - cg.getSelectedIndex() * 35);
      tetrisGame.resumeGame();
      display.setCurrent(tetrisGame);
/*      Form form = (Form)displayable;
      Gauge gauge = (Gauge)form.get(0);
      tetrisGame.setLevel(gauge.getValue());
      display.setCurrent(tetrisGame);
*/
    }
    else if (command == cancelCommand) {
      display.setCurrent(tetrisGame);
    }
  }

  protected void pauseApp() {
    /**@todo Implement this javax.microedition.midlet.MIDlet abstract method*/
//    tetrisGame.pauseGame();
  }

  protected void destroyApp(boolean parm1) {
    /**@todo Implement this javax.microedition.midlet.MIDlet abstract method*/
    display.setCurrent(null);
    System.gc();
  }
}

 

本文地址:http://com.8s8s.com/it/it15459.htm