Game Development Reference
In-Depth Information
The last missing bit of information is how to render Mr. Nom's world based on its model. That's
actually pretty easy. Take a look at Figure 6-1 and Figure 6-5 again. Each cell is exactly
32 × 32 pixels in size. The stain images are also 32 × 32 pixels in size, and so are the tail parts
of Mr. Nom. The head images of Mr. Nom for all directions are 42 × 42 pixels, so they don't fit
entirely into a single cell. That's not a problem, though. All we need to do to render Mr. Nom's
world is take each stain and snake part, and multiply its world coordinates by 32 to arrive at the
object's center in pixels on the screen—for example, a stain at (3,2) in world coordinates would
have its center at 96 × 64 on the screen. Based on these centers, all that's left to do is to take the
appropriate asset and render it centered around those coordinates. Let's get coding. Listing 6-12
shows the GameScreen class.
Listing 6-12. GameScreen.java
package com.badlogic.androidgames.mrnom;
import java.util.List;
import android.graphics.Color;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Screen;
public class GameScreen extends Screen {
enum GameState {
Ready ,
Running ,
Paused ,
GameOver
}
GameState state = GameState. Ready ;
World world;
int oldScore = 0;
String score = "0";
We start off by defining an enumeration called GameState that encodes our four states (ready,
running, paused, and game-over). Next, we define a member that holds the current state of the
screen, another member that holds the World instance, and two more members that hold the
currently displayed score in the forms of an integer and a string. The reason we have the last two
members is that we don't want to create new strings constantly from the World.score member
each time we draw the score. Instead, we'll cache the string and only create a new one when the
score changes. That way, we play nice with the garbage collector.
public GameScreen(Game game) {
super (game);
world = new World();
}
Search WWH ::




Custom Search