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

📄 tictactoegame.java

📁 J2ME超酷三子棋游戏代码,提供给大家参考!
💻 JAVA
字号:
package game;

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

public class TicTacToeGame extends MIDlet implements CommandListener{
  private static TicTacToeGame instance;
  //private Displayable1 displayable = new Displayable1();
  private GameCanvas gameCanvas;
  private Display aDisplay;
  private Displayable current;
  private Form aForm;
  private ChoiceGroup pieceChoice,playChoice;
  private Command okCommand;
  private Alert anAlert;
  private String CIRCLE_TEXT="Circle";
  private String CROSS_TEXT="Cross";
  private String PLAYER_FIRST="Player_First";

  /** Constructor */
  public TicTacToeGame() {
    instance = this;
  }

  /** Main method */
  public void startApp() {
    /*
     Display:代表系统显示屏的管理器,
     Displayable“代表可以在显示屏上显示的对象,象:Screen、Canvas都是它的子类;
     */
  aDisplay= Display.getDisplay(this);
  current=aDisplay.getCurrent();//如果current=null则 说明没有内容在屏幕上显示
  //利用Alert建立一个过渡屏幕
  anAlert=new Alert("TicTacToe","Rights Reserved!\2004 by sunxin",loadImage("/splash.png"),AlertType.INFO);
  anAlert.setTimeout(5000);//设定显示时间
  /**********
   一个进行游戏设置的界面;
   包括让玩家选择图标等
  **********/
 aForm=new Form("Preferrence");
 pieceChoice=new ChoiceGroup("Choose piece",Choice.EXCLUSIVE);
 pieceChoice.append(CIRCLE_TEXT,loadImage("/circle.png"));
 pieceChoice.append(CROSS_TEXT,loadImage("/cross.png"));

 playChoice=new ChoiceGroup("who plays first",Choice.MULTIPLE);
 playChoice.append("player first",loadImage("/player.png"));
 okCommand=new Command("play",Command.SCREEN,1);
 //把上面的元素加入Form中
 aForm.append(pieceChoice);
 aForm.append(playChoice);
 aForm.addCommand(okCommand);
 aForm.setCommandListener(this);

 if(current==null)//第一次显示
 {
   aDisplay.setCurrent(anAlert,aForm);
 }
 else
 {
   aDisplay.setCurrent(current);
 }

  }

  /** Handle pausing the MIDlet */
  public void pauseApp() {
  }

  /** Handle destroying the MIDlet */
  public void destroyApp(boolean unconditional) {

  }
  public void exitGame()
  {
    destroyApp(false);
    notifyDestroyed();
  }
  //完成游戏参数设置,进入游戏
public void setPreference(boolean isPlayerCircle,boolean isPlayerFirst)
  {
    gameCanvas=new GameCanvas(this,isPlayerCircle,isPlayerFirst);
        aDisplay.setCurrent(gameCanvas);
  }
  public  void commandAction(Command c,Displayable d)
  {
    boolean isPlayerCircle=pieceChoice.isSelected(0);
    boolean isPlayerFirst=playChoice.isSelected(0);
    if(c==okCommand)
    {
      this.setPreference(isPlayerCircle,isPlayerFirst);
    }
  }
  //建立Image对象
  public Image loadImage(String imageFile)
  {
    Image anImage;
    try
    {
      anImage=Image.createImage(imageFile);

    }
    catch(Exception e)
    {
      anImage=null;
    }
    return anImage;
  }
  /** Quit the MIDlet */
  public static void quitApp() {
    instance.destroyApp(true);
    instance.notifyDestroyed();
    instance = null;
  }

}

⌨️ 快捷键说明

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