Set up your Java Project and add Kree to your build path
Create a package called avoid
Creating a Launcher Class
packageavoid;importdev.jabo.kree.Game;importdev.jabo.kree.Window;publicclassLauncher {publicstaticvoidmain(String[] args) {Window window =newWindow("Avoid the Enemy",800,600);Game game =newGame(window);game.start(); }}
In this class we will add our main class.
In line 11 we will create an instance for our window which takes 3 parameters a window title, window width, and window height.
In line 12 we will create an instance for our Game which takes a parameter of a Window. In the Game class this is where all operations will start such as game loop, user inputs and more.
Lastly we have game.start() this will trigger the start of our game
You can name your Scene anyway you want for this tutorial we'll use GameScene . Your scene must extend Scene class so Kree will understand that you're creating a Scene
After extending our GameScene class to Scene Eclipse will give us an error. Don't fret we just need to implement some unimplemented methods such as Initialize, Render, and Update
Initialize method is called once after our scene is created
Update & Render method is called 60 times a second
Setting our active scene
packageavoid;importdev.jabo.kree.Game;importdev.jabo.kree.SceneManager;importdev.jabo.kree.Window;publicclassLauncher {publicstaticvoidmain(String[] args) {Window window =newWindow("Avoid the Enemy",800,600);Game game =newGame(window);GameScene gameScene =newGameScene(game);SceneManager.setScene(gameScene);game.start(); }}
In line 22 we are Initializing our player object to a new GameObject which takes 2 parameters a parent scene and the GameObject name
In line 23 we are setting the position of our player to the center of our window to do that we need to access some variables on our window shown in the code above
In line 24 we've set the scale of our object to 32 by 32 pixels
and lastly, we added a component called MeshRenderer to render a simple rectangle shape dependently on player's position and scale.
Creating Custom Component and Adding Player Movement
Creating a custom component in Kree is pretty similar to creating scenes but in creating custom component we needed to extend our class to Component class so that we can add it to our GameObject
In this tutorial we will take a unique approach on creating our player movement. The way we will implement it will be the player will follow the mouse whenever it has been clicked implementing this in our game using Kree is pretty straight forward we will use the method on Vector2 called moveTowards which takes 2 parameters the first one being a Vector2 and a float speed.
To achieve this mechanic we will need to create a Vector2 object which will be our target and in the Update method we will update it whenever the left mouse button was clicked
In line 20 you might be a little confuse. This is to prevent our program to get error when we're trying to access our GameObject
You might get confused on the gameobject variable that we're accessing. Basically when you add a component to a GameObject the gameobject variable will be assigned to the GameObject.
Creating an Enemy is similar on what we did on creating our player but to give our game more variety we added some randomness to our Enemy's position.
Generating random numbers with Kree is simple we just need to access a method on Random class built inside Kree. Random.range(min, max)
To make our enemy more dangerous we added a mechanic that enemy should follow the player but slower than the player so the our enemy will not be too overpowered
You might notice that we did not use the BoxCollider class for this one the reason is the BoxCollider on Kree v1.9 is pretty bug which will be fixed soon
To detect if the enemy hitted our player we will use a static class from Vecto2 which is the distance which will return the distance between 2 vectors
You can see that we used Debug class here to debug our game. Debug class is pretty useful when we are debugging something on our code.
What to do next?
To give our game more customability we will end the tutorial here. Let your creativity decide where to go next
But before we ended here let me give you some tips to improve our game
Add User Interface!
Add ParticleSystems!
Congratulations on completing this tutorial!
Need help?
Feel free to go through the documentation provided in this website