JaVa
   

Java 1.4 Game Programming

Andrew Mulholland and Glenn Murphy Archive.org Cataloging-in-Publication Data Mulholland, Andrew.
     Java 1.4 game coding / by Andrew Mulholland and Glenn Murphy.
         p. cm.
           1.   Java (Computer program language) 2.  Computer games—Programming.
     I.  Murphy, Glenn, 1908- II.  Title.
     QA76.73.J38 M849 2003      794.8'152762—dc21                                                2002155485
                                                                                             CIP © 2003, Wordware Publishing, Inc. All Rights Reserved
Los Rios Boulevard
Plano, Texas 75074 No part of this tutorial may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc. 10  9  8  7  6  5  4  3  2  1
Java is a trademark of Oracle
All brand names and product names mentioned in this tutorial are things or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or things should not be regarded as intent to infringe on the property of others. The uploader recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All inquiries for volume purchases of this tutorial should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling: (972) 423-0090 About the Authors This for me is probably the easiest part of the tutorial to write. I really have a liking for writing about myself as I always feel it will sound boastful and conceited, which I am. Well, I started life as a baby and I'm afraid that's where it all began. I started coding at around 16 years of age while attending college in my hometown of Manchester, England. I use the term "attended" loosely, however, because as soon as the coding began, I was hooked, and ducked out of many a lecture to create a variety of games, albeit on an 80x25 ASCII character resolution and a useful gotoxy(x, y) method. In the second year of college, a friend and coding buddy, Nick Kitson, and I co-wrote a 16,000-line soccer management game in Pascal called ESM European Soccer Manager, where you could actually watch the matches in an overhead view. Working on this taught me more than anything about programming. My advice—pick a goal and go for it. After college, I made it into the Computer Games Technology program at the University of Abertay Dundee and am now midway through the (honours) 4th year. While "attending" university I have furthered my knowledge from Pascal to C/C++ and then on to Java. The ability to make web games playable in a browser (applets) was what originally made Java so appealing to me, and had been a mystery to me for long enough. There began my introduction to Java. Before Java, I was mainly a procedural programmer, and not that well tuned to object-oriented coding (OOP). The good thing about Java, in this sense, is that it is completely OOP, so there was no choice but to program in this style. For this I think learning Java is the best guide to OOP you can get. And then came the tutorial. After working on a Java game over the summer of 2000, I teamed up with my now co-author Andrew, who has been my flatmate and friend since the first year of university. The university's random accommodation allocation for freshers can take the credit for us meeting in the student halls. We began work on the tutorial in late 2001, while both juggling our honours degree courses at the same time. Besides losing my virginity and trying to complete Jet Set Willy, this tutorial has been the most grueling experience of my life, but it was all worth it in the end. (I hope this last sentence makes it to publication.) My primary hope for this tutorial is that it makes me as much money as possible. My secondary hope, besides coding games in Java, is that the tutorial indicates the difficulties that we came across when researching Java for games coding in a clear manner, especially those surrounding threads, input, and graphics that we put a lot of work into. I think to become a good programmer you have to enjoy it; otherwise it's little use. Most of the enjoyment I find is in showing off what I have done, which there is no harm in now and again (and again and again J). One thing I am aware of is that in actually challenging yourself to do something, and believing that you can do it, there seems to be a fear factor where you often do not even attempt to code something, because you have never done it before. In buying this tutorial, you have made a solid move in conquering this fear. My interests mostly revolve around playing pool, watching films, and occasionally coding the night away, and there is still no better feeling than "7-balling" someone in a crowded club. I also collect Star Wars costumes and wear them out clubbing regularly. As a final word, I hope this tutorial is as useful to you as it was for me in writing it, and wish you luck in your quest of knowledge. Glenn Murphy
glenn@bugmenot.com This for me is probably one of the hardest parts of the tutorial to write. I really have a dislike for writing about myself as I always feel it will sound boastful or conceited. Nevertheless, here goes… Well, I am currently 21 years old and halfway through my 4th (honours) year of university studying BSc (Hons) Computer Games Technology at the University of Abertay in Dundee, Scotland. I would say I have been coding for around six years now and have obtained quite a broad range of skills within this time. My first real stab at game coding was about a year and a half before I left home to go to university when I downloaded the DJGPP DOS compiler (http://www.delorie.com/djgpp) and the Allegro game library (http://www.talula.demon.co.uk/allegro) originally started by Shawn Hargreaves. After starting and never finishing a few projects, the next logical step was to move onto looking at OpenGL and DirectX. As well as looking at the 3D side of games coding however, I also invested time in learning Perl and MySQL, although I have recently switched to using PHP4 as it is sooo much nicer than Perl. ;) From there, I then progressed onto Java and to be honest it is probably the best thing I have ever done. Java really is such a great language. Don't get me wrong—it does have some issues, but the structure of the language and also the documentation is really excellent (ever tried using MSDN? Urgh.). One thing that deceived me, however, when I started to use Java was the simplicity. When you start looking into Java properly, you think—ah great, all the libraries have been written for me. However, as you will see as you progress through the tutorial, these libraries are excellent for business app development, but there are some pitfalls and serious issues to consider when looking at the language and libraries (packages) from a game development point of view. Probably now is a good time for a plug. Previously to this tutorial, I coauthored Developer's Guide to Multiplayer Games, which focuses in detail on using sockets in C/C++ to create client-server games. That tutorial has a large tutorial section that takes you through the process of writing a reusable network library and a multiplayer game, which also includes a signup/login and lobby system. What the tutorial does not cover, however, is DirectPlay as we remain platform independent throughout the tutorial, so your game server will compile as easily on the Linux platform as it will on the Windows platform. If you are interested, you can find out more information (and buy it J) at the following Amazon.com link: http://www.amazon.com/exec/obidos/tg/detail/-/1556228686 On a final note, I hope you enjoy reading this tutorial and find the information within it useful. If you have any questions or problems with anything in the tutorial, do not hesitate to e-mail either myself or Glenn and we will try to help you as best we can!

Andrew Mulholland
andrew@bugmenot.com

JaVa
   
Comments