二、实现游戏的步骤

  下图显示的是一个游戏MIDlet在成功安装和运行之后用户界面状态的典型的变化流程。我们想通过一个游戏者的视角来阐述开发移动游戏的过程。


图3 用户界面状态图表

  1 开始游戏

  在用户启动MIDlet之后,将显示游戏特定的闪动屏幕。闪动屏幕是FullCanvas的一个实例。它可用于显示一个公司的标志或者用动画形式介绍游戏。除了End键以外的所有键盘事件(MIDlet可用的)都可以跳过闪动屏幕并显示主菜单。还应该设置一个时间限定,能够在一定的时间过后自动跳出闪动屏幕进入游戏屏幕。
GameMIDlet类是游戏的基本类;它处理MIDlet的生命周期并且处理游戏显示。下面的代码是闪动屏幕和游戏MIDlet类的构架。

//Skeleton for the base class of game
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GameMIDlet extends MIDlet {
 private Display display = null;
 //Splash screen that starts the application
 private SplashFullCanvas splash;
 public GameMIDlet() {
  splash = new SplashFullCanvas(this);
 }
 protected void startApp() throws MIDletStateChangeException {
  if (display == null) {
   display = Display.getDisplay(this);
  }
  //splash screen to the display
  setDisplayable(splash);
 }
 protected void pauseApp() {
}

 protected void destroyApp(boolean p0)
 throws MIDletStateChangeException {
 }

 public void setDisplayable(Displayable dl) {
  display.setCurrent(dl);
 }
}

//Skeleton for the splash screen in Nokia Java Game
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
import com.nokia.mid.ui.*;

public class SplashFullCanvas extends FullCanvas {
 private GameMIDlet parent = null;
 private MainMenu menu = null;
 private Timer timer = null;
 public SplashFullCanvas(GameMIDlet parent) {
  this.parent = parent;
  menu = new MainMenu(
   Resources.getString(Resources.ID_GAME_NAME),
   List.IMPLICIT, parent);
   startTimer();
 }
protected void paint(Graphics g) {
 //Do the splash screen here
}
protected void keyPressed(int keyCode) {
 timer.cancel();
 timer = null;
 //All key events received set the main menu to the screen
 parent.setDisplayable(menu);
}

//Timer for the splash screen. Main menu is set to the display
//after 5 seconds.
private void startTimer() {
 TimerTask task =new TimerTask() {
  public void run() {
   parent.setDisplayable(menu);
  }
 };
 timer = new Timer();
 timer.schedule(task, 5000);
}
}

  2 主菜单(MainMenu)屏幕

  主菜单是包含游戏特定选项的固有目录("Continue"、"New game"、"Options"、"High scores"、"Instructions"、"About"和"Exit game")。"Continue"只有在游戏被暂停的时候才能被显示。当"Continue"显示的时候,它必须是目录列表的第一个元素。主菜单的标题必须是游戏的名称。下面的代码是主菜单的框架。

//Skeleton for the main menu
import javax.microedition.lcdui.*;
public class MainMenu extends List implements CommandListener {
 private GameMIDlet parent = null;
 private GameFullCanvas game = null;
 public MainMenu(String p0, int p1, String[] p2, Image[] p3,
 GameMIDlet parent) {
  super(p0, p1, p2, p3);
  init(parent);
 }
 public MainMenu(String p0, int p1, GameMIDlet parent) {
  super(p0, p1);
  init(parent);
 }
 public void init(GameMIDlet parent) {
  this.parent = parent;
  this.setCommandListener(this);
  //if game paused then "Continue" should be available in
  //selection list
  if (game != null && game.isPaused()) {
   if(!(this.getString(0).equals(
    new String(Resources.getString(
     Resources.ID_GAME_CONTINUE))))) {
      this.insert(0,
       Resources.getString(Resources.ID_GAME_CONTINUE),
       null);
     }
     this.setSelectedIndex(0,true);
    }
   else {
    //These must be with or without icons
    this.append(Resources.getString(Resources.ID_GAME_NEW), null);
    this.append(Resources.getString(Resources.ID_GAME_OPTIONS),null);
    this.append(Resources.getString(
     Resources.ID_GAME_HIGHSCORES), null);
    this.append(Resources.getString(
    Resources.ID_GAME_INSTRUCTIONS), null);
    this.append(Resources.getString(Resources.ID_GAME_ABOUT), null);
    this.append(Resources.getString(Resources.ID_GAME_EXIT), null);
   }
  }
  public void commandAction(Command p0, Displayable p1) {
   List lis = (List) p1;
   String selected =
   lis.getString(lis.getSelectedIndex());
   if (selected.equals(Resources.getString(Resources.ID_GAME_NEW))) {
    game = new GameFullCanvas(parent, this);
    parent.setDisplayable(game);
   }
   else if (selected.equals(
    Resources.getString(Resources.ID_GAME_OPTIONS))) {
     parent.setDisplayable(
      new OptionList(Resources.getString(Resources.ID_GAME_OPTIONS),
       List.IMPLICIT,
       parent, this));
    }
   else if (selected.equals(
    Resources.getString(Resources.ID_GAME_HIGHSCORES))) {
     parent.setDisplayable(new HighScore(parent, this));
    }
   else if (selected.equals(
    Resources.getString(Resources.ID_GAME_INSTRUCTIONS))) {
     parent.setDisplayable(
      new Instructions(
       Resources.getString(Resources.ID_GAME_INSTRUCTIONS),parent,this));
    }
   else if (selected.equals(
    Resources.getString(Resources.ID_GAME_ABOUT))) {
     parent.setDisplayable(
      new About(
       Resources.getString(Resources.ID_GAME_ABOUT),
       parent,
       this));
    }
   else if (selected.equals(
    Resources.getString(Resources.ID_GAME_EXIT))) {
     parent.notifyDestroyed();
   }
   else if (selected.equals(
    Resources.getString(Resources.ID_GAME_CONTINUE))) {
     if (game != null) {
      game.gameContinue();
      parent.setDisplayable(game);
   }
  }
 }
}

3 游戏屏幕

  如果用户从主菜单中选择"New game",那么开始游戏并且显示游戏屏幕。游戏屏幕使用全屏画布(FullCanvas)。如果按下任何功能键,那么用户界面必须返回主菜单,并且应使游戏暂停。其他的按键对游戏是有效的。注意:游戏不应该在屏幕上创建任何功能键的标签。如果必须使用功能键标签,那么应用程序应该使用默认的Canvas屏幕Commands。示例代码没有解决诸如线程和线程安全等问题,这些问题在设计的时候必须格外注意。下面的代码是游戏屏幕的框架。

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
public class GameFullCanvas extends FullCanvas {
private GameMIDlet parent = null;
private MainMenu menu = null;
private boolean gamePaused = false;
public GameFullCanvas(GameMIDlet parent, MainMenu menu) {
this.parent = parent;
this.menu = menu;
}
protected void paint(Graphics g) {
//Paint the game screen here
}
protected void keyPressed(int keyCode) {
if (keyCode == KEY_SOFTKEY1 || keyCode == KEY_SOFTKEY2
|| keyCode == KEY_SOFTKEY3) {
gamePaused = true;
//main menu to the screen
menu.init(parent);
parent.setDisplayable(menu);
}
}
public void gameContinue() {
gamePaused = false;
}
public boolean isPaused() {
return gamePaused;
}
}

  4 游戏选项屏幕

  用户可以通过选择主菜单中的"Options"选项改变特定的游戏选项。Options列表是固有的列表,包含处理游戏设置的条目,例如:声音、振动(见一、8节)、音调等等。如果要回到主菜单的话,需要使用Back命令。下面的代码是Options列表的框架。

  注意:如果游戏被安装到Games菜单的话,就不需要lights/sounds设置条目了,因为那些选项已经由Games菜单提供了。

import javax.microedition.lcdui.*;
public class OptionList extends List implements CommandListener {
private GameMIDlet parent = null;
private MainMenu menu = null;
private KeyDefinitions def = null;
private Command back = new Command("", Command.BACK, 2);
public OptionList(String p0, int p1, String[] p2, Image[] p3,
GameMIDlet parent, MainMenu menu) {
super(p0, p1, p2, p3);
this.menu = menu;
init(parent);
}
public OptionList(String p0, int p1, GameMIDlet parent,
MainMenu menu) {
super(p0, p1);
this.menu = menu;
init(parent);
}
private void init(GameMIDlet parent) {
this.parent = parent;
this.addCommand(back);
this.setCommandListener(this);
//These are just a examples for the game specific options
this.append(Resources.getString(Resources.ID_GAME_LEVEL),
null);
this.append(Resources.getString(Resources.ID_GAME_SOUNDS),
null);
this.append(Resources.getString(Resources.ID_GAME_VIBRA),
null);
}
public void commandAction(Command p0, Displayable p1) {
if (p0 == back) {
parent.setDisplayable(menu);
}
else {
List lis = (List) p1;
int idx = lis.getSelectedIndex();
switch (idx) {
case 0:
//TODO
break;
case 1:
//TODO
break;
case 2:
//TODO
break;
case 3:
parent.setDisplayable(def);
break;
//More if needed
default:
break;
}
}
}
}

5 高分屏幕

  当用户从主菜单中选择"High scores"选项的时候,高分就会显示出来。高分是显示在全屏画布(FullCanvas)实例上的。分数应该在一个屏幕上就显示完,而不要卷动页面,因为这样会给用户带来麻烦。
当然了,高分屏幕也可能会包含一些图片或者动画。用户应该能够通过按下左功能键、右功能键、数字键或者Send键返回主菜单。这个例子不提供任何处理高分的机制。处理高分的两种典型的方法是通过使用记录管理服务(RMS)永久保存分数或者通过HTTP连接把高分保存到服务器中。下面的代码是高分的框架。

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
public class HighScore extends FullCanvas {
private GameMIDlet parent = null;
private MainMenu menu = null;
public HighScore(GameMIDlet parent, MainMenu menu) {
this.parent = parent;
this.menu = menu;
}
protected void paint(Graphics g) {
//Paint the high scores here
}
public void keyPressed(int keyCode) {
if (keyCode != KEY_END) {
//selection list to the screen
parent.setDisplayable(menu);
}
}
}

  6 教学屏幕

  当用户从主菜单中选择"Instructions"条目的时候,就会显示出游戏的规则。游戏规则文本放置在Form实例中。Form中应该包含用于进入下一条教学规则的命令(例如,标签"More")和回到主菜单的命令(例如,标签"Back")。"More"一般由左功能键控制,而"Back"由右功能键控制。如果教学规则里包含动画,那么这些动画应该使用全屏画布(FullCanvas)来显示。按下左功能键将跳过动画进入下一个教学屏幕。按下右功能键,用户将从动画中回到主菜单。键End将结束应用程序。下面的代码是文字教学规则和动画的框架。

//Text instructions. Text can be written in constructor or in own
method.
//Developer should remember that also instruction texts should be
//internationalized
import javax.microedition.lcdui.*;
public class Instructions extends Form implements CommandListener {
//Command for going next instruction if needed
private Command more = new Command(
Resources.getString(Resources.ID_GAME_MORE),
Command.OK, 1);
//Command for going back to the main menu
private Command back = new Command("", Command.BACK, 2);
private GameMIDlet parent = null;
private MainMenu menu = null;
public Instructions(String title, GameMIDlet parent, MainMenu
menu) {
super(title);
this.parent = parent;
this.menu = menu;
this.addCommand(back);
this.addCommand(more);
this.setCommandListener(this);
}
public void commandAction(Command p0, Displayable p1) {
if (p0 == more) {
//go to the next if needed e.g animation
parent.setDisplayable(new InstructionAnimation(parent));
}
else if (p0 == back) {
parent.setDisplayable(menu);
}
}
}
//Instruction animation
import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
public class InstructionAnimation extends FullCanvas {
private GameMIDlet parent = null;
public InstructionAnimation(GameMIDlet parent) {
this.parent = parent;
}
protected void paint(Graphics g) {
//Do the animation here
}
public void keyPressed(int keyCode) {
if (keyCode == KEY_SOFTKEY1) {
//go to the next instruction screen if needed
}
else if (keyCode == KEY_SOFTKEY2) {
//selection list to the screen
parent.setDisplayable(new MainMenu(
Resources.getString (
Resources.ID_GAME_NAME),
List.IMPLICIT, parent));
}
}
}

  7关于(About)屏幕

  关于(About)屏幕显示游戏制作公司的消息文本或标志。当用户从主菜单中选择"About"选项的时候,就会启动这个屏幕。和教学规则页面一样,关于屏幕页面如果只需要文本信息的话,那么可以使用Form来实现。如果需要图像或动画,那么应该使用Canvas或FullCanvas。

//Text "About" code skeleton
import javax.microedition.lcdui.*;
public class About extends Form implements CommandListener {
//Command for going back to the main menu
private Command back = new Command("", Command.BACK, 1);
private GameMIDlet parent = null;
private MainMenu menu = null;
public About(String title, GameMIDlet parent, MainMenu menu) {
super(title);
this.parent = parent;
this.menu = menu;
this.addCommand(back);
this.setCommandListener(this);
}
public void commandAction(Command p0, Displayable p1) {
if (p0 == back) {
parent.setDisplayable(menu);
}
}
}

  8 退出

  从主菜单中选择"Exit game"选项来中止游戏并释放所有的资源。

  9 Resources类

  Resources类不是一个用户界面类,与本文中介绍的其他类不同。这个类处理国际化问题。

/**
* A simple class to simulate a resource bundle.
* Modify the contents of this class according to the
* locales/languages you want your application to support.
* In your application, retrieve a string using code such as the
* following:
*
* String s = Resources.getString(Resources.ID_GAME_NEW);
*
* Copyright (C) 2002 Nokia Corporation
*/
public class Resources {
// Identifiers for text strings.
public static final int ID_GAME_NEW = 0;
public static final int ID_GAME_OPTIONS = 1;
public static final int ID_GAME_HIGHSCORES = 2;
public static final int ID_GAME_INSTRUCTIONS = 3;
public static final int ID_GAME_ABOUT = 4;
public static final int ID_GAME_CONTINUE = 5;
public static final int ID_GAME_BACK = 6;
public static final int ID_GAME_MORE = 7;
public static final int ID_GAME_EXIT = 8;
public static final int ID_GAME_LEVEL = 9;
public static final int ID_GAME_SOUNDS = 10;
public static final int ID_GAME_VIBRA = 11;
public static final int ID_GAME_NAME = 12;
// List of supported locales.
// The strings are Nokia-specific values
// of the "microedition.locale" system property.
private static final String[] supportedLocales = {
"en", "fi-FI", "fr", "de"
};
//NOTE: default language must be the first one
//for getString to work!
// Strings for each locale, indexed according to the
// contents of supportedLocales
private static final String[][] strings = {
{ "New game", "Settings", "High scores", "Instructions",
"About","Continue", "Back", "More", "Exit game",
"Level", "Sounds","Shakes", "Game name" },
{ "Uusi peli", "Asetukset", "Huipputulokset", "Peliohjeet",
"Tietoja","Jatka", "Poistu", "Jatka", "Poistu",
"Vaikeusaste", "Peli??net", "V?rin?tehosteet",
"Pelin nimi" },
{ "Nouveau jeu", "Paramètres", "Scores", "Instructions",
"A propos","Continuer", "Retour", "Suite", "Sortir",
"Niveau", "Sons", "Vibrations", "Jeu nom" },
{ "Neues Spiel", "Einstellungen", "Rekord", "Anleitung",
"über","Weiter", "Zurück", "Weiter", "Beenden",
"Ebene", "Ton", "Vibrationen", "Spiel name" }
};
/**
* Gets a string for the given key.
* @param key Integer key for string
* @return The string
*/
public static String getString(int key) {
String locale = System.getProperty("microedition.locale");
if (locale == null) {
locale = new String(""); // use empty instead of null
}
// find the index of the locale id
int localeIndex = -1;
for (int i = 0; i < supportedLocales.length; i++) {
if (locale.equals(supportedLocales[i])) {
localeIndex = i;
break;
}
}
// not found
if (localeIndex == -1) {
// defaults to first language, in this example English
return strings[0][key];
}
return strings[localeIndex][key];
}
}