Game Development Reference
In-Depth Information
Figure 3-5. Game sprites in the drawables folder
Creating the Star Field
Another neat trick is to create a random star field in the background (see Figure 3-6). We can do this by
using arrays of X and Y coordinates for each star. But we must also set style and colors using Paint
objects. Listing 3-7 demonstrates how to create the random star field and styles for SpaceBlaster .
Listing 3-7. A Random Star Field
/**
* create the star field in the background
*/
public void initStars() {
starsX = new int[numStars];
starsY = new int[numStars];
starsC = new Paint[numStars];
for (int i = 0; i < numStars; i++) {
starsX[i] = (int) ((Math.random() * xSize - 1) + 1);
starsY[i] = (int) ((Math.random() * ySize - 1) + 1);
starsC[i] = newColor();
}
}
public Paint newColor() {
Search WWH ::




Custom Search