Teach Yourself

Internet Game
           Programming with Java
                                              
in
21 Days

by Michael Morrison


C  O  N  T  E  N  T  S




Introduction

WEEK 1  At a Glance

Chapter 1  Games on the Web

Chapter 2  Java Game Programming

Chapter 3  Object-Oriented Programming with Java

Chapter 4  The Basics of Graphics

Chapter 5  Java Graphics Techniques

Chapter 6  Sprite Animation

Chapter 7  Sim Tarantula: Creepy Crawly Sprites

WEEK 1  In Review

WEEK 2  At a Glance

Chapter 8  User Input in Games

Chapter 9  Handling User Input with Java

Chapter 10  Traveling Gecko: Blistering Desert Fun

Chapter 11  The Basics of Sound

Chapter 12  Playing Sound with Java

Chapter 13  Scorpion Roundup: Action By Moonlight

Chapter 14  Squishing Bugs in Java Games

WEEK 2  In Review

WEEK 3  At a Glance

Chapter 15  Teaching Games to Think

Chapter 16  Connect4: Human versus Machine

Chapter 17  The Basics of Multiplayer Gaming

Chapter 18  Networking with Java

Chapter 19  NetConnect4: Human versus Human

Chapter 20  Optimizing Java Code for Games

Chapter 21  Assembling a Game Development Toolkit

WEEK 3  In Review

appendix A  Quiz Answers

appendix B  Keeping Up-to-Date on Java Game Programming

appendix C  Differences Between Java and C/C++

Credits



To my dad, who is the embodiment of the perfect father. You have taught me by example how confidence, integrity, and hard work can take me anywhere
I want to go. I love you dad, and I will be happy if I can
achieve only half of what you have!

Copyright © 1996 by Sams.net Publishing

FIRST EDITION

All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290.

International Standard Book Number: 1-57521-148-3

HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : leafwriters@leaf.stpn.soft.net

Trademarks

All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net Publishing 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 or service mark. Java is a trademark of Sun Microsystems, Inc.


President, Sams Publishing Richard K. Swadley
Team Leader Greg Wiegand
Managing Editor Cindy Morrow
Director of Marketing John Pierce
Assistant Marketing Managers Kristina Perry
Rachel Wolfe



Acquisitions Editor Chris DennyDevelopment Editor Jeff Koch
Software Development Specialist Steve StraigerProduction Editor Ryan Rader
Copy Editor Cheri ClarkIndexer Tom Dinse
Technical Reviewer Brad JonesEditorial Coordinator Bill Whitmer
Technical Edit Coordinator Lynette QuinnResource Coordinator Deborah Frisby
FormatterFrank Sinclair Editorial Assistants Carol Ackerman, Andi Richter, Rhonda Tinch-Mize
Cover Designer Tim AmrheinBook Designer Gary Adair
Copy Writer Peter FullerProduction Team Supervisor Brad Chinn
ProductionMary Ann Abramson, Stephen Adams, Debra Bolhuis, Georgiana Briggs, Charlotte Clapp, Jason Hand, Daniel Harris, Clint Lahnen, Ryan Oldfather, Casey Price, Dana Rhodes, Laura Robbins, Susan Springer


Acknowledgments

I would like to thank Chris Denny for giving me another opportunity to ramble about game programming. A big thanks also goes to Angelique Brittingham for her initial guidance and for treating me to artichoke lasagna.

I'd like to thank the love of my life, Mahsheed, whose jaw was dismantled during much of the development of this book, but who remained incredibly supportive, as always. I love you, girl!

I would also like to thank my parents, who have always encouraged and supported my nerdly interests-I love and miss you lots.

An enormous thanks goes to Dr. James Bertz for seeing to it that Mahsheed stayed healthy throughout her jaw surgery, for educating me about the finer points of orthognathic surgery, and for just being an all-around good person.

A big thanks goes to Greg Turner for his incredibly gracious help in putting together the network code for the NetConnect4 applet. Also, thanks to Sven Wiebus and Keith Pomakis, who were willing to let me rip apart their Connect4 AI code. And thanks to Jonathan Hardwick for his valuable information on Java optimization.

Finally, thanks to my friends in Nashville who are far away but definitely not forgotten: Keith, Heath, Mehrdad, Paul, Justin, Randy, Mike, Shawn, Travis, and Sarah. I miss you guys!


About the Author

Michael Morrison is a contributing author of Tricks of the Java Programming Gurus and Java Unleashed, and the co-author of Windows 95 Game Developer's Guide Using the Game SDK. He currently lives in Scottsdale, Arizona with his now almost legal partner in life, Mahsheed. When not glued to his computer, Michael enjoys skateboarding, mountain biking, and committing flagrant fouls in basketball. You can contact Michael at mmorrison@thetribe.com, or visit his Web site at http://www.thetribe.com.

Tell Us What You Think!

As a reader, you are the most important critic and commentator of our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.

Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at http://www.mcp.com.

Note
If you have a technical question about this book, call the technical support line at (800) 571-5840, ext. 3668.

As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book-as well as what we can do to make our books stronger. Here's the information:

Fax:(317) 581-4669
E-mail:programming_mgr@sams.mcp.com
Mail:Greg Wiegand
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290


Introduction

Now that the initial hype surrounding Java is starting to settle down, many developers are sitting around wondering what's next. So we have this really cool technology, but now what? The next logical step for Java programmers is to branch out and start applying their newfound skills to specific areas. One such area that has always intrigued me is games. Having a natural interest in games, I decided to apply my Java skills toward seeing what types of games could be written in Java. You're now holding the results of my experiment!

It turned out that even though Java has its weaknesses (namely in the area of performance), it is actually a very competent game development language. It is so competent, in fact, that after finishing the first sample game for this book, I wanted to distance myself from C++. If you are a C++ programmer interested in writing games in Java, this book is for you. However, a knowledge of C++ is by no means a prerequisite for this book. In fact, it is targeted toward beginning and intermediate Java programmers who are looking for some fun ways to use Java. Even if you aren't a programmer at all, you might find this book useful as a guide to incorporating games and entertainment into your Web site.

Throughout this book, you learn about game programming from a very Web-centric perspective. This is only natural because Java is very Web-centric itself. You learn all about the unique benefits (and drawbacks) of developing Internet games in Java for the Web, accompanied by plenty of source code that you can reuse on your own projects. More specifically, you learn about the following major issues related to Internet game programming with Java:

These topics are covered throughout 21 lessons; hence the title, Teach Yourself Internet Game Programming with Java in 21 Days! Over a three-week period, you move from learning the fundamentals of Java game programming to writing three complete games and a variety of other sample applets. The sample games include high-speed animation, sound, and network support for multiple players. By the end of this book, you'll be ready to build your own Internet Java games from scratch. By the way, all you need to build the sample games is the Java Developer's Kit, which is included on the CD-ROM. Good luck and have fun…I sure did!