Table Of ContentMichael Morrison
800 East 96th St.,Indianapolis,Indiana,46240 USA
Beginning Mobile Phone Game Programming Associate Publisher
Copyright©2005 by Sams Publishing Michael Stephens
All rights reserved. No part of this book shall be reproduced, stored in a Acquisitions Editor
retrieval system, or transmitted by any means, electronic, mechanical, Loretta Yates
photocopying, recording, or otherwise, without written permission from
the publisher. No patent liability is assumed with respect to the use of Development Editor
the information contained herein. Although every precaution has been Mark Renfrow
taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions. Nor is any liability assumed for Managing Editor
damages resulting from the use of the information contained herein. Charlotte Clapp
International Standard Book Number: 0-672-32665-5
Project Editor
Library of Congress Catalog Card Number: 2004095066
George Nedeff
Printed in the United States of America
Copy Editor
First Printing: October 2004
Margo Catts
07 06 05 4 3 2
Indexer
Trademarks Chris Barrick
All terms mentioned in this book that are known to be trademarks or
Proofreader
service marks have been appropriately capitalized. Sams Publishing
Jessica McCarty
cannot attest to the accuracy of this information. Use of a term in this
book should not be regarded as affecting the validity of any trademark
Technical Editor
or service mark.
David Franson
Warning and Disclaimer
Team Coordinator
Every effort has been made to make this book as complete and as accu- Cindy Teeters
rate as possible, but no warranty or fitness is implied. The information
provided is on an “as is” basis. The author and the publisher shall have Designer
neither liability nor responsibility to any person or entity with respect to Gary Adair
any loss or damages arising from the information contained in this
book. Page Layout
Cheryl Lynch
Contents at a Glance
Part I Getting Started with Mobile Game Programming
1 Games on the Go
2 Mobile Java Game Development Basics
3 Constructing a Mobile Game Skeleton
Part II Mobile Game Programming Essentials
4 Mobile Game Graphics 101
5 Using Sprite Animation
6 Handling Mobile User Input
7 Henway: Paying Tribute to Frogger
8 Making Noise with Tones
9 Playing Digitized Sound and Music
Part III Virtual Worlds and Mobile Game Intelligence
10 Creating Tiled Game Layers
11 Managing Multiple Game Layers
12 High Seas: A Game for the Pirate in You
13 Teaching Games to Think
Part IV Taking Advantage of the Wireless Network
14 Mobile Game Networking Essentials
15 Connect 4: A Classic Game Goes Wireless
16 Debugging and Deploying Mobile Games
Part V Sprucing Up Your Games
17 Optimizing Mobile Java Games
18 Space Out: Paying Tribute to Space Invaders
19 Keeping Track of High Scores
Part VI Appendixes
A Java Game API Reference
B Mobile Game Programming Resources
C Creating Graphics for Mobile Games
Bonus Java Programming Primer (CD-ROM)
Index
Table of Contents
Introduction 1
Part I: Getting Started with Mobile Game Programming
CHAPTER 1: Games on the Go 9
Essentials of Mobile Gaming ............................................................10
The First Mobile Phone Game ..................................................11
The Market for Mobile Games ..................................................12
The Culture of Mobile Games ..................................................12
The Bleeding Edge of Mobile Gaming ......................................13
Getting to Know Mobile Platforms ....................................................15
Java 2 Micro Edition (J2ME) ....................................................16
Binary Runtime Environment for Wireless (BREW) ....................17
Symbian ..................................................................................17
Windows Mobile Smartphone ..................................................18
Java As a Mobile Game Platform ....................................................19
What Is Java? ..........................................................................19
Why Java? ..............................................................................19
Java and Mobile Game Programming ......................................20
A Quick J2ME Primer ......................................................................22
Configurations and the Connected Limited Device
Configuration (CLDC) ..............................................................23
Profiles and the MIDP ..............................................................24
Summary ........................................................................................26
Field Trip ........................................................................................27
vi Beginning Mobile Phone Game Programming
CHAPTER 2: Mobile Java Game Development Basics 29
Game Design Basics ........................................................................30
Coming Up with the Basic Idea .............................................. 30
Developing the Storyline ........................................................ 31
Establishing the Play Modes .................................................... 31
A J2ME Game Development Primer ................................................ 32
Getting to Know the J2ME Wireless Toolkit ...................................... 35
Using KToolbar ...................................................................... 36
Managing MIDlet Game Projects ............................................ 37
Building a Game MIDlet ........................................................ 39
Testing a Game MIDlet .......................................................... 39
The J2ME Emulator and Physical Devices ........................................ 42
Summary ...................................................................................... 43
Field Trip ........................................................................................ 43
CHAPTER 3: Constructing a Mobile Game Skeleton 45
Exploring the J2ME APIs ................................................................ 46
The CLDC API ........................................................................ 47
The MIDP API ........................................................................ 48
Understanding MIDlets .................................................................. 50
Inside a MIDlet ...................................................................... 50
The Basics of MIDlet Development .......................................... 52
Building the Skeleton Example Program ........................................ 53
Writing the Program Code ...................................................... 54
Preparing the MIDlet for Distribution ...................................... 59
Building and Testing the Finished Product .............................. 61
Summary ........................................................................................62
Extreme Game Makeover ................................................................63
Contents vii
Part II: Mobile Game Programming Essentials
CHAPTER 4: Mobile Game Graphics 101 67
Mobile Graphics Basics ....................................................................68
Understanding the Graphics Coordinate System ......................68
Learning the Basics of Color ....................................................70
Working with Graphics in J2ME ......................................................72
Drawing Graphics Primitives ....................................................73
Drawing Text ..........................................................................77
Drawing Images ......................................................................79
Building the Olympics Example Program ........................................81
Writing the Program Code ......................................................81
Testing the Finished Product ....................................................84
Building the Slideshow Example Program ........................................84
Writing the Program Code ......................................................84
Testing the Finished Product ....................................................89
Summary ........................................................................................90
Extreme Game Makeover ................................................................90
CHAPTER 5: Using Sprite Animation 91
Understanding Animation ..............................................................92
Animation and Frame Rate ......................................................92
Making the Move to Computer Animation ..............................93
2D Versus 3D Animation ..........................................................94
Analyzing 2D Sprite Animation ......................................................95
Frame-Based Animation ..........................................................95
Cast-Based Animation ............................................................96
Applying Sprite Animation to Mobile Games ................................102
Working with the Layer and Sprite Classes ..................................103
viii Beginning Mobile Phone Game Programming
Achieving Smooth Animation with the GameCanvas Class ..............106
Building the UFO Example Program ..............................................108
Writing the Program Code ......................................................108
Testing the Finished Product ..................................................116
Summary ......................................................................................116
Extreme Game Makeover ..............................................................117
CHAPTER 6: Handling Mobile User Input 119
Assessing Mobile Game Input ........................................................120
Handling Key Input with the GameCanvas Class ..............................121
Revisiting the Sprite Class ............................................................123
Detecting Sprite Collisions ......................................................123
Working with Frame-Animated Sprites ..................................125
Building the UFO 2 Example ..........................................................127
Writing the Program Code ......................................................127
Testing the Finished Product ..................................................133
Summary ......................................................................................134
Extreme Game Makeover ..............................................................134
CHAPTER 7: Henway: Paying Tribute to Frogger 135
The Scoop on Henway ....................................................................136
Designing the Game ......................................................................138
Developing the Game ....................................................................139
Writing the Game Code ..........................................................140
Testing the Game ..........................................................................151
Summary ......................................................................................153
Extreme Game Makeover ..............................................................154
CHAPTER 8: Making Noise with Tones 155
Sound and Mobile Games ..............................................................156
A Tonal Sound and Music Primer ..................................................157
Querying a Phone for Its Audio Capabilities ..................................159
Playing Tones in Mobile Games ....................................................162
Contents ix
Playing Individual Tones ........................................................163
Playing a Tone Sequence ........................................................164
Building the UFO 3 Example Program ............................................168
Writing the Program Code ......................................................169
Testing the Finished Product ..................................................174
Summary ......................................................................................174
Field Trip ......................................................................................174
CHAPTER 9: Playing Digitized Sound and Music 175
A Digitized Sound Primer ..............................................................176
Getting to Know Wave Sounds ......................................................177
Creating and Editing Wave Sounds ................................................179
Revisiting the Player Interface ......................................................180
Playing Wave Sounds in Mobile Games ..........................................182
Playing a Wave from a JAR File ..............................................182
Playing a Wave from a URL ..................................................184
Feeling the Music with MIDI ..........................................................184
Playing MIDI Music in Mobile Games ............................................186
Playing a MIDI Song from a JAR File ......................................186
Playing a MIDI Song from a URL ..........................................187
Building the Henway 2 Example Game ..........................................188
Writing the Game Code ..........................................................189
Testing the Finished Product ..................................................193
Summary ......................................................................................193
Field Trip ......................................................................................194
Part III: Virtual Worlds and Mobile Game Intelligence
CHAPTER 10: Creating Tiled Game Layers 197
What Is a Tiled Layer? ..................................................................198
Creating Maps for Tiled Layers ......................................................200
Using the Mappy Map Editor ..................................................201
Using the Tile Studio Map Editor ............................................205
Formatting Map Information for Games ................................206