Day 12 and 13 – Learning Android Development

I decided to start with game development on android. I will use a free library called libgdx and follow a tutorial on making a little 2D game with it. libgdx is a java library that allows its users to create games on their desktop and then deploy it to Android. The author of the tutorial says that it is based on OpenGL, but you don’t touch the OpenGL yourself,  like in pygame. You let libgdx deal with the complex stuff while you focus on the actual game. Sounds like something good right now, as I want to publish my first app in a short time.

The Tutorial

Everything that follows is what I did by following the tutorial which is linked on the paragraph above. The author is Tamas Jano, all credits for this game go to him. Here is the game’s source. The game is based off another game called Star Guard.

I started off by installing libgdx and by creating three projects, star-assault, the main project, and star-assault-android along with star-assault-desktop. The last two serve as launchers for the two platforms. Then i added the libgdx JARS into their corresponding build paths within the project so I can use/implement the methods.

After the setting up, I started with the game. I created the game class StarAssault and then implemented the interface ApplicationListener. Which has the core methods create(), resize(), render(), pause(), resume(), dispose(). Most of them correspond to the app lifecycle phases. The render() method is the heart of the game, there is where I put the main loop and resize() id when the screen dimensions are changed.

After just implementing the methods without filling them up, the player class which is called Bob is created. The game is 2D tile based, so 1 block is 1 unit of height and 1 unit of width. Bob is 1/2 unit tall. Bob has several attributes, like a position in the world, acceleration when jumping, velocity when moving, etc. Bob will be running at 2.7 units per second. Because a running speed of 10km/h is 2.7 meters per second, and 1 meter = 1 unit.

Then follow the Block class, which is another actor just like Bob. Blocks  have some attributes as well, a position, bounds so that nothing penetrates them and sizeBoth Bob and Block use a vector class from libgdx for the position, this helps because it provides help with position, velocity, collition etc.

Next is the World class, which is a container class for Bob and Block. An array is created for several instantiated Blocks and Bob is of course uniquely instantiated unless I want more Bobs around.

To render the world, a screen is needed, so I created the class GameScreen . Screen is an interface from libgdx, I implement that to the GameScreen class. The Game class from libgdx has tools that are needed so I extended the StarAssault class with Game. Gamescreen is then called/instantiated under StarAssault.

StarAssault will use the create() method, which will instantiate a GameScreen object. The GameScreen object will then use the show() method and subsequently the render() method for every cycle. So the game loop will be situated in the GameScreen class.

What follows is the WorldRenderer class, this one get’s instantiated under the show() method in GameScreen . Then under the GameScreen’s render() method, the method render() from WorldRenderer gets invoked. WorlRenderer.render()  gets all the information from the world and its actors and renders them.

After setting up the renderer, the positions, without setting up an event handler, or proper images, and setting up the desktop launcher, I got this:

Desktop Launcher
on my Samsung Galaxy SII

The blocks are wider on my phone because I used a dimension of 480×320 for the desktop version, which is apparently not the same ratio as the Samsung’s.

Remember I said height and width were the same? well, only for the engine, not visually. The engine is told to render a screen equivalent to 10×7 blocks and each block measures 1(unit). But it is also told to fill out the screen and so the width of the blocks are visually bigger, because a phone’s horizontal view has a wide aspect. And so when an image is inserted, it will be distorted by OpenGL in order to fill out the screen.

With the Textures
On the SII

I made the block 20×20 pixels and Bob 6×10. On the desktop, the resolution of the window is 480×320, so that means each block should have a width of 48 so each pixel matches the screen pixels, but there’s only 20 pixels in the width of each block, so that means the image gets distorted (it gets wider) to fill out. Doesn’t really matter for this game though.

for the event handling, I created a new package and in it the class WorldController. The class is able to handle both touch input for android and key input for the PC. Bob is able to move now, but only horizontally.

Conclusion

This is as far as the tutorial goes, a second part was promised where the game would be completed with features like terrain interaction, animations, sounds, AI, etc. but seeing as the post was released on February 2012, I doubt a second part will ever come. Nonetheless this was a great tutorial and I will probably take another look at how this game was made again step by step. I liked the library too. Thanks Tamas.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *