0% found this document useful (0 votes)
149 views

GameProgramming 20060901

This technical article provides a summary of a simple mobile game programming demo on the A1200 handset. It discusses designing basic concepts like key mapping, game actions, and options panels. It also covers designing game rules, basic functions, and ways to improve the game by adding background images, music, and language selection. The full sample code is available online for developers to download and learn from.

Uploaded by

api-3730467
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

GameProgramming 20060901

This technical article provides a summary of a simple mobile game programming demo on the A1200 handset. It discusses designing basic concepts like key mapping, game actions, and options panels. It also covers designing game rules, basic functions, and ways to improve the game by adding background images, music, and language selection. The full sample code is available online for developers to download and learn from.

Uploaded by

api-3730467
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

A Simple Demo of

Mobile Game
Programming on the
A1200 Handset

August 29, 2006

TECHNICAL ARTICLE
A Simple Demo of Mobile Game Programming on the A1200 Handset
By
MOTODEV Staff

obile phone gaming has become one of the most important parts of the mobile market. In this
M document, we will introduce basic concepts of mobile game development using simple demo code
for the A1200 Ming handset.

Successful Mobile Games

Before beginning development of a mobile game, a developer should understand the market.
Specifically:

• What game types are most popular in the mobile market?


• What makes a game successful?

The three most popular game types are:

• Multi-player games
• Content-based games
• Video games with good high graphic quality

Although there are many different types, many of the most successful mobile games share the following
basic characteristics:

1. Easy to Learn/Easy to Play: Most customers aren’t interested in spending hours learning to play a
mobile game. Successful games are designed for the general consumer, not expert gamers.

2. Can be interrupted: Multitasking is at the heart of the mobile phone experience. Users often play
games in the short time between tasks (such as waiting for an email), and interruptions to answer
incoming calls or messages may be frequent. So a good mobile game will provide short-term
entertainment and allow the user to easily switch modes between the game and work.

3. Subscription-Based: The profit margin of a successful mobile game with high usage times may be
enhanced by a subscription-based model.

4. Interactive: No matter how well a game is designed, once the central puzzle is solved or all the levels
are completed, the novelty can wear off. A highly interactive game will hold a user’s interest longer than
a less interactive one.
5. Takes advantage of mobility: Cutting-edge games can utilize a device’s GPS, SMS, or MMS
capabilities to become true mobile experiences.

6. Suitable for a wide audience: A game that is suitable for all ages or interesting for many types of
users will have a larger potential

Mobile Game Programming

In this article, we will create a simple number-puzzle game. We will walk through the basics of
designing a game and defining basic functions. We will also discuss ways to improve the game with
background pictures and music.

The complete sample code for this game is available for download from the MOTODEV Web site at
http://developer.motorola.com/?path=1.873.

Key Mapping

Figure 1 Key Mapping for A1200


Game Action

Figure 2 Game Actions defined in GameCanvas class of MIDP 2.0

Designing the game

1. Game rules

Here, we are going to implement a number puzzle game. The rules are very simple:

1. Fill in the numbers 1-9 in the grid squares. Each number can be used only once.

2. The sums of the numbers on the lines through the center square (horizontally and vertically)
should be the same.

3. The sum of the four numbers at the corners should be equal to the sum of the four numbers in the
center of each side.

People like games with simple rules so that they can start easily. The best games are simple and also fun.
Our number puzzle is not very much fun, but that’s okay; it’s just a demo.

2. Basic Functions

Most games will have several options, but don’t add so many options that your game becomes difficult
to play. And don’t forget to offer your users a help function: it is easy to implement and useful, and it
can make your game more friendly.

Options Panel:

In this game, we have several options:

 Start playing
 New game
 Level Selection
 Music On/Off
 About (Help Info)
--------------------------------------
Options: Option Panel
--------------------------------------

package com.carol.gridGame;

import javax.microedition.lcdui.*;

public class Options extends Form implements CommandListener {

private MyResourceBundle myRes;

boolean easy;
boolean normal;
boolean hard;
boolean expert;

Command ok;
Command cancel;

Display dpy;
Displayable prev;

ChoiceGroup cg;

boolean[] scratch;

Options(Display dpy_, Displayable prev_, String strLanguage_, String title) {


super(title);

myRes = new MyResourceBundle(strLanguage_);

dpy = dpy_;
prev = prev_;

scratch = new boolean[2];

easy = false;
normal = true;
hard = false;
expert = false;

// REMIND should use a label here


cg = new ChoiceGroup(null, Choice.EXCLUSIVE);
cg.append(myRes.getString("EasyText"), null);
cg.append(myRes.getString("NormalText"), null);
cg.append(myRes.getString("HardText"), null);
cg.append(myRes.getString("ExpertText"), null);
append(cg);

loadUI();

ok = new Command(myRes.getString("OKText"), Command.OK, 0);


cancel = new Command(myRes.getString("CancelText"), Command.CANCEL, 1);

addCommand(ok);
addCommand(cancel);
setCommandListener(this);
}

public void commandAction(Command c, Displayable d) {


if (c == ok) {
readUI();
} else if (c == cancel) {
loadUI();
}
dpy.setCurrent(prev);
}

void loadUI() {
cg.setSelectedIndex(0, easy);
cg.setSelectedIndex(1, normal);
cg.setSelectedIndex(2, hard);
cg.setSelectedIndex(3, expert);
}

void readUI() {
easy = cg.isSelected(0);
normal = cg.isSelected(1);
hard = cg.isSelected(2);
expert = cg.isSelected(3);
}
}

---------------------------------------------------------
About: About box giving the rules
---------------------------------------------------------

/**
* Show the play method
* @author Carol Wu
* @since 2005.09.20
* @version 1.0
*/

package com.carol.gridGame;

import javax.microedition.lcdui.*;

/**
* Typical about box giving the rules
*/

public class About {

private static final String rule_US =


"author: Carol Wu\n\n"
+ "fill number \"1,2,3,4,5,6,7,8,9\" in the grids,\n each "
+ "number can be filled only once\n"
+ "Press\"<<\"to return to the former step(10 steps most)\n"
+ "Press\"C\"to clear the numbers in the grids\n\n"
+ "1. The sums of the number on the line through the center should the same
\n\n"
+ "2. The sum of the number at the corners equals to the sum of the ones in
the center of the 4 sides \n\n";

private static final String rule_CN =


"作者: Carol Wu\n\n"
+ "在空格中填入数字1,2,3,4,5,6,7,8,9,\n每个数字只能出现一次\n"
+ "按\"<<\"返回上一步(最多只能返回10步)\n"
+ "按\"C\"清除小方格中所填数字\n\n"
+ "1.经过中心格的每条直线(两条对角线,一条中心纵线和一条中心横线)上的数字的和相等.\n\n"
+ "2.4个角上的数字和与每边上中心格的数字和相等.\n\n";

/** the previous screen to go back to */


//private Displayable previous;

/**
* Do not allow anyone to create this class
*/
private About() {};

/**
* Put up the About box and when the user click ok return
* to the previous screen.
* @param display The <code>Display</code> to return to when the
* about screen is dismissed.
*/
public static void showAbout(Display display,String strLanguage) {
Alert alert;

// Add the howto info


if (strLanguage.equals("US")){
alert = new Alert("Rules");
alert.setString(rule_US);
}
else {
alert = new Alert("游戏规则");
alert.setString(rule_CN);
}

alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}

}
Figure 3 Options Panel

3. Design Pattern

Here we use the MVC pattern shown below.

Figure 4 MVC Pattern

4. Making Your Game Better

Adding a background picture for your game:

You can use the Image class to draw a background for you game as below.

try{
bgImage = Image.createImage("/Background.png");
}catch(Exception e){
test("file doesn’t exist");
}
g.drawImage(bgImage,0,0,Graphics.TOP | Graphics.LEFT);

Adding Background Music:

There are several ways to play sound with MIDP 2.0 Media API:

1. Use the Manager class to get a player for a selected media type;
2. Use the Player API to play media on the specific player;
3. Use the Control interface to control the playback of the media.

In the sample code, we use the first method.

Adding Language Selection:


Here we show you how to implement a simple ResourceBundle class.

---------------------------------------------------------
MyResourceBundle: for Language Selection
---------------------------------------------------------

package com.carol.gridGame;

public class MyResourceBundle {

private String strLanguage;


private Object[][] objs;

// constructor
public MyResourceBundle(String strLanguage_){
strLanguage=strLanguage_;
if (strLanguage=="CN"){
objs = new Res_zh_CN().getContents();
}
else if (strLanguage=="US"){
objs = new Res_en_US().getContents();
}
}

public String getString(String str_){


String str = "no string";
for(int i=0; i<objs.length; i++){
if (str_.equals((String)objs[i][0])){
str = (String)objs[i][1];
return str;
}
}
return str;
}

}
--------------------------------------
Res_en_US: English
--------------------------------------

package com.carol.gridGame;

public class Res_en_US {

static final Object[][] contents = new String[][]{


{ "TitleText", "playing..." },
{ "ExitText", "Exit" },
{ "StartText", "Start" },
{ "OptionText", "Level" },
{ "MusicOnText", "turn on music" },
{ "MusicOffText", "turn off music" },
{ "NewGameText", "New Game"},
{ "AboutText", "About"},
{ "YouWinText", "You Win!" },
{ "EasyText", "Easy"},
{ "NormalText", "Normao"},
{ "HardText", "Hard"},
{ "ExpertText", "Expert"},
{ "OKText", "OK"},
{ "CancelText", "Cancel"}};

public Object[][] getContents() {


return contents;
}

--------------------------------------
Res_zh_CN: Chinese
--------------------------------------

package com.carol.gridGame;

public class Res_zh_CN {

static final Object[][] contents = new String[][]{


{ "TitleText", "游戏进行中..." },
{ "ExitText", "退出" },
{ "StartText", "开始游戏" },
{ "OptionText", "难度选择" },
{ "MusicOnText", "打开音乐" },
{ "MusicOffText", "关闭音乐" },
{ "NewGameText", "新开一局"},
{ "AboutText", "关于游戏"},
{ "YouWinText", "恭喜!你赢了!" },
{ "EasyText", "容易"},
{ "NormalText", "一般"},
{ "HardText", "较难"},
{ "ExpertText", "专家级"},
{ "OKText", "确认"},
{ "CancelText", "取消"}};
public Object[][] getContents() {
return contents;
}

5. Running Effect

The images below show the completed application running on an A1200 Ming handset:

Figure 5 Running Effect 1


References

Java Community Process (JCP): http://www.jcp.org

Motorola Developer Network: http://developer.motorola.com

Motorola J2ME SDK v6.1 for Linux OS Products:


http://developer.motorola.com/?path=1.2.6.25.765.770

Beginning Mobile Phone Game Programming, by Michael Morrison:


http://www.bouncycastle.org

You might also like