Welcome


Introduction

Kree Java is a simple and lightweight 2D Java Game library yet has many features that can make a decent 2D game. Kree's components is designed simple so beginners can follow along and make awesome games with it.

Installation

Installation Tutorial available on YouTube: Kree Java | Installation

- Make sure you installed the latest release at Kree Releases - This installation only provides details about Eclipse Installation

Eclipse

  • Create a new Plain Java Project
    File > New > Java Project
  • Go to your project's build path
    Project > Properties > Java Build Path > Libraries > Add External JARS > Locate Kree.jar on your directory
  • Creating an Asset Folders
    Project > Properties > Java Build Path > Libraries > Add Class Folder > Create New Folder > (Assets) > Tick the Assets Folder > OK

Kree Components


Game

  • This components is main component of Kree this is where all operations start and it takes one parameter Window
// Usage
new Game(Window window);

Methods:

// Methods
start(); // Starts the loop

Window

  • This component provides a window for us to display the game's graphics
// Usage
new Window(String windowTitle, int windowWidth, windowHeight);
// Methods
getWindowWidth(); // Returns window width

getWindowHeight(); // Returns window height

getWindowTitle(); // Returns window title

SceneManager (Static)

How to create a Scene?
- This components handles the scenes in our game

// Methods
SceneManager.setScene(Scene scene);

Input (Static)

  • This component handles all the User Input
// Static Properties
Input.mouseClicked;

Input.leftMouseDown;

Input.middleMouseDown;

Input.rightMouseDown;
// Static Methods
Input.getMouse(); // Returns the position of mouse in Vector2

Input.isKeyPressed(int keyCode); // Returns if the key is pressed

Input.mouseIn(GameObject gameObject); // Returns true if the mouse is in GameObject // NOTE: Only works if the GameObject has BoxCollider component

Debug (Static)

  • This component is used for debugging
// Methods
Debug.log(/* Output here */)

Sprite

  • This component is used for displaying images in scenes
// Usage
new Sprite(String path);
// Methods
BufferedImage image; // Returns the image

GameObject

// Usage
new GameObject(Scene parentScene, String objectName);
// Properties
int id; // Returns the unique identifier of this GameObject

String name; // Returns the name of the GameObject

Transform transform; // See more at Transform documentation
// Methods
addComponent(Component component); // Adds a component to the GameObject

setPosition(Vector2 position); // Sets the position of the GameObject

setScale(Vector2 scale); // Sets the scale of the GameObject

destroy(); // Deletes the GameObject from the scene

Transform

  • This component handles the scale and position of GameObject
// Usage
new Transform();
// Properties
Vector2 position; // Position of in scene

Vector2 scale; // Dimension in scene

int rotation; // NOTE: In Degrees
// Methods
translate(Vector2 position); // Moves the position by given Vector2

Vector2

  • This component contains two points x and y
// Usage
new Vector2(int x, int y);
// Static Properties
Vector2.left; // new Vector2(-1, 0)

Vector2.right; // new Vector2(1, 0)

Vector2.up; // new Vector2(0, -1)

Vector2.down; // new Vector2(0, 1)
// Properties
int x; // Returns integer x

int y; // Returns integer y
// Static Methods
Vector2.Distance(Vector2 v1, Vector2 v2); // Returns distance between two vectors
// Methods
add(Vector2 vector); // Adds the current vector to another

subtract(Vector2 vector); // Subtracts the current vector to another

multiply(Vector2 vector); // Multiplies the current vector to another

divide(Vector2 vector); // Divides the current vector to another

moveTowards(Vector2 position, float speed); // Move the Vector2 towards another Vector2

SpriteSheet

  • Works like Sprite() but it is composed of it
// Usage
new SpriteSheet(Sprite sheet, int frameWidth, int frameHeight);
// frameWidth - How wide each frame is
// frameHeight - How tall each frame is

Animation

  • This component is used to make animated sprites in our scenes
// Usage
new Animation(String animationName, SpriteSheet frames, int speedInMillisecond);

Audio

  • This component is used to play sound/ music on our game
// Usage
new Audio(String pathToAudio);
// Methods
play(); // Play one time

play(boolean loop); // Loop

stop(); // Stop the audio

isPlaying(); // Returns true if the audio is still playing

GameObject Components

How to add GameObject Components to a GameObject?

  • Adding Components to a GameObject is straightforward, See the code below

public class YourScene extends Scene {

    private GameObject player = new GameObject(this, "Player");

    ...

    public void Initialize() {
        player.addComponent(new ComponentNameHere());
        // Scroll down to see different components to use
    }

    ...

}

Can I create my own GameObject Component?

  • GameObjects are versatile we've prepared a quick guide on making your components here

Can I disable a component?

ComponentNameHere.disable = true;

MeshRenderer

  • Renders a rectangular shape that is dependent to the GameObject's scale and position
new MeshRenderer();
// Properties
Color color; // returns the color of the mesh
// Methods
setColor(Color color); // Sets the color of Mesh

SpriteRenderer

  • Renders a sprite that is dependent to the GameObject's scale and position
new SpriteRenderer(String path);

Animator

  • Renders a sprite that is dependent to the GameObject's scale and position
new Animator(Animation[] listOfAnimations);
// Methods
setActiveAnimation(String name); // Set the active animation by name

BoxCollider

  • This component is used for collision detection
new BoxCollider();
// Properties
boolean trigger; // If true it will not be affected by RigidBody
// Methods
collidingWith(BoxCollider collider); // Returns true if a vector is colliding the BoxCollider

RigidBody

  • This component is used for GameObjects to be affected by environment Physics
new RigidBody();
// Properties
int mass; // How heavy the object is
// Methods
addHorizontalForce(int xForce); // Apply force horizontally

ParticleSystem

  • This component is used to make particle system
new ParticleSystem(GameObject parent, int particleType, int minimumSize, int maximumSize, int minimumVelocity, int maximumVelocity, int particleCount, int particleRange, Color color);
// Static Properties
int ParticleSystem.TYPE_CIRCULAR; // Used in the seconf parameter of ParticleSystem
// Methods
isAlive(); // Returns true if the particle system is still emitting particles

LookAtObject

  • This component is used to make particle system
new LookAtObject();
// Properties
Vector2 target; // Returns the target position

GameObject objectTarget; // Returns the GameObject target
// Methods
setTarget(Vector2 target); 

setTarget(GameObject target);

Kree User Interface Beta


TextInput

  • TextInput lets user to write texts in your game
// Usage
new TextInput(Scene parentScene, Vector2 position, Vector2 scale);
// Methods
setFont(Font font); // Sets the font of the text in the input box

setColor(Color backgroundColor, Color foregroundColor, Color selectedColor); // Sets different colors depending on the state

setPosition(Vector2 position); // Position the UI on the scene

setScale(Vector2 scale); // Scale of the UI

limitCharacter(int limit); // Limit how many character

getValue(); // Returns the value of the TextInput (String)

getPosition(); // Return Vector2 

getScale(); // Return Vector2 

Text

  • Text lets you display text in user's screen
// Usage
new Text(Scene parentScene, String value, Vector2 position, int width);
// Methods
setColor(Color color); // Sets the color of the text

setFont(Font font); // Sets the font of the text

setText(String text); // Sets the value of the text

getText(); // Returns the value of the text
// Debugging methods
showBorder(); // Shows the boundary of the text;

hideBorder(); // Hides the boundary of the text;

ProgressBar

  • This UI lets you render a progress bar for health, xp, tutorials, and more!
// Usage
new ProgressBar(Scene parentScene);

new ProgressBar(Scene parentScene, Vector2 position);

new ProgressBar(Scene parentScene, Vector2 position, Vector2 scale);
// Methods
add(float toAdd); // Add to current value

subtract(float toSubtract); // Subtract to current value

setValue(float value); // Sets value

setMaxValue(float maxValue); // Sets max value

setColor(Color backgroundColor, Color foregroundColor); // Sets color of background and foreground

setPosition(Vector2 position); // Sets position

setScale(Vector2 scale); // Sets size

setPadding(int padding); // Sets the padding between background and foreground

getValue(); // Returns current value

getMaxValue(); // Returns max value

getPosition(); // Returns Vector2

getScale(); // Returns Vector2

Button

  • This UI adds interaction to your game
// Usage 
new Button(Scene parentScene, String text, Vector2 position, Vector2 scale);
// Properties
Transform transform; // Position, Scale, and Rotation.
// Methods
onClick(); // Returns true if the button has been clicked

onHover(); // Returns true if the mouse is hovering the button

Snippets


Launcher

public class Launcher {

    public static void main(String[] args) {

        Game game = new Game(new Window("My Game", 800, 600));

        GameScene gameScene = new GameScene(game);

        SceneManager.setScene(gameScene);

        // start must always be in the end of the main method
        game.start();

    }

}

Scene Snippet

Tutorial now available on YouTube: Kree Java - Creating Scenes

- Wondering how to create a scene?

public class SceneNameHere extends Scene {

    public Scene(Game game) {
        super(game);
    }

    @Override
    public void Initialize() {

    }

    @Override
    public void Update() {

    }

    @Override
    public void Render(Graphics g) {

    }

}

Player Key Control


public class SceneNameHere extends Scene {

    private GameObject player = new GameObject(this, "Player");

    public Scene(Game game) {
        super(game);
    }

    @Override
    public void Initialize() {
        player.setPosition(new Vector2(128, 128));
        player.setScale(new Vector2(16, 16));
    }

    @Override
    public void Update() {
        if(Input.isKeyPressed(37)) // 37 is KeyCode of LEFT KEY
            player.transform.translate(Vector2.left);

        if(Input.isKeyPressed(38)) // 38 is KeyCode of UP KEY
            player.transform.translate(Vector2.up);

        if(Input.isKeyPressed(39)) // 39 is KeyCode of RIGHT KEY
            player.transform.translate(Vector2.right);

        if(Input.isKeyPressed(40)) // 40 is KeyCode of DOWn KEY
            player.transform.translate(Vector2.down);
    }

    @Override
    public void Render(Graphics g) {

    }

}


Player Follow Mouse On Click

public class SceneNameHere extends Scene {

    private GameObject player = new GameObject(this, "Player");
    private Vector2 target = new Vector2(0, 0);

    public Scene(Game game) {
        super(game);
    }

    @Override
    public void Initialize() {
        player.setPosition(new Vector2(128, 128));
        player.setScale(new Vector2(16, 16));
    }

    @Override
    public void Update() {
        if(Input.mouseClicked)
            target = Input.getMouse();
        player.transform.position.moveTowards(target);
    }

    @Override
    public void Render(Graphics g) {

    }

}

Animating Player

public class SceneNameHere extends Scene {

    private GameObject player = new GameObject(this, "Player");

    private Animation[] playerAnimations = new Animation[] {
        new Animation("Idle", new SpriteSheet(new Sprite("player_idle.png"), 16, 16), 500),
        new Animation("Walk", new SpriteSheet(new Sprite("player_walk.png"), 16, 16), 500),
    };

    private Animator playerAnim = new Animator(playerAnimations);

    public Scene(Game game) {
        super(game);
    }

    @Override
    public void Initialize() {
        player.setPosition(new Vector2(128, 128));
        player.setScale(new Vector2(16, 16));

        // Change Active Animation
        playerAnim.setActiveAnimation("Idle");
        player.addComponent(playerAnim);
    }

    @Override
    public void Update() {

    }

    @Override
    public void Render(Graphics g) {

    }

}

Implementing ParticleSystem

public class SceneNameHere extends Scene {

    private GameObject particleSystem = new GameObject(this, "My Particle");

    public Scene(Game game) {
        super(game);
    }

    @Override
    public void Initialize() {
        particleSystem.setPosition(new Vector2(32, 32));
        particleSystem.addComponent(new ParticleSystem(particleSystem, ParticleSystem.TYPE_CIRCULAR, 3, 16, 1, 2, 100, 32, Color.ORANGE));
    }

    @Override
    public void Update() {

    }

    @Override
    public void Render(Graphics g) {

    }

}

Playing Audio on Click

public class SceneNameHere extends Scene {

    private Audio audioOnClick = new Audio("Assets/myAudio.wav");

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

    }

    @Override
    public void Initialize() {



    }

    @Override
    public void Update() {

        if(Input.mouseClicked) {
            if(!audioOnClick.isPlaying()) {
                audioOnClick.play();
            }
        }

    }

    @Override
    public void Render(Graphics g) {


    }


}

Creating Player Movement Component

PlayerComponent.java

public class PlayerMovement extends Component {

    public float movementSpeed = 0.5f;

    public PlayerMovement(float movementSpeed) {
        this.name = "Player Movement";
        this.movementSpeed = movementSpeed;
    }

    @Override
    public void Update() {

        if(gameObject == null)
            return;

        // Left key
        if(Input.isKeyPressed(37)) {
            gameObject.transform.translate(Vector2.left);
        }
        // Right key
        if(Input.isKeyPressed(39)) {
            gameObject.transform.translate(Vector2.right);
        }
        // Up key
        if(Input.isKeyPressed(38)) {
            gameObject.transform.translate(Vector2.up);
        }
        // Down key
        if(Input.isKeyPressed(40)) {
            gameObject.transform.translate(Vector2.down);
        }

    }

    @Override
    public void Render(Graphics g) {

    }


}

SceneNameHere.java

public class SceneNameHere extends Scene {

    private GameObject player = new GameObject(this, "Player");

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

    }

    @Override
    public void Initialize() {

        player.setScale(new Vector2(32, 32));
        player.addComponent(new PlayerMovement(0.5f));
        player.addComponent(new MeshRenderer());

    }

    @Override
    public void Update() {

    }

    @Override
    public void Render(Graphics g) {


    }


}