High score

This is a mini game made with the Kree Game Engine

What you'll need

  • Kree v2.1 or latest

  • Java IDE

What you'll learn

  • How to use UI

  • Storing data using Prefs

  • WASD movement

Start off

  1. Set up your Java Project and add Kree to your build path

  2. Create a package called avoid

Creating a Launcher Class

// Launcher.java
package avoid;
import dev.jabo.kree.Game;
import dev.jabo.kree.Scene;
import dev.jabo.kree.SceneManager;
import dev.jabo.kree.Window;
public class Launcher {
public static Game game = new Game(new Window("High Score", 800, 600));
public static MyScene myScene = new MyScene(game);
public static void main(String[] args) {
SceneManager.setScene(myScene);
game.start();
}
}

Just a basic launcher class. For more explanations, check out https://kree.gitbook.io/documentation/avoid-the-enemy#preparation

Creating Scene

// Launcher.java
package avoid;
import java.awt.*;
import java.awt.color.ColorSpace;
import dev.jabo.kree.*;
import dev.jabo.kree.components.*;
import dev.jabo.kree.ui.Button;
import dev.jabo.kree.ui.Text;
public class MyScene extends Scene {
public MyScene(Game game) {
super(game);
}
@Override
public void Initialize() {
}
@Override
public void Update() {
}
@Override
public void Render(Graphics g) {
}
}

Here, we setup the class and also import everything we need.

Adding variables

// MyScene.java
package avoid;
import java.awt.*;
import java.awt.color.ColorSpace;
import dev.jabo.kree.*;
import dev.jabo.kree.components.*;
import dev.jabo.kree.ui.Button;
import dev.jabo.kree.ui.Text;
public class MyScene extends Scene {
private GameObject player;
private Text txt = new Text(this, "Maximum value of X ever: 0", new Vector2(50,50), 200);
private float max = 0;
public MyScene(Game game) {
super(game);
}
@Override
public void Initialize() {
}
@Override
public void Update() {
}
@Override
public void Render(Graphics g) {
}
}

Variables we need:

  • player(GameObject)

  • txt (Text, to display our high score)

  • max (float, our high score)

Initializing the player and adding WASD movement

// MyScene.java
package avoid;
import java.awt.*;
import java.awt.color.ColorSpace;
import dev.jabo.kree.*;
import dev.jabo.kree.components.*;
import dev.jabo.kree.ui.Button;
import dev.jabo.kree.ui.Text;
public class MyScene extends Scene {
private GameObject player;
private Text txt = new Text(this,"Maximum value of X ever: 0", new Vector2(50,50), 200);
private float max=0;
public MyScene(Game game) {
super(game);
}
@Override
public void Initialize() {
// Player
player = new GameObject(this, "Player");
player.getTransform().setPosition(new Vector2(game.getWindow().getWindowWidth() / 2, game.getWindow().getWindowHeight() / 2));
player.getTransform().setScale(new Vector2(32, 32));
player.addComponent(new MeshRenderer());
player.addComponent(new PlayerMovement(1f, PlayerMovement.WASD));
player.addComponent(new RigidBody());
((RigidBody)player.getComponent("RigidBody")).gravity = false;
player.addComponent(new Prefs());
}
@Override
public void Update() {
}
@Override
public void Render(Graphics g) {
}
}

Our player is a GameObject. it spams in the middle of the screen.

Components used:

  • MeshRenderer

  • PlayerMovement

  • Prefs (if you're just building a small game it's okay to use Prefs on a normal GameObject, but if you're build a bigger game you should use a manager GameObject to handle Prefs)

Implementing Prefs

// MyScene.java
package avoid;
import java.awt.*;
import java.awt.color.ColorSpace;
import dev.jabo.kree.*;
import dev.jabo.kree.components.*;
import dev.jabo.kree.ui.Button;
import dev.jabo.kree.ui.Text;
public class MyScene extends Scene {
private GameObject player;
private Text txt = new Text(this, "Maximum value of X ever: 0", new Vector2(50,50), 200);
private float max = 0;
public MyScene(Game game) {
super(game);
}
@Override
public void Initialize() {
// Player
player = new GameObject(this, "Player");
player.getTransform().setPosition(new Vector2(game.getWindow().getWindowWidth() / 2, game.getWindow().getWindowHeight() / 2));
player.getTransform().setScale(new Vector2(32, 32));
player.addComponent(new MeshRenderer());
player.addComponent(new PlayerMovement(1f, PlayerMovement.WASD));
player.addComponent(new RigidBody());
((RigidBody)player.getComponent("RigidBody")).gravity = false;
player.addComponent(new Prefs());
max = ((Prefs)player.getComponent("Prefs")).getFloat("max");
}
@Override
public void Update() {
if(player.getTransform().getPosition().getX() > max) {
max = player.getTransform().getPosition().getX();
((Prefs)player.getComponent("Prefs")).storeFloat("max", max);
}
}
@Override
public void Render(Graphics g) {
}
}

With getFloat(String name) you get a float variable, already stored.

With storeFloat(String name, float value) you can store a variable.

Prefs isn't secure! Data is stored in data.txt file

Updating the text with the high score

// MyScene.java
package avoid;
import java.awt.*;
import java.awt.color.ColorSpace;
import dev.jabo.kree.*;
import dev.jabo.kree.components.*;
import dev.jabo.kree.ui.Button;
import dev.jabo.kree.ui.Text;
public class MyScene extends Scene {
private GameObject player;
private Text txt = new Text(this, "Maximum value of X ever: 0", new Vector2(50,50), 200);
private float max = 0;
public MyScene(Game game) {
super(game);
}
@Override
public void Initialize() {
// Player
player = new GameObject(this, "Player");
player.getTransform().setPosition(new Vector2(game.getWindow().getWindowWidth() / 2, game.getWindow().getWindowHeight() / 2));
player.getTransform().setScale(new Vector2(32, 32));
player.addComponent(new MeshRenderer());
player.addComponent(new PlayerMovement(1f, PlayerMovement.WASD));
player.addComponent(new RigidBody());
((RigidBody)player.getComponent("RigidBody")).gravity = false;
player.addComponent(new Prefs());
max = ((Prefs)player.getComponent("Prefs")).getFloat("max");
}
@Override
public void Update() {
txt.setText("Maximum value of X ever: "+ max);
if(player.getTransform().getPosition().getX() > max) {
max = player.getTransform().getPosition().getX();
((Prefs)player.getComponent("Prefs")).storeFloat("max", max);
}
}
@Override
public void Render(Graphics g) {
}
}

With setText(String) you can set the text a of Text Component.

What to do next?

  • This very basic tutorial showed you some amazing things. Now you can try to implement this in you own Kree game!

Congratulations on completing this tutorial!

Need help? Feel free to go through the documentation provided in this website join us on Discord to get a hand https://discord.com/invite/m2hYa6F