This page intentionally left blank
© 2004 by Premier Press, a division of Course Technology. All rights reserved. No
part of this book may be reproduced or t...
For G. I.
Acknowledgments

I

f you’ve never written a book, I have to tell you that it’s not easy. Once upon a time I
was an armcha...
Acknowledgments

To Tarek, Sahar, Radfan, and Suleiman for the encouragement and support only friends
can give.
To Rick, m...
About the Author

MARTIN J. WELLS began programming his own games on a Tandy micro-computer more
than 20 years ago. Throug...
Letter from the Series Editor
Writing games for PCs is fun, but it just doesn’t have the feel of a console or other handhe...
Contents at a Glance

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv

Part I
Chap...
Contents at a Glance

Chapter 10

The Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
...
Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

Part I

What Is J2ME? ...
Contents

Chapter 3

J2ME-Enabled Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
MID Overview . .
Nok...
xiv

Contents

Chapter 5

The J2ME API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
MIDP API ...
Contents
Motorola . . . . . . . . . . . .
Setup . . . . . . . . . . . .
GameScreen . . . . . .
Sprites and PlayField
Image...
xvi

Contents
Adventure Games . . . . . . . .
Strategy Games . . . . . . . . . .
Traditional Games . . . . . . . .
Designi...
Contents
Moving at an Angle . . . . . . . . . . . .
Cartesian Space . . . . . . . . . . . . .
Directional Movement . . . ....
xviii

Contents
Creating a Tile Engine . . . . . . . . . . .
The Tile Map . . . . . . . . . . . . . . .
Rendering the Tile...
Contents

Chapter 13

The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465
Front End Overv...
J2me game programming
J2me game programming
J2me game programming
Upcoming SlideShare
Loading in...5
×

J2me game programming

253

Published on

j2me game,java game,game java free,tai game java,java mobile,java game development,download java game cho dien thoai

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
253
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "J2me game programming "

  1. 1. This page intentionally left blank
  2. 2. © 2004 by Premier Press, a division of Course Technology. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Course PTR, except for the inclusion of brief quotations in a review. The Premier Press logo and related trade dress are trademarks of Premier Press and may not be used without written permission. J2ME is a trademark of Sun Microsystems, Inc. in the United States and other countries. Photoshop and Acrobat are registered trademarks of Adobe Systems Incorporated in the United States and other countries. Paint Shop Pro is a registered trademark of Jasc Software in the United States and other countries. SourceForge is a trademark of VA Software Corporation. RetroGuard is a trademark of Retrologic Systems in the United States and other countries. The MathFP package can only be used for non-commercial purposes. Permission to use the Nokia Developer’s Suite 2.0 for J2ME, Nokia 7210 MIDP SDK 1.0, and, Nokia 3510i MIDP SDK 1.0, have been granted by Nokia, Inc. (c) 2004. Nokia, Inc. All rights reserved. All other trademarks are the property of their respective owners. Important: Course PTR cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. Course PTR and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Course PTR from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Course PTR, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press. Educational facilities, companies, and organizations interested in multiple copies or licensing of this book should contact the publisher for quantity discount information. Training manuals, CD-ROMs, and portions of this book are also available individually or can be tailored for specific needs. ISBN: 1-59200-118-1 Library of Congress Catalog Card Number: 2003109341 Printed in the United States of America Course PTR, a division of Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com SVP, Course Professional, Trade, Reference Group: Andy Shafran Publisher: Stacy L. Hiquet Senior Marketing Manager: Sarah O’Donnell Marketing Manager: Heather Hurley Manager of Editorial Services: Heather Talbot Acquisitions Editor: Mitzi Koontz Associate Marketing Manager: Kristin Eisenzopf Project Editor: Jenny Davidson Technical Reviewer: Cristiano Garibaldi Retail Market Coordinator: Sarah Dubois Copy Editor: Cathleen Snyder Interior Layout Tech: Marian Hartsough Cover Designer: Steve Deschene CD-ROM Producer: Brandon Penticuff Indexer: Sharon Shock Proofreader: Sara Gullion
  3. 3. For G. I.
  4. 4. Acknowledgments I f you’ve never written a book, I have to tell you that it’s not easy. Once upon a time I was an armchair critic of books—in hindsight, maybe that’s why I decided to write one. The thing that strikes you when you embark on writing something like this is the sheer mountain of (hard) work involved. In fact, it’s so much work that it really isn’t possible for one person to do it alone. Throughout the writing of this book, I’ve been helped in so many ways by so many people that I can’t hope to get across how grateful I am. All I can do is say a thank you and trust you to know how much I mean it. I will, however, attempt to recognize some of the people who deserve a little acknowledgment—if I’ve forgotten you, then obviously you didn’t deserve it. . . . :) First, thanks to André LaMothe for giving me the opportunity to contribute to such an excellent series. To Mitzi Koontz, Jenny Davidson, and Cathleen Snyder for the excellent feedback, continual support, and understanding when things didn’t quite go according to plan—we got there in the end. Thanks also to Cristiano Garibaldi for covering all the technical bases (and learning along with me). To Colin Pyle and J. Alexander von Kotze: Thanks for never giving up on the dream of making games for a living (and an extra thanks to Colin for supplying the excellent sprites used in the examples). To Blake, for allowing me to use his laptop sometimes, and to Ryan, for showing me how a CD drive opens a thousand times. To Vandana and Pratibha Rai for buying me some time (and thanks for the math books laaa). To Scott, Rhandy, Simon, Rob, Gibbo, Mike, Jules, Kristy, Kat, Lee, and the rest of the team for keeping the day job challenging, interesting, and fun. vi
  5. 5. Acknowledgments To Tarek, Sahar, Radfan, and Suleiman for the encouragement and support only friends can give. To Rick, my only mentor, for showing me that computing really is a science. To my mum, for showing me that being creative is a way of life and dad for throwing in regular doses of reality. And finally, to the one and only G.I.: Thanks for believing from day one we could do it, and then bearing the brunt of following through on that belief. vii
  6. 6. About the Author MARTIN J. WELLS began programming his own games on a Tandy micro-computer more than 20 years ago. Throughout an extensive career in the IT industry, he has worked in many diverse fields involving a huge variety of computer languages and systems, including Java from its origins. He has extensive experience in media, communications, and entertainment industry development and has founded successful companies in all of these areas. Martin lives with his wife and two sons in Sydney, Australia. He loves playing soccer and inline hockey, reading, and playing with anything cool and interesting (including his sons). About the Series Editor ANDRÉ LAMOTHE, CEO, Xtreme Games LLC, has been involved in the computing industry for more than 25 years. He wrote his first game for the TRS-80 and has been hooked ever since! His experience includes 2D/3D graphics, AI research at NASA, compiler design, robotics, virtual reality, and telecommunications. His books are top sellers in the game programming genre, and his experience is echoed in the Premier Press Game Development books. viii
  7. 7. Letter from the Series Editor Writing games for PCs is fun, but it just doesn’t have the feel of a console or other handheld device. However, the thought of creating an embedded game for a phone was completely out of the question a few years ago, unless you wanted to call up Nokia or Motorola and see if you could have the contract to create the on-board games. (I wish someone would have; they are terrible!) Anyway, luckily for us, new phones support a number of technologies that allow programmers to create fantastic applications. One such technology is Java II Micro Edition, or J2ME. And that’s what this book is all about—writing games for any phone that supports the J2ME standard. When I first thought of doing a book on J2ME game programming, I knew that I wanted it to push the envelope to set a new standard on what can be done on a phone. That means I had to find an author who was an expert on the platform, but was also willing to push limits and do the impossible, in a manner of speaking. I have to say that I am very happy with this book. The author, Martin Wells, had the same vision about wanting to create the most amazing book on phone/J2ME game programming. For example, he knew that he had to put a chapter on 3D in the book and talk about optimization and other advanced topics. The bottom line is that this book is the best book on the market about making real games on the J2ME platform; moreover, it’s written by someone who has made numerous games on the platform. Marty knows the ins and outs and tricks of the system, which is invaluable in such a complex subject area with so many other choices to confuse you. The other amazing thing about this book is that it is completely self-contained; if you don’t know Java 2, there is a Java 2 primer contained within, so more or less all you need is your phone, the book, and some time and you are going to be creating J2ME games on your own phone! I think that this is an amazing thing to be able to do. It’s like having your own little game console in your hand. You can play your own games or give them to your friends, or possibly even sell and market them (which is also covered within the book). In conclusion, if you have been interested in writing games for phones under the J2ME platform, but don’t know where to start, how to integrate all the technology, or make sense of all the different APIs, then this is the book for you. Rarely can a single book empower someone to do so much, but Martin Wells has done an amazing job of it. André LaMothe Series Editor, Premier Game Development Series
  8. 8. Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv Part I Chapter 1 J2ME History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Chapter 2 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Chapter 3 J2ME-Enabled Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 Part II Show Me the Code! . . . . . . . . . . . . . . . . .47 Chapter 4 The Development Environment . . . . . . . . . . . . . . . . . . . . . .49 Chapter 5 The J2ME API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Chapter 6 Device-Specific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Chapter 7 Game Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Part III Game On . . . . . . . . . . . . . . . . . . . . . . . .245 Chapter 8 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 Chapter 9 x What Is J2ME? . . . . . . . . . . . . . . . . . . . . . .1 The Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269
  9. 9. Contents at a Glance Chapter 10 The Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Chapter 11 The World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 Chapter 12 The Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433 Chapter 13 The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465 Chapter 14 The Device Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493 Chapter 15 The Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519 Chapter 16 The Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535 Part IV Sell, Sell, Sell . . . . . . . . . . . . . . . . . .541 Chapter 17 Marketing Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543 Chapter 18 Sales Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553 Part V What Next? . . . . . . . . . . . . . . . . . . . . . .569 Chapter 19 CLDC 1.1 and MIDP 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . .571 Chapter 20 Isometric Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .621 Chapter 21 Ray Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .639 Chapter 22 Making the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . .669 Appendix A Java 2 Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .693 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738 xi
  10. 10. Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part I What Is J2ME? . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1 J2ME History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Java’s Acorn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Java’s Growth in the Sun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 So What Is Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Multiple Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Micro Devices Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Micro Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Chapter 2 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 I Shall Call It Mini-ME . . . . . . . . J2ME Architecture . . . . . . . . . . . Configurations and Profiles Two Sizes Fit All . . . . . . . . . MIDP . . . . . . . . . . . . . . . . . . . . . Target Devices . . . . . . . . . . . MIDP Applications . . . . . . . MIDP 1.0 Libraries . . . . . . . . MIDP 2.0 . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . xii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 .14 .15 .17 .28 .28 .31 .35 .37 .38
  11. 11. Contents Chapter 3 J2ME-Enabled Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 MID Overview . . Nokia . . . . . . Sony Ericsson Motorola . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 .40 .44 .45 .46 Part II Show Me the Code! . . . . . . . . . . . . . . . . .47 Chapter 4 The Development Environment . . . . . . . . . . . . . . . . . . . . .49 Getting the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . Installing the Software . . . . . . . . . . . . . . . . . . . . . Setting Up the JDK . . . . . . . . . . . . . . . . . . . . . Setting Up the MIDP . . . . . . . . . . . . . . . . . . . . Making Path Changes . . . . . . . . . . . . . . . . . . . Setting MIDP_HOME . . . . . . . . . . . . . . . . . . . . Updating CLASSPATH . . . . . . . . . . . . . . . . . . . Testing the Setup . . . . . . . . . . . . . . . . . . . . . . Baking Some MIDlets! . . . . . . . . . . . . . . . . . . . . . . Compiling Your Application . . . . . . . . . . . . . . Preverifying Your Class File . . . . . . . . . . . . . . . Running Your MIDlet . . . . . . . . . . . . . . . . . . . Creating the Full Package . . . . . . . . . . . . . . . . . . . Hello, Again . . . . . . . . . . . . . . . . . . . . . . . . . . Building the Class . . . . . . . . . . . . . . . . . . . . . . Creating the JAR . . . . . . . . . . . . . . . . . . . . . . . Creating the JAD . . . . . . . . . . . . . . . . . . . . . . Running the Package . . . . . . . . . . . . . . . . . . . The J2ME Wireless Toolkit . . . . . . . . . . . . . . . . . . . Installing the Toolkit . . . . . . . . . . . . . . . . . . . . Using the KToolbar . . . . . . . . . . . . . . . . . . . . . Working with Other Development Environments JBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sun ONE Studio . . . . . . . . . . . . . . . . . . . . . . . . Other IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 .50 .51 .51 .52 .53 .54 .54 .55 .58 .58 .59 .59 .60 .61 .61 .62 .63 .64 .64 .65 .70 .71 .73 .75 .76 xiii
  12. 12. xiv Contents Chapter 5 The J2ME API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 MIDP API Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 The MIDlet Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78 Using Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Working with the Connector . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Working with HttpConnection . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Persistence (RMS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Record Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Enumerating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Comparing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 Listening In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 User Interface (LCDUI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110 UI Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118 Command Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 High-Level UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Forms and Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 Low-Level UI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Chapter 6 Device-Specific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . .173 Device-Specific Libraries . . . . . . . . . Nokia . . . . . . . . . . . . . . . . . . . . . . . . Installing the Tools . . . . . . . . . . The Nokia UI API . . . . . . . . . . . . Siemens . . . . . . . . . . . . . . . . . . . . . . Installing . . . . . . . . . . . . . . . . . . SMS and Phone API . . . . . . . . . . Device Control . . . . . . . . . . . . . Sounds and Tunes . . . . . . . . . . . Advanced Graphics and Gaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 .174 .174 .176 .198 .198 .199 .200 .201 .202
  13. 13. Contents Motorola . . . . . . . . . . . . Setup . . . . . . . . . . . . GameScreen . . . . . . Sprites and PlayField ImageUtil . . . . . . . . PaletteImage . . . . . . Other Extensions . . . . . . Conclusion . . . . . . . . . . . Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 .203 .204 .207 .208 .208 .210 .210 Game Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Game Time . . . . . . . . . . . . . . . Game Design . . . . . . . . . . . . . . The Application Class . . . . . . . The Menu . . . . . . . . . . . . . . . . The Game Screen . . . . . . . . . . . The Game Loop . . . . . . . . . . . . Adding the Graphics . . . . . . . . Double Buffering . . . . . . . Drawing the World . . . . . . Linking to the Application The Actors . . . . . . . . . . . . . . . . Many Roles . . . . . . . . . . . . The Wombat . . . . . . . . . . . Movement . . . . . . . . . . . . . Cars and Trucks . . . . . . . . . Bringing Them to Life . . . . Input Handling . . . . . . . . . . . . Collision Detection . . . . . . . . . Game Over . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . Part III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 .211 .212 .214 .216 .217 .221 .221 .223 .225 .226 .226 .230 .231 .233 .235 .239 .240 .242 .244 Game On . . . . . . . . . . . . . . . . . . . . . . . .245 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 The State of Play . . Platform Issues Game Types . . . . . . Action Games . Puzzle Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 .248 .250 .250 .251 xv
  14. 14. xvi Contents Adventure Games . . . . . . . . Strategy Games . . . . . . . . . . Traditional Games . . . . . . . . Designing Your Game . . . . . . . . The Platform . . . . . . . . . . . . What Makes a Good Game? The Inspiration . . . . . . . . . . Visualize Things . . . . . . . . . The Design Document . . . . . The Development Process . . . . . Creating a Prototype . . . . . Developing the Game . . . . . Finish What You . . . . . . . . . Your Idea . . . . . . . . . . . . . . . . . . Game Type . . . . . . . . . . . . . Game Name . . . . . . . . . . . . Setting . . . . . . . . . . . . . . . . . Game Play . . . . . . . . . . . . . . Features . . . . . . . . . . . . . . . Interface . . . . . . . . . . . . . . . Resources . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 .251 .252 .252 .253 .255 .257 .258 .258 .260 .260 .261 .262 .263 .263 .264 .264 .265 .265 .267 .267 .268 The Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269 Sprite Basics . . . . . . . . . . . Preparing Graphics . . Loading the Images . Getting Some Action Advanced Sprites . . . . . . . States Versus Frames . More Action . . . . . . . And Beyond . . . . . . . Conclusion . . . . . . . . . . . . Chapter 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269 .270 .273 .277 .282 .282 .297 .301 .302 The Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Getting Some Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Basic Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304
  15. 15. Contents Moving at an Angle . . . . . . . . . . . . Cartesian Space . . . . . . . . . . . . . Directional Movement . . . . . . . Simulating Floating-Point Math A Moving Example . . . . . . . . . . Advanced Motion . . . . . . . . . . . . . . Velocity . . . . . . . . . . . . . . . . . . . Acceleration . . . . . . . . . . . . . . . Free Flight . . . . . . . . . . . . . . . . . Adding Bounce . . . . . . . . . . . . . Collision Detection . . . . . . . . . . . . . Basic Intersection . . . . . . . . . . . Intersection through Exclusion . Reacting to a Collision . . . . . . . Advanced Collision Detection . . A Collision Example . . . . . . . . . Actors . . . . . . . . . . . . . . . . . . . . . . . The New Actor Class . . . . . . . . . Revised Ship Class . . . . . . . . . . . Revised Game Screen . . . . . . . . Adding Weapon Fire . . . . . . . . . The Enemy . . . . . . . . . . . . . . . . . . . . Computing Distance . . . . . . . . . Targeting an Angle . . . . . . . . . . Turning through Spin . . . . . . . . Aligning Directions . . . . . . . . . . Creating an Enemy . . . . . . . . . . Creating a Brain . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 .305 .305 .307 .311 .317 .318 .318 .319 .321 .325 .325 .326 .328 .330 .332 .342 .342 .345 .347 .349 .355 .355 .356 .358 .361 .364 .365 .366 The World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 A New World . . . . . . . . . . . Nature of the Universe World Coordinates . . . Scrolling the View Port Panning the View Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 .367 .368 .369 .370 xvii
  16. 16. xviii Contents Creating a Tile Engine . . . . . . . . . . . The Tile Map . . . . . . . . . . . . . . . Rendering the Tile Layer . . . . . . Tile Collision Detection . . . . . . . Animated Tiles . . . . . . . . . . . . . Activator Tiles . . . . . . . . . . . . . . Non-Collidable Tiles . . . . . . . . . Background Layers . . . . . . . . . . Putting It All Together . . . . . . . Building Worlds . . . . . . . . . . . . . . . . Level Design . . . . . . . . . . . . . . . Array Maps . . . . . . . . . . . . . . . . Random Level Generator . . . . . Using a Map Editor . . . . . . . . . . Advanced Object Management . . . . Object Pooling . . . . . . . . . . . . . Linked Lists . . . . . . . . . . . . . . . . Mutually Exclusive Lists . . . . . . . Tracking Object Use . . . . . . . . . The Actor Pool Class . . . . . . . . . Adapting the World to Pooling Conclusion . . . . . . . . . . . . . . . . . . . . Chapter 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373 .374 .376 .378 .379 .380 .383 .384 .386 .388 .388 .390 .390 .407 .416 .417 .418 .421 .422 .423 .428 .432 The Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433 The Game Screen . . . . . . . . . . . . . Game State . . . . . . . . . . . . . . . . . Adding State to GameScreen The Primary Actor . . . . . . . . . . . . Ship Types . . . . . . . . . . . . . . . Expanding the Enemy . . . . . . Dealing with Damage . . . . . . . . . Adding Energy . . . . . . . . . . . The Shield Effect . . . . . . . . . . Energy Bar . . . . . . . . . . . . . . . Dealing with Death . . . . . . . . Saving and Loading . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433 .433 .434 .437 .438 .442 .445 .446 .448 .450 .452 .458 .464
  17. 17. Contents Chapter 13 The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465 Front End Overview . . . . . . . . . . The Application Class . . . . . . . . Configuration Options . . . . Configuring Keys . . . . . . . . Saving and Loading Settings The Menus . . . . . . . . . . . . . . . . . The Splash Screen . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . Chapter 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465 .467 .470 .472 .475 .479 .488 .492 The Device Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493 Nokia Customization . . . . . . . . . . . FullCanvas . . . . . . . . . . . . . . . . Vibration . . . . . . . . . . . . . . . . . Using Reflection and Rotation Build Systems . . . . . . . . . . . . . . . . . Setting Up Ant . . . . . . . . . . . . Creating a Build File . . . . . . . . Using Antenna . . . . . . . . . . . . Multi-Device Builds . . . . . . . . . . . . Device-Specific Code . . . . . . . . Managing Resources . . . . . . . . The Complete Build . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . Chapter 15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493 .493 .495 .496 .500 .501 .502 .505 .512 .512 .514 .515 .518 The Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519 Speed, Glorious Speed . . . . . . When to Optimize . . . . . Don’t Optimize Blindly . . Method Profiling . . . . . . Memory Profiling . . . . . . Garbage Monitoring . . . . Optimization . . . . . . . . . . . . . High-Level Optimizations Low-Level Optimizations Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519 .519 .520 .521 .523 .525 .527 .527 .529 .534 xix

×