You are here

Chapter 31: Adding a Game Status display

Now it's about time to start wrapping things together and start handling and displaying current level, time and so on.

Let's start with the basics to get some kind of game status display, by creating a new class: GameStatus.java:

package com.ajomannen.justroids;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class GameStatus {
  private Paint textPaint;
  private int level;
  private int thrusts;
  private int firedMissiles;
  private long levelStartTime;

  public GameStatus() {
   textPaint = new Paint();
   textPaint.setColor(Color.LTGRAY);
   textPaint.setTextSize(25);

   level = 1;
   thrusts = 0;
   firedMissiles = 0;
   levelStartTime = System.nanoTime();
  }

  public int getLevel() {
   return level;
  }

  public void setLevel(int level) {
   this.level = level;
  }

  public void increaseThrusts() {
   thrusts++;
  }

  public void increaseFiredMissiles() {
   firedMissiles++;
  }

  public void draw(Canvas canvas, float screenHeight, float screenWidth) {
   long passedLevelTime = (System.nanoTime() - levelStartTime) / 1000000000L;
 
   textPaint.setTextAlign(Paint.Align.LEFT);
   canvas.drawText("Level: " + level, 1, 19, textPaint);
   canvas.drawText("Thrusts: " + thrusts, 1, screenHeight-2, textPaint);
 
   textPaint.setTextAlign(Paint.Align.RIGHT);
   canvas.drawText("Time: " + passedLevelTime + "s", screenWidth-1, 19, textPaint);
   canvas.drawText("Missiles: " + firedMissiles, screenWidth-1, screenHeight-2, textPaint);
  }

}

In order to use this new GameStatus, we have to modify three other classes; GameEngine, Ship and MissileHandler.

First we introduce the GameStatus, handle "level" through it, draw it and pass it on to Ship and MissileHandler during updates in GameEngine.java:

package com.ajomannen.justroids;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;

public class GameEngine {

  Context context;
  Resources resources;

  public float screenWidth;
  public float screenHeight;
  private Paint blackPaint;

  // private int level;
  private AsteroidHandler asteroidHandler;
  private MissileHandler missileHandler;
  private Ship ship;
  private GameStatus gameStatus;

  public static final int EVENT_NONE = 0;
  public static final int EVENT_LEFT = 1;
  public static final int EVENT_RIGHT = 2;
  public static final int EVENT_THRUST = 3;
  public static final int EVENT_FIRE = 4;
  public int pendingEvent = EVENT_NONE;

  public void Init(Context context) {
   this.context = context;
   resources = context.getResources();

   blackPaint = new Paint();
   blackPaint.setColor(Color.BLACK);
   blackPaint.setStyle(Style.FILL);

   // level = 5;
   gameStatus = new GameStatus();
   asteroidHandler = new AsteroidHandler(resources, gameStatus.getLevel());
   missileHandler = new MissileHandler(resources);
   ship = new Ship(resources, 120, 280, 0.1, 0, 0);

   setSurfaceDimensions(240, 320);

  }

  public void onDestroy() {
   try {
   } catch (Exception e) {
   }
  }

  public void setSurfaceDimensions(int width, int height) {
   screenWidth = width;
   screenHeight = height;
  }

  public void update() {
   asteroidHandler.update(screenWidth, screenHeight);
   ship.update(screenWidth, screenHeight, asteroidHandler.asteroids,
     pendingEvent, gameStatus);
   missileHandler.update(screenWidth, screenHeight, pendingEvent, ship.x,
     ship.y, ship.angle, asteroidHandler.asteroids, gameStatus);
   pendingEvent = EVENT_NONE;
  }

  public void draw(Canvas canvas) {
   canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), blackPaint);
   asteroidHandler.draw(canvas);
   missileHandler.draw(canvas);
   ship.draw(canvas);
   gameStatus.draw(canvas, screenHeight, screenWidth);
  }

}

Then we increase GameStatus' "thrust" counter in Ship.java:

package com.ajomannen.justroids;

import java.util.List;

import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Ship extends GfxObject {
  private Paint paint;

  public Ship(Resources resources, float x, float y, double velocity,
    double direction, int angle) {
   paint = new Paint();
   bitmap = BitmapFactory.decodeResource(resources, R.drawable.ship);
   bitmapHighlighted = BitmapFactory.decodeResource(resources,
     R.drawable.ship_highlighted);
   this.x = x;
   this.y = y;
   setVelocity(velocity);
   setDirection(direction);
   this.angle = angle;
  }

  public void update(float screenWidth, float screenHeight,
    List<Asteroid> asteroids, int event, GameStatus gameStatus) {
   float dX;
   float dY;
   float distance;

   switch (event) {

   case GameEngine.EVENT_NONE: {
    break;
   }

   case GameEngine.EVENT_LEFT: {
    angle -= 10;
    if (Math.abs(angle) >= 360)
     angle = 0;
    break;
   }

   case GameEngine.EVENT_RIGHT: {
    angle += 10;
    if (Math.abs(angle) >= 360)
     angle = 0;
    break;
   }

   case GameEngine.EVENT_THRUST: {
    addVelocity(0.5, angle);
    gameStatus.increaseThrusts();
    break;
   }

   }

   move(screenWidth, screenHeight);
   setCollided(false);
   for (Asteroid asteroid : asteroids) {
    dX = Math.abs(x - asteroid.x);
    dY = Math.abs(y - asteroid.y);
    distance = (float) Math.sqrt(dX * dX + dY * dY);
    if (distance <= (bitmap.getWidth() / 2 + asteroid.bitmap.getWidth() / 2)) {
     collided = true;
    }
   }
  }

  public void draw(Canvas canvas) {
   draw(canvas, x, y, paint);
  }

}

And finally we increase GameStatus' "firedMissiles" counter in MissileHandler.java:

package com.ajomannen.justroids;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class MissileHandler {
  private Bitmap bitmap;
  public ArrayList<Missile> missiles;
  private Paint paint;

  public MissileHandler(Resources resources) {
   paint = new Paint();
   bitmap = BitmapFactory.decodeResource(resources, R.drawable.missile_10);
   missiles = new ArrayList<Missile>();
  }

  public void update(float screenWidth, float screenHeight, int pendingEvent,
    float shipX, float shipY, int shipAngle, List<Asteroid> asteroids,
    GameStatus gameStatus) {
   float dX;
   float dY;
   float distance;

   if (pendingEvent == GameEngine.EVENT_FIRE) {
    Missile newMissile = new Missile(bitmap, shipX, shipY, 6, shipAngle);
    missiles.add(newMissile);
    gameStatus.increaseFiredMissiles();
   }
   Iterator<Missile> missilesIterator = missiles.iterator();
   Missile missile;
   while (missilesIterator.hasNext()) {
    missile = missilesIterator.next();
    if (missile.isLooped() || missile.collided == true) {
     missilesIterator.remove();
    } else {
     missile.move(screenWidth, screenHeight);
     for (Asteroid asteroid : asteroids) {
      dX = Math.abs(missile.x - asteroid.x);
      dY = Math.abs(missile.y - asteroid.y);
      distance = (float) Math.sqrt(dX * dX + dY * dY);
      if (distance <= (missile.bitmap.getWidth() / 2 + asteroid.bitmap
        .getWidth() / 2)) {
       asteroid.collided = true;
       missile.collided = true;
      }
     }
    }
   }
   missiles.trimToSize();
  }

  public void draw(Canvas canvas) {
   for (Missile missile : missiles) {
    missile.draw(canvas, missile.x, missile.y, paint);
   }
  }

}

Now we have a basic GameStatus in place:

 

Next chapter will focus on increasing level when all asteroids are gone and perhaps making the shop mortal again ;-)

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer