📜 ⬆️ ⬇️

Golden T Game Engine (GTGE) - first acquaintance

It so happened that I needed to write a 2D application - a game in Java. I had to look for a ready-made engine that can load sprites, handle collisions of objects, work with tiles and backgrounds. It was the minimum set of functions I needed. The choice fell on the " Golden T Game Engine (GTGE) " . Having clearly understood it, I would like to share my experience with the Habrowiers .

After downloading the engine from the official site, connect it to our project. Now create the main application class. I have it called Main . After that, we will write the minimum application for the engine.
import java.awt. * ;
import com.golden.gamedev. * ;

public class Main extends Game {
public static void main ( String [ ] args ) {
GameLoader game = new GameLoader ( ) ; // Class that controls the initialization of the application
game. setup ( new Main ( ) , new Dimension ( 640 , 480 ) , false ) ; // Initialize the graphics engine
game. start ( ) ;
}

@Override
public void initResources ( ) {
// Initialize game variables and resources
}
')
@Override
public void render ( Graphics2D g ) {
// Rendering Graphics
}

@Override
public void update ( long elapsedTime ) {
// Updates of variables and resources
}
}

Now, you can run. A window appears with the GTGE logo and FPS counter. But the picture is not updated. Simply put, overlaps each other. This can be seen by looking at the FPS counter. Next, you need to initialize the playing field with the background. As a background, there can be any RGB color, a picture. What is the playing field? The game field is a platform for rendering graphics on it. To create the playing field and subsequently the background to it, you need to connect a couple more files:
import com.golden.gamedev.object. * ;
import com.golden.gamedev.object.background. * ;

Playground (Play Field) - is a class. But before creating an object, we need to initialize the background. In this case, it will be a solid white color. At the beginning of the Main class we write:
Background back = new ColorBackground ( new Color ( 255 , 255 , 255 ) ) ;

Now, it's time to create a playing field. Immediately after initializing the background, we write:
PlayField pf = new PlayField ( back ) ;

One of the arguments to the PlayField class constructor is the background, which will be set by default. And finally, the most important thing is the update and rendering of the playground.
In the graphics rendering function, we write the following:
pf. render ( g ) ;

And in the update function -
pf. update ( elapsedTime ) ;

You can run. What do we see? White empty playing field. Something is missing. Next, we will create a game object that is controlled by arrows.
To draw a sprite on the playground, you must first load it and place it in a group of sprites. This is what we do now. We will need three variables that are responsible for the group of the sprite, the loaded picture and, in fact, for the sprite itself .
We declare these variables at the very beginning of the class:
SpriteGroup spr_group ;
BufferedImage spr_buffer ;
Sprite spr_player ;

Next, connect the file to upload images:
import java.awt.image.BufferedImage ;

That's it, now full combat readiness for sprite loading. I chose this picture as the “main character”:
image
How to download the sprite and put it on the playground? For starters, we need a group for sprites. The group is needed for convenient storage of all sprites. For each group can be set N- th number of sprites. It is much more convenient to manipulate the group than each sprite separately. After creating the group, you need to upload a picture. Then, create a sprite object and assign it a downloaded image. Then, add this sprite to the group and place the group on the playground.
Here is the code with which you can do the above (loading the picture should only be in the resource initialization function! Therefore, I transferred the creation of the sprite and similar functions to this function) :
// Create a group of sprites
spr_group = new SpriteGroup ( "grp_player" ) ;
// Load the picture
spr_buffer = getImage ( "plane1.png" ) ;
// Create a player sprite
spr_player = new Sprite ( spr_buffer, 100 , 100 ) ;
// Add player sprite to group
spr_group. add ( spr_player ) ;
// Put a group of sprites on the playground
pf. addGroup ( spr_group ) ;

Now, you can start the game and see what happened there? We have a sprite on the playing field. It would be nice, "learn" to manage it. What are we doing for this?
We have a link to the sprite object. This object has functions moveX and moveY . Here we will manipulate them.
To handle keystrokes, we must include the following file:
import java.awt.event.KeyEvent ;

In the update function we will write the following:
if ( keyDown ( KeyEvent . VK_W ) ) spr_player. moveY ( - 3 ) ;
if ( keyDown ( KeyEvent . VK_S ) ) spr_player. moveY ( 3 ) ;
if ( keyDown ( KeyEvent . VK_A ) ) spr_player. moveX ( - 3 ) ;
if ( keyDown ( KeyEvent . VK_D ) ) spr_player. moveX ( 3 ) ;

Now, we can control our main character.

Animation

Next, I would like to talk about the loading animation. For animation, I will use the following sprite:
image
In order to create an animated sprite, you need to upload a picture with a special function getImage s . You can upload as well as a picture consisting of several frames, and each frame separately. Here are two small examples:
Example 1:
BufferedImage [ ] aspr_buffer = getImages ( "plane2.png" , 3 , 1 ) ;

Example 2:
BufferedImage [ ] aspr_buffer = {
getImage ( "plane1.png" ) ,
getImage ( "plane2.png" ) ,
getImage ( "plane3.png" )
} ;

In the first example, we load one image and the function already cuts it into separate frames. The first argument of the function is the path to the picture. The second is the number of frames horizontally, the third argument is the number of frames vertically. It's simple. In essence, the function in the first example does the same as in the second example.
In the second example, we create an array and load one by one into it.
To create a sprite for this image, you need to create an object for an animated sprite:
aspr_player = new AnimatedSprite ( aspr_buffer, 100 , 100 ) ;

This object has a number of functions. The main ones, I will now describe in detail.
// The function returns a reference to the timer object
aspr_player. getAnimationTimer ( ) ;

In order to change the speed of the animation, we need to turn to the timer. This is done by the function above. Then, we call the setDelay function, the argument of which is a number - the number of ms between shifts by the frame of the sprite.
aspr_player. getAnimationTimer ( ) . setDelay ( 100 ) ;

Each frame will be replaced after 100ms.
To change the number of frames of an animated sprite, you need to call the setAnimationFrame function
Example:
aspr_player. setAnimationFrame ( 0 , aspr_buffer. length - 1 ) ;

The first argument is the first frame, the second is the last. The last frame is the number of elements in the array with the loaded pictures - 1 . Thus, you can change the sequence of frames in the sprite.
Using the setAnimate function, you can enable / disable sprite animation.
// Animation enabled
aspr_player. setAnimate ( true ) ;

And using the setLoopAnim function, you can change the “looping” of frames. Those. after the end of the animation, it will not be stopped, but will automatically start over.
aspr_player. setLoopAnim ( true ) ;

Let's return to our main example.
Change the image upload code and create sprites on
aspr_buffer = getImages ( "plane2.png" , 3 , 1 ) ;
aspr_player = new AnimatedSprite ( aspr_buffer, 100 , 100 ) ;
aspr_player. getAnimationTimer ( ) . setDelay ( 80 ) ;
aspr_player. setAnimationFrame ( 0 , aspr_buffer. length - 1 ) ;
aspr_player. setAnimate ( true ) ;
aspr_player. setLoopAnim ( true ) ;
spr_group. add ( aspr_player ) ;

And at the beginning of the class, we will declare several variables:
BufferedImage [ ] aspr_buffer ;
AnimatedSprite aspr_player ;

And also change the motion of the sprite to
if ( keyDown ( KeyEvent . VK_W ) ) aspr_player. moveY ( - 2 ) ;
if ( keyDown ( KeyEvent . VK_S ) ) aspr_player. moveY ( 2 ) ;
if ( keyDown ( KeyEvent . VK_A ) ) aspr_player. moveX ( - 2 ) ;
if ( keyDown ( KeyEvent . VK_D ) ) aspr_player. moveX ( 2 ) ;

Now you can run and see. On the playground there is an airplane sprite with a “spinning blade”.

On this, I finish the first acquaintance with the Golden T Game Engine . This is not all that I would like to talk about, but the post turned out to be quite large. Thanks so much for reading the article.

For writing the article I used information from the official site of the engine.
Link to an example.

Source: https://habr.com/ru/post/130115/


All Articles