Here, we setup the class and also import everything we need.
Adding variables
// MyScene.javapackageavoid;importjava.awt.*;importjava.awt.color.ColorSpace;importdev.jabo.kree.*;importdev.jabo.kree.components.*;importdev.jabo.kree.ui.Button;importdev.jabo.kree.ui.Text;publicclassMySceneextendsScene {privateGameObject player;privateText txt =newText(this,"Maximum value of X ever: 0",new Vector2(50,50),200);privatefloat max =0;publicMyScene(Game game) { super(game); } @OverridepublicvoidInitialize() { } @OverridepublicvoidUpdate() { } @OverridepublicvoidRender(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.javapackageavoid;importjava.awt.*;importjava.awt.color.ColorSpace;importdev.jabo.kree.*;importdev.jabo.kree.components.*;importdev.jabo.kree.ui.Button;importdev.jabo.kree.ui.Text;publicclassMySceneextendsScene {privateGameObject player;privateText txt =newText(this,"Maximum value of X ever: 0",new Vector2(50,50),200);privatefloat max=0;publicMyScene(Game game) { super(game); } @OverridepublicvoidInitialize() {// Player player =newGameObject(this,"Player");player.getTransform().setPosition(newVector2(game.getWindow().getWindowWidth() /2,game.getWindow().getWindowHeight() /2));player.getTransform().setScale(newVector2(32,32));player.addComponent(newMeshRenderer());player.addComponent(newPlayerMovement(1f,PlayerMovement.WASD));player.addComponent(newRigidBody()); ((RigidBody)player.getComponent("RigidBody")).gravity=false;player.addComponent(newPrefs()); } @OverridepublicvoidUpdate() { } @OverridepublicvoidRender(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.javapackageavoid;importjava.awt.*;importjava.awt.color.ColorSpace;importdev.jabo.kree.*;importdev.jabo.kree.components.*;importdev.jabo.kree.ui.Button;importdev.jabo.kree.ui.Text;publicclassMySceneextendsScene {privateGameObject player;privateText txt =newText(this,"Maximum value of X ever: 0",new Vector2(50,50),200);privatefloat max =0;publicMyScene(Game game) { super(game); } @OverridepublicvoidInitialize() {// Player player =newGameObject(this,"Player");player.getTransform().setPosition(newVector2(game.getWindow().getWindowWidth() /2,game.getWindow().getWindowHeight() /2));player.getTransform().setScale(newVector2(32,32));player.addComponent(newMeshRenderer());player.addComponent(newPlayerMovement(1f,PlayerMovement.WASD));player.addComponent(newRigidBody()); ((RigidBody)player.getComponent("RigidBody")).gravity=false;player.addComponent(newPrefs()); max = ((Prefs)player.getComponent("Prefs")).getFloat("max"); } @OverridepublicvoidUpdate() {if(player.getTransform().getPosition().getX() > max) { max =player.getTransform().getPosition().getX(); ((Prefs)player.getComponent("Prefs")).storeFloat("max", max); } } @OverridepublicvoidRender(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.javapackageavoid;importjava.awt.*;importjava.awt.color.ColorSpace;importdev.jabo.kree.*;importdev.jabo.kree.components.*;importdev.jabo.kree.ui.Button;importdev.jabo.kree.ui.Text;publicclassMySceneextendsScene {privateGameObject player;privateText txt =newText(this,"Maximum value of X ever: 0",new Vector2(50,50),200);privatefloat max =0;publicMyScene(Game game) { super(game); } @OverridepublicvoidInitialize() {// Player player =newGameObject(this,"Player");player.getTransform().setPosition(newVector2(game.getWindow().getWindowWidth() /2,game.getWindow().getWindowHeight() /2));player.getTransform().setScale(newVector2(32,32));player.addComponent(newMeshRenderer());player.addComponent(newPlayerMovement(1f,PlayerMovement.WASD));player.addComponent(newRigidBody()); ((RigidBody)player.getComponent("RigidBody")).gravity=false;player.addComponent(newPrefs()); max = ((Prefs)player.getComponent("Prefs")).getFloat("max"); } @OverridepublicvoidUpdate() {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); } } @OverridepublicvoidRender(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