Implementing the Screen

Equipped with this new knowledge, we can now easily implement the HighscoreScreen class, as shown in Listing 6-7.

Listing 6-7. HighscoreScreen.java, Showing Us Our Best Achievements So Far package com.badlogic.androidgames.mrnom;

import java.util.List;

import com.badlogic.androidgames.framework.Game; import com.badlogic.androidgames.framework.Graphics; import com.badlogic.androidgames.framework.Screen; import com.badlogic.androidgames.framework.Input.TouchEvent;

public class HighscoreScreen extends Screen { String lines[] = new String[5];

public HighscoreScreen(Game game) { super(game);

lines[i] = "" + (i + l) + ". " + Settings.highscores[i];

As we want to stay friends with the garbage collector, we store the strings of the five high-score lines in a string array member. We construct the strings based on the Settings.highscores array in the constructor.

^Override public void update(float deltaTime) {

List<TouchEvent> touchEvents = game.getInput().getTouchEvents(); game.getInput().getKeyEvents();

int len = touchEvents.size(); for (int i = 0; i < len; i++) {

TouchEvent event = touchEvents.get(i); if (event.type == TouchEvent.TOUCH_UP) { if (event.x < 64 && event.y > 416) { if(Settings.soundEnabled) Assets.click.play(1); game.setScreen(new MainMenuScreen(game)); return;

Next we define the update() method, which is unsurprisingly boring. All we do is check for whether a touch-up event hit the button in the bottom-left corner. If that's the case, we play the click sound and transition back to the MainMenuScreen.

^Override public void present(float deltaTime) { Graphics g = game.getGraphics();

g.drawPixmap(Assets.background, 0, 0); g.drawPixmap(Assets.mainMenu, 64, 20, 0, 42, 196, 42);

g.drawPixmap(Assets.buttons, 0, 416, 64, 64, 64, 64);

The present() method is pretty simple with the help of the mighty drawText() method we just defined. We render the background image first as usual, followed by the "HIGHSCORES" portion of the Assets.mainmenu image. We could have stored that in a separate file, but we reuse it to free up more memory.

Next we loop through the five strings for each high-score line we created in the constructor. We draw each line with the drawText() method. The first line starts at (20,100), the next line is rendered at (20,150), and so on. We just increase the y-coordinate for text rendering by 50 pixels for each line so that we have a nice vertical spacing between the lines. We finish the method off by drawing our button.

public void drawText(Graphics g, String line, int x, int y) { int len = line.length(); for (int i = 0; i < len; i++) {

char character = line.charAt(i);

int srcX = 0; int srcWidth = 0; if (character == '.') { srcX = 200; srcWidth = l0; } else {

g.drawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, 32); x += srcWidth;

^Override public void resume() { }

^Override public void dispose() { }

The remaining methods should be self-explanatory. Let's get to the last missing piece of our Mr. Nom game: the game screen.

0 0

Post a comment