Android Open Source - BobEngine Game Room






From Project

Back to project page BobEngine.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project BobEngine 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.bobbyloujo.jumpybug;
/*w  w  w.j a va 2  s.c om*/
import java.util.Random;

import bobby.engine.bobengine.BobView;
import bobby.engine.bobengine.NumberDisplay;
import bobby.engine.bobengine.Room;

/**
 * Created by Ben on 1/6/2015.
 */
public class GameRoom extends Room {

    // Constants
    private final int SPACE = 100;      // The space between each flower
    private final int GAP = 300;        // The gap between top and bottom flowers
    private final int NUM_FLOW = 3;     // Number of flowers

    // Variables
    private int distance;               // Distance travelled by the bug
    private int currentFlower;          // Flower that is currently being sent across the screen
    private int score;                  // Player score. Number of flowers passed.

    // Objects
    private Bug bug;                    // The bug
    private Flower[] topFlowers;        // The upside down top flowers
    private Flower[] bottomFlowers;     // The right side up bottom flowers
    private Background bg1;             // Background panel 1
    private Background bg2;             // Background panel 2
    private NumberDisplay scoreDis;     // Score display

    // Other
    private Random rand;                // Random number generator.

    public GameRoom(BobView container) {
        super(container);

        rand = new Random();

        // Initialize game objects
        bug = new Bug(nextInstance(), this);
        bg1 = new Background(nextInstance(), this);
        bg2 = new Background(nextInstance(), this);
        scoreDis = new NumberDisplay(nextInstance(), this); // NumberDisplay is a built in GameObject for displaying numbers

        topFlowers = new Flower[NUM_FLOW];
        bottomFlowers = new Flower[NUM_FLOW];
        for (int i = 0; i < NUM_FLOW; i++) {
            topFlowers[i] = new Flower(nextInstance(), this);
            bottomFlowers[i] = new Flower(nextInstance(), this);
        }

        // Add the objects to the room.
        addObject(bug);
        addObject(bg1);
        addObject(bg2);
        addObject(scoreDis);

        for (int i = 0; i < NUM_FLOW; i++) {
            addObject(topFlowers[i]);
            addObject(bottomFlowers[i]);
        }
    }

    /**
     * Set/reset the room.
     */
    public void set() {
        // Variables
        distance = 0;
        currentFlower = 0;
        score = 0;

        // Objects
        bug.set();
        bg1.set(getWidth() / 2, 4);
        bg2.set(getWidth() * 3 / 2, 4);

        scoreDis.x = getWidth() / 2;
        scoreDis.y = getHeight() * 3 / 4;
        scoreDis.width = getWidth() / 5;
        scoreDis.height = scoreDis.width;
        scoreDis.setNumber(score);

        for (int i = 0; i < NUM_FLOW; i++) {
            topFlowers[i].set(true);
            bottomFlowers[i].set(false);
        }
    }

    public void incrementScore() {
        score++;
    }

    public int getScore() {
        return score;
    }

    /**
     * Step event happens every frame.
     *
     * @param dt - delta time. Will be 1 if the game is running at 60fps. Will be lower if the game is running fast, higher if
     *           the game is running slow.
     */
    @Override
    public void step(double dt) {
        distance++;

        scoreDis.setNumber(score);                                                   // Update score display
        scoreDis.x = getWidth() / 2 + scoreDis.getWidth() / 2;  // Keep it centered

        /**
         * Send a new flower when we've moved the correct distance.
         *
         * getRatioX() can be used to adjust values for screens with different x dimensions. There is
         * also a getRatioY() method for the y dimension.
         */
        if (distance % SPACE * getRatioX() == 0) {

            // Move the next flowers off the right side of the screen
            topFlowers[currentFlower].x = getWidth() + topFlowers[currentFlower].width;
            bottomFlowers[currentFlower].x = getWidth() + bottomFlowers[currentFlower].width;

            // Random y positions
            int offset = rand.nextInt(getHeight() / 4);
            topFlowers[currentFlower].y = getHeight() + offset - GAP * getRatioY();
            bottomFlowers[currentFlower].y = offset;

            // Increment to the next flower.
            currentFlower++;
            if (currentFlower == NUM_FLOW) currentFlower = 0;
        }

        /**
         * Collision detection.
         *
         * After assigning collision boxes to two objects, you can check if they have collided
         * using checkCollision(object1, object2). Here we compare the bug to all the flowers.
         */
        for(int i = 0; i < NUM_FLOW; i++) {
            if (checkCollision(bug, topFlowers[i]) || checkCollision(bug, bottomFlowers[i])) {
                GameView.gameOver.set();
                getView().goToRoom(GameView.gameOver);
            }
        }
    }
}




Java Source Code List

bobby.engine.bobengine.BobActivity.java
bobby.engine.bobengine.BobActivity.java
bobby.engine.bobengine.BobRenderer.java
bobby.engine.bobengine.BobRenderer.java
bobby.engine.bobengine.BobView.java
bobby.engine.bobengine.BobView.java
bobby.engine.bobengine.BuildConfig.java
bobby.engine.bobengine.BuildConfig.java
bobby.engine.bobengine.GameObject.java
bobby.engine.bobengine.GameObject.java
bobby.engine.bobengine.Graphic.java
bobby.engine.bobengine.Graphic.java
bobby.engine.bobengine.GraphicsHelper.java
bobby.engine.bobengine.GraphicsHelper.java
bobby.engine.bobengine.NumberDisplay.java
bobby.engine.bobengine.NumberDisplay.java
bobby.engine.bobengine.Room.java
bobby.engine.bobengine.Room.java
bobby.engine.bobengine.SoundPlayer.java
bobby.engine.bobengine.SoundPlayer.java
bobby.engine.bobengine.SplashActivity.java
bobby.engine.bobengine.SplashActivity.java
bobby.engine.bobengine.Touch.java
bobby.engine.bobengine.Touch.java
bobby.engine.template.AnObject.java
bobby.engine.template.AnObject.java
bobby.engine.template.BuildConfig.java
bobby.engine.template.BuildConfig.java
bobby.engine.template.GameView.java
bobby.engine.template.GameView.java
bobby.engine.template.MainActivity.java
bobby.engine.template.MainActivity.java
bobby.engine.template.StartRoom.java
bobby.engine.template.StartRoom.java
bobby.engine.touchinput.AnObject.java
bobby.engine.touchinput.AnObject.java
bobby.engine.touchinput.GameView.java
bobby.engine.touchinput.GameView.java
bobby.engine.touchinput.MainActivity.java
bobby.engine.touchinput.MainActivity.java
bobby.engine.touchinput.StartRoom.java
bobby.engine.touchinput.StartRoom.java
bobby.example.bobengineexample.Android.java
bobby.example.bobengineexample.BuildConfig.java
bobby.example.bobengineexample.GameView.java
bobby.example.bobengineexample.MainActivity.java
bobby.example.bobengineexample.StartRoom.java
bobby.example.cameraexample.ApplicationTest.java
bobby.example.cameraexample.MainActivity.java
com.bobbyloujo.jumpybug.ApplicationTest.java
com.bobbyloujo.jumpybug.Background.java
com.bobbyloujo.jumpybug.Bug.java
com.bobbyloujo.jumpybug.Flower.java
com.bobbyloujo.jumpybug.GameOver.java
com.bobbyloujo.jumpybug.GameRoom.java
com.bobbyloujo.jumpybug.GameView.java
com.bobbyloujo.jumpybug.MainActivity.java
com.bobbyloujo.jumpybug.StartRoom.java