Android Open Source - ZombieBird Game Renderer






From Project

Back to project page ZombieBird.

License

The source code is released under:

Apache License

If you think the Android project ZombieBird listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kilobolt.GameWorld;
/*from   w ww  .  jav a2  s.  c o m*/
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.kilobolt.GameObjects.Bird;
import com.kilobolt.GameObjects.Grass;
import com.kilobolt.GameObjects.Pipe;
import com.kilobolt.GameObjects.ScrollHandler;
import com.kilobolt.ZBHelpers.AssetLoader;

public class GameRenderer {

  private GameWorld myWorld;
  private OrthographicCamera cam;
  private ShapeRenderer shapeRenderer;

  private SpriteBatch batcher;

  private int midPointY;
  private int gameHeight;

  // Game Objects
  private Bird bird;
  private ScrollHandler scroller;
  private Grass frontGrass, backGrass;
  private Pipe pipe1, pipe2, pipe3;

  // Game Assets
  private TextureRegion bg, grass;
  private Animation birdAnimation;
  private TextureRegion birdMid, birdDown, birdUp;
  private TextureRegion skullUp, skullDown, bar;

  public GameRenderer(GameWorld world, int gameHeight, int midPointY) {
    myWorld = world;
    this.gameHeight = gameHeight;
    this.midPointY = midPointY;

    cam = new OrthographicCamera();
    cam.setToOrtho(true, 136, gameHeight); // this will be scaled to the resolution in Main.java in ZombieBird-desktop.

    batcher = new SpriteBatch();
    // attach batcher to camera
    batcher.setProjectionMatrix(cam.combined);

    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(cam.combined);

    // Call helper methods to initialize asset and object instance variables
    initGameObjects();
    initAssets();
  }

  public void render(float runTime) {
    System.out.println("GameRenderer - render()");

    // Fill the screen with black to prevent flickering
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // Begin ShapeRenderer
    shapeRenderer.begin(ShapeType.Filled);

    // Draw Background color
    shapeRenderer.setColor(55 / 255.0f, 80 / 255.0f, 100 / 255.0f, 1);
    shapeRenderer.rect(0, 0, 136, midPointY + 66);

    // Draw grass
    shapeRenderer.setColor(111 / 255.0f, 186 / 255.0f, 45 / 255.0f, 1);
    shapeRenderer.rect(0, midPointY + 66, 136, 11);

    // Draw Dirt
    shapeRenderer.setColor(147 / 255.0f, 80 / 255.0f, 27 / 255.0f, 1);
    shapeRenderer.rect(0, midPointY + 77, 136, 52);

    // End ShapeRenderer
    shapeRenderer.end();

    // Begin SpriteBatch
    batcher.begin();

    // Disable transparency
    // This is good for performance when drawing images that do not require transparency
    batcher.disableBlending();
    batcher.draw(bg, 0, midPointY + 23, 136, 43);

        // 1. Draw Grass
        drawGrass();
        
        // 2. Draw Pipes
        drawPipes();
        batcher.enableBlending();
        
        // 3. Draw Skulls (requires transparency)
        drawSkulls();

    // Draw bird at desired coordinates. Retrieve Animation object from AssetLoader
    // Pass in the runTime variable to get current frame
    if (bird.shouldntFlap()) {
      batcher.draw(birdMid, bird.getX(), bird.getY(), bird.getWidth() / 2.0f, bird.getHeight() / 2.0f, bird.getWidth(), bird.getHeight(), 1, 1, bird.getRotation());
    } else {
      batcher.draw(birdAnimation.getKeyFrame(runTime), bird.getX(), bird.getY(), bird.getWidth() / 2.0f, bird.getHeight() / 2.0f, bird.getWidth(), bird.getHeight(), 1, 1, bird.getRotation());
    }

    // End SpriteBatch
    batcher.end();
    
    // testing the boundingCircle to ensure it is lined up
    shapeRenderer.begin(ShapeType.Filled);
    shapeRenderer.setColor(Color.RED);
    shapeRenderer.circle(bird.getBoundingCircle().x, bird.getBoundingCircle().y, bird.getBoundingCircle().radius);
    
    // testing the rectangles for the pipes and skulls
        // Bar up for pipes 1 2 and 3
        shapeRenderer.rect(pipe1.getBarUp().x, pipe1.getBarUp().y,
                pipe1.getBarUp().width, pipe1.getBarUp().height);
        shapeRenderer.rect(pipe2.getBarUp().x, pipe2.getBarUp().y,
                pipe2.getBarUp().width, pipe2.getBarUp().height);
        shapeRenderer.rect(pipe3.getBarUp().x, pipe3.getBarUp().y,
                pipe3.getBarUp().width, pipe3.getBarUp().height);

        // Bar down for pipes 1 2 and 3
        shapeRenderer.rect(pipe1.getBarDown().x, pipe1.getBarDown().y,
                pipe1.getBarDown().width, pipe1.getBarDown().height);
        shapeRenderer.rect(pipe2.getBarDown().x, pipe2.getBarDown().y,
                pipe2.getBarDown().width, pipe2.getBarDown().height);
        shapeRenderer.rect(pipe3.getBarDown().x, pipe3.getBarDown().y,
                pipe3.getBarDown().width, pipe3.getBarDown().height);

        // Skull up for Pipes 1 2 and 3
        shapeRenderer.rect(pipe1.getSkullUp().x, pipe1.getSkullUp().y,
                pipe1.getSkullUp().width, pipe1.getSkullUp().height);
        shapeRenderer.rect(pipe2.getSkullUp().x, pipe2.getSkullUp().y,
                pipe2.getSkullUp().width, pipe2.getSkullUp().height);
        shapeRenderer.rect(pipe3.getSkullUp().x, pipe3.getSkullUp().y,
                pipe3.getSkullUp().width, pipe3.getSkullUp().height);

        // Skull down for Pipes 1 2 and 3
        shapeRenderer.rect(pipe1.getSkullDown().x, pipe1.getSkullDown().y,
                pipe1.getSkullDown().width, pipe1.getSkullDown().height);
        shapeRenderer.rect(pipe2.getSkullDown().x, pipe2.getSkullDown().y,
                pipe2.getSkullDown().width, pipe2.getSkullDown().height);
        shapeRenderer.rect(pipe3.getSkullDown().x, pipe3.getSkullDown().y,
                pipe3.getSkullDown().width, pipe3.getSkullDown().height);
    
    shapeRenderer.end();
  }

  private void initGameObjects() {
    bird = myWorld.getBird();
    scroller = myWorld.getScroller();
    frontGrass = scroller.getFrontGrass();
    backGrass = scroller.getBackGrass();
    pipe1 = scroller.getPipe1();
    pipe2 = scroller.getPipe2();
    pipe3 = scroller.getPipe3();
  }

  private void initAssets() {
    bg = AssetLoader.bg;
    grass = AssetLoader.grass;
    birdAnimation = AssetLoader.birdAnimation;
    birdMid = AssetLoader.bird;
    birdDown = AssetLoader.birdDown;
    birdUp = AssetLoader.birdUp;
    skullUp = AssetLoader.skullUp;
    skullDown = AssetLoader.skullDown;
    bar = AssetLoader.bar;
  }

  private void drawGrass() {
    // Draw the grass
    batcher.draw(grass, frontGrass.getX(), frontGrass.getY(), frontGrass.getWidth(), frontGrass.getHeight());
    batcher.draw(grass, backGrass.getX(), backGrass.getY(), backGrass.getWidth(), backGrass.getHeight());
  }

  private void drawSkulls() {
    // Temporary code! Sorry about the mess :)
    // We will fix this when we finish the Pipe class.

    batcher.draw(skullUp, pipe1.getX() - 1, pipe1.getY() + pipe1.getHeight() - 14, 24, 14);
    batcher.draw(skullDown, pipe1.getX() - 1, pipe1.getY() + pipe1.getHeight() + 45, 24, 14);

    batcher.draw(skullUp, pipe2.getX() - 1, pipe2.getY() + pipe2.getHeight() - 14, 24, 14);
    batcher.draw(skullDown, pipe2.getX() - 1, pipe2.getY() + pipe2.getHeight() + 45, 24, 14);

    batcher.draw(skullUp, pipe3.getX() - 1, pipe3.getY() + pipe3.getHeight() - 14, 24, 14);
    batcher.draw(skullDown, pipe3.getX() - 1, pipe3.getY() + pipe3.getHeight() + 45, 24, 14);
  }

  private void drawPipes() {
    // Temporary code! Sorry about the mess :)
    // We will fix this when we finish the Pipe class.
    batcher.draw(bar, pipe1.getX(), pipe1.getY(), pipe1.getWidth(), pipe1.getHeight());
    batcher.draw(bar, pipe1.getX(), pipe1.getY() + pipe1.getHeight() + 45, pipe1.getWidth(), midPointY + 66 - (pipe1.getHeight() + 45));

    batcher.draw(bar, pipe2.getX(), pipe2.getY(), pipe2.getWidth(), pipe2.getHeight());
    batcher.draw(bar, pipe2.getX(), pipe2.getY() + pipe2.getHeight() + 45, pipe2.getWidth(), midPointY + 66 - (pipe2.getHeight() + 45));

    batcher.draw(bar, pipe3.getX(), pipe3.getY(), pipe3.getWidth(), pipe3.getHeight());
    batcher.draw(bar, pipe3.getX(), pipe3.getY() + pipe3.getHeight() + 45, pipe3.getWidth(), midPointY + 66 - (pipe3.getHeight() + 45));
  }
}




Java Source Code List

com.kilobolt.GameObjects.Bird.java
com.kilobolt.GameObjects.Grass.java
com.kilobolt.GameObjects.Pipe.java
com.kilobolt.GameObjects.ScrollHandler.java
com.kilobolt.GameObjects.Scrollable.java
com.kilobolt.GameWorld.GameRenderer.java
com.kilobolt.GameWorld.GameWorld.java
com.kilobolt.Screens.GameScreen.java
com.kilobolt.ZBHelpers.AssetLoader.java
com.kilobolt.ZBHelpers.InputHandler.java
com.kilobolt.ZombieBird.MainActivity.java
com.kilobolt.ZombieBird.Main.java
com.kilobolt.ZombieBird.RobovmLauncher.java
com.kilobolt.ZombieBird.ZBGame.java
com.kilobolt.ZombieBird.client.GwtLauncher.java