Introduction

This book will teach you how to create games with the awesome Java language. Previous editions emphasized the casual game market with chapter projects designed to run in AppletViewer or in a web browser. While still relevant, the code in this new edition is a bit simpler, using a JFrame more often instead of an applet, so that examples can be run as a Java application. Some chapters still feature applet projects, while many others are now JFrame-based applications. The final Galactic War project in Part III is still an applet, since a focus of the book is an emphasis on the casual game market. Game programming is a challenging subject, and it can be difficult to figure out how to get started. This book takes away some of the mystery of game programming by explaining how to create a game in Java. I assume that you have a little Java programming experience, but if you have never used Java before you should be able to keep up. Chapter 2 provides a quick summary of the language.

This book takes the approach that we can have fun while learning how to program games. Typing in long source code listings out of a book is not fun, so I don’t ask you to do that very much. Instead, you will learn to write short programs that demonstrate the major topics, and over time you will get the hang of it. There is no memorization required here, as I’m a firm believer that repetition and practice is the best way to learn, not theory and memorization.

Definition

Java is a programming language, invented by Sun Microsystems, now owned by Oracle, and largely maintained as a loosely constrained community project. The primary design goal of Java was to build runtime binary files that will run on any computer system in the world without being recompiled. This “compile one, run many” philosophy works! The Java compiler creates a bytecode binary file containing virtual machine instructions that the JRE (Java Runtime Environment) can execute on any computer system on which it is installed.

You will learn how to write a simple Java program in the first chapter. From there, you will learn the details of how to write games that will run in a web browser or as an application. We cover source code at a pace that will not leave you behind. By the end of this book, you will have learned to create a complete game called Galactic War, and will be able to deploy it to your website in a Java Archive (JAR) file. I’m not talking about some half-baked simulation posing as a game. I’m talking about a high-quality game, suitable for publishing in the casual game market. There are thousands of casual gamers who are paying to download games of this type from the many casual game sites on the web today—such as Real Arcade (now www.GameHouse.com). By learning how to create a casual game, you may even be preparing for a career in the game industry, developing games for Microsoft Xbox Live Arcade and other commercial endeavors.

Definition

Web-based games are video games that are installed on a website and run in a web browser, so that the end-users do not need to install the game. Some games are able to store high-score lists and player data on the web server. The most popular type of web game is a “Casual Game” such as Farmville on Facebook.

While we’re on the subject of casual games, you can even program your own Xbox 360 games, distributed on Xbox Live Arcade, using Microsoft’s free XNA Game Studio Express software. Although this subject is beyond the scope of this book, I bring it up because Microsoft’s C# language is unabashedly similar to Java. I have another book on this subject titled XNA Game Studio 4.0 for Xbox 360 Developers that you might want to check out.

It all begins here! Are you serious about this subject and willing to learn? As a course developer and instructor of game development, I am scrutinized daily by students who eat, drink, and breathe video games. I cannot create something that stinks or I’ll never hear the end of it! So I am as motivated to teach you cutting-edge game development techniques here as I am in a real classroom setting, by students who are paying a lot more than the retail cost of this book to learn these concepts. I have used this book in several Java courses already, so you are guaranteed high-quality material in these pages that will not be a waste of your time. In other words, this book has already been through the flames of scrutiny twice before, so you are guaranteed a solid read and good, working code.

What Will You Learn in This Book?

This book will teach you the difference between Java applications and applets (which run in a web browser). You will then learn about Java’s graphics classes and begin writing graphics code. You will learn how to get input from the user, and how to play sound effects and music—all within the context of an online game. From there, the sky’s the limit! Figure 1 shows the game you will learn to create in this book. Starting with the basics (and I’m talking about extreme basics here!), you will write a simple 2D game using vector graphics (using lines and filled polygons).

You will learn how to create this game from scratch!

Figure 1. You will learn how to create this game from scratch!

You will then learn new techniques in each chapter, such as how to load a bitmap file and render it on the 2D applet window using Java 2D classes like Graphics2D. You will eventually put the handful of game-related classes together in an event-driven game engine. As you can see from this screenshot, the final game uses some attractive artwork and is chock full of small details! You will learn about simple bitmaps and then sprites before getting into animation. Along the way, you will learn how to use Java’s advanced 2D library to rotate and scale sprites, and I’ll show you some interesting code that moves bullets, power-ups, asteroids, and other game objects on the screen smoothly and realistically. The end result is a professional sprite-based game engine that packs a serious punch! By learning how to create this retail-quality casual game, you will have learned enough to create your own games, suitable for sale in the casual game market (where games are played over the web).

Definition

An applet is a limited type of Java program designed to run in a web browser. Due to security restrictions, an applet is not able to access the file system on a user’s PC like a Java application, which is installed like any other application software.

Since this book is dedicated to teaching the basics of game programming, it will cover a lot of subjects very quickly, so you’ll need to read the chapters of this book sequentially for best results. I use a casual writing style to make the subjects easy to understand and use repetition rather than memorization to nail the points home. You will learn by practice and will not need to struggle with any one subject, because you will use them several times throughout the book. Each chapter builds on the one before. The Galactic War game developed in Part III refers back to previous chapters, so I recommend reading one chapter at a time, in order, to fully understand everything that is going on. I tend to just use code after explaining how it works the first time, and often do not explain something over and over again because the book moves along at a brisk pace. We have a lot to cover in a limited amount of space, so I recommend reading the book from start to finish.

What about the Programming Language?

This book is about game programming, and it assumes that you already know at least some Java. I recommend that you acquire a Java primer to read before delving into this book, or to keep handy for those parts that may confuse you. For starters, you can pick up Java Programming for the Absolute Beginner, by Joseph P. Russell (Cengage Learning). We do not utilize any advanced features of Java SE 6, even though this is the latest version of Java, so if you are new to the language you should be able to make the best of it by following along.

While covering some of the basics over the first three chapters, you’ll have created a complete casual game in Java that runs in a web browser, which will be a milestone as well as a measure of your own skill level at that point. As I mentioned, this book is not a primer on the Java language, but rather, makes use of this very capable, high-level language to create games. You will find the code in this book much easier to understand if you have at least read a primer on the subject. We discuss game programming, not basic Java programming.

All of the projects in this book will compile with the Java SE 6 development kit. While later versions (such as Java SE 7) will compile the code, older versions of Java may complain about classes or methods that are not recognized.

What IDE Should You Use?

You will be able to compile the programs in this book using the javac.exe program, and will run the programs using appletviewer.exe. We use an awesome little editor called TextPad that can tie in to these command-line tools to compile and run your Java code. We also explore and use NetBeans as an alternative development environment (if you’re looking for a more professional experience).

The first version of this book focused on a professional IDE (Integrated Development Environment) called JBuilder to help organize Java game projects. However, much has changed in the five years since the first edition came out. Borland JBuilder Foundation was a free trial version of JBuilder, and we were able to support the 2005 and 2006 versions at the time. But Borland sold its development tools division to Embarcadero, and it is a proprietary tool now for enterprise development. We simply do not need to use commercial software to build Java code—there are some great options in open source software today!

If a revision is meant to update a book, then it may seem odd to step away from using a professional IDE. But that is exactly what many professional Java programmers are doing—utilizing a simple text editor and the Java SE 6 development kit directly. There are many reasons why this is preferable, and the best explanation may be a desire to avoid the adoption of any single IDE since there are no standard project files in the industry, and each one is dramatically different. I recommend using NetBeans or TextPad. NetBeans is absolutely free and fully featured, but it’s kind of large and complex. TextPad is not free, but it is very inexpensive (under $30) and has the great advantage of being lightweight and simple. The main advantage to NetBeans is the programmer pop-up help built into the editor that shows function parameters and class member lists. For those who prefer Eclipse, we do spend some time with that IDE as well.

Avoiding any single IDE allows us to focus more on the Java code and this helps with cross-platform development. The code in this book will compile and run on the following systems:

  • Windows (x86, x64, Itanium)

  • Linux (x86, x64, Itanium)

  • Solaris (SPARC, x86, x64)

Definition

Cross-platform development is the ability to compile and run the same code on many different computer systems. Java code and executable files are supported on many platforms. You do not need to recompile your Java code for every system, as the same .class file will run on all of them!

Due to this extensive list of supported operating systems, it is obvious why we would not want to limit ourselves to a single IDE, but would prefer to support all of these systems. As a consequence, none of the source code in the book resource files include any project files, but merely source code files and assets (image and sound files).

Tip

You will be using Java SE 6 (i.e. “Java Standard Edition 6”), which is the lightweight version of Java best suited for creating standalone and web-based games. Our text editor of choice is TextPad 5.0, which can compile your Java source code files with a macro key.

TextPad is a small, easy-to-use source code editor that recognizes the Java Development Kit and is able to compile your Java code with a simple macro (Ctrl+1). By using TextPad as our “IDE”, you’ll be working directly with the file system on your hard drive rather than a virtual project manager (such as the one in NetBeans). In TextPad, you’ll see the actual files on your drive, and there is no concept of “adding” files to a “project” because you are working with your source files directly. I recommend purchasing a license of TextPad from www.textpad. com. Sure, there are free editors available, but none that I have found with the feature set and stability of TextPad. For example, two freeware products, Programmer’s Notepad and Notepad++, both seem to have stability problems in Windows 7.

Conventions Used in This Book

The following styles are used in this book to highlight portions of text that are important. You will find these highlighted boxes here and there throughout the book.

Note

This is what a note looks like. Notes are additional information related to the text.

Tip

This is what a tip looks like. Tips give you pointers in the current tutorial being covered.

Caution

This is what a caution looks like. Cautions provide you with guidance and what to do or not do in a given situation.

Definition

This is what a definition looks like. Definitions will explain the meaning behind a technical concept or word.

Companion Web Site Downloads

You may download the companion website files from www.courseptr.com/ downloads. Please note that you will be redirected to our Cengage Learning site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.150.219