Game Development Reference
In-Depth Information
C H A P T E R 4
■ ■ ■
Java Games Continued:
Fun with Polygons
In this chapter, we take things to the next level with an arcade classic: Asteroids. The goal of this chapter
is to illustrate a polygon-based game (Asteroids), as opposed to the previous chapter's sprite-based
game (Space Blaster). Using polygons presents a new set of challenges due to the limited polygon
capabilities of Android, as you'll see in the next section. Nevertheless, Asteroids is a relatively simple
game that takes advantage of the high portability of the Java language by reusing code from the standard
Java SE Abstract Windowing Toolkit (AWT). Let's get started.
About the Chapter Layout
This chapter has a different layout than previous chapters, as it doesn't start with a description of the
game implementation but a series of caveats I found when trying to create Asteroids:
Caveats of drawing polygons and rectangles : It turns out that, at the time of this
writing, there is no polygon support in Android. This made creating this game
tougher than the previous chapter's, as I found the need to create three brand new
classes: Rectangle , Polygon , and PolygonSripte . Lucky for me, I was able to take
advantage of the high portability of the Java language and reuse most of the code
of the Rectangle and Polygon classes of Java SE (this is one of the reasons I like the
Java language). PolygonSprite is a class I created from the ground up.
Game architecture and implementation : In this section, the actual game
implementation starts. I chose to do this because the game cannot be described
without the previous foundation classes. Here you will learn about resource
description, game life cycle, processing key press and touch events plus Testing in
the emulator. Let's get started.
Understanding the Caveats of Drawing Polygons in Android
Before starting work on Asteroids, let's take a look at the caveats of drawing polygons in Android. This
section represents the foundation over which the game itself will be built. There is a little problem when
thinking of building a polygon-based game in Android—the API has no polygons. This would be like
designing a house just to find out there is no land to build on. In the previous chapter, we used the
onDraw method of the LinearLayout class to render bitmaps. Asteroids will use the same technique to
Search WWH ::




Custom Search