Android Game Development

There are many ways and many frameworks out there for Android Game development. But, this Kotlin Android Tutorial helps you to get started with Android Game Development from scratch with well detailed steps.

Game Story

Any game that you would like to develop should have a story and script. You have to decide about the players in the game, the strategies, how you level up in the game, and what are the objects that are going to be used. When you are ready with an fully scripted idea, you are good to start for taking it to application development mode. Small changes to the script during the development may not cost you much time, but if there are major changes to the game plan or such, it may eat up your development time. And this could be checked to some extent by modularizing your code.

In this tutorial, our game story is as follows.

There is a grenade trying to blast you. The player has to avoid it. A simple hero-villain story to get started.

What game development techniques we shall learn in this tutorial

For any android application development, we need some techniques. For example, if you are creating a Contacts application, you should be having an idea of how to get the list of contacts and how to present it to the user : these are the techniques or capabilities that the developer should be aware of.

Similarly for this android game, we shall learn following techniques to realize our game.

  • PlayGround : Use of SurfaceView and SurfaceHolder.Callback
  • Sprites on Playground : In Gaming terminology, Sprite is a 2D bitmap image drawn on to the Canvas of SurfaceView
  • Sprite Movement : Redrawing the image of sprite at a different position
  • Sprite Position : Tracking the touch of the Android user, to update the position of sprite(s)

Android Game Development – Kotlin Example

Following is a step-by-step guide to develop an Android Game in Kotlin programming language. Or if you like to get to the code and go through it yourself, you may find the zipped file at https://github.com/tutorialkart/Android-Game-Examples/blob/master/ExampleGame.zip.

  1. Create an Android Application for the Game.

    Open Android Studio and create a new Application with Kotlin support. Create Android Application with Kotlin supportLets name our application with “Example Game” and Click on Next. Select Minimum SDKFor this project, we have selected Android 5.0 as minimum supported Android version. Click on Next. Android Empty ActivitySelect an “Empty Activity” and Click on Next. Main ActivityUncheck “Backwards Compatibility(AppCompat)” and click on Finish. Our Android Application is created.

  2. Add SurfaceView and SurfaceHolder.Callback which is our playground.

    SurfaceView by default does not help us much. So, we shall create a class that extends SurfaceView and implements SurfaceHolder.Callback. New Kotlin ClassRight Click on the package “com.tutorialkart.examplegame” and follow the actions. Kotlin Class Selection Give name as “GameView” and select “Class” for Kind.

    Change the primary constructor or GameView to GameView(context: Context, attributes: AttributeSet) and Extend with SurfaceView(context, attributes), SurfaceHolder.Callback. You should override some of the functions of SurfaveHolder.Callback as shown in the below program.

    Also, in order to update the position of our players and draw them on the Canvas of SurfaceView, we need to create a function update() and override draw(Canvas: canvas) function of SurfaceView.

  3. Include GameView in activity_main.xml.

    We shall use the GameView in activity_main.xml.

    And in the AndroidManifest.xml, lets set the theme as Theme.NoTitleBar.Fullscreen, for the MainActivity.

  4. FullScreen Activity.

    Make your Activity get rid of the notification bar and title bar.

  5. Create Game Thread.

    Next thing we need to do is, creating a thread which calls GameView for : updating the state of the game and drawing the game objects on to Canvas. GameView runs on UI Thread and it could go through different stages of life cycle. We do not want this to mess up with the smoothness of our game, which is why we are using a dedicated thread. We shall create a Kotlin Class named, “GameThread”. The process of creating this Kotlin Class is same as that of GameView Class, and the GameThread.kt is as shown below :

    The important things to note here is the following four lines of code, in the try block :

    – We locked the Canvas, so that it allows us to draw on it. – Using synchronized(surfaceHolder) means, we are not letting any other threads do any modifications to surfaceHolder (where Canvas is). – Then we update the game state and draw the canvas with the new state of sprites. Once we are done updating the canvas, unlock it in the finally block.

    The rest of the code is trying to update the canvas at the frequency set by targetFPS . And finally where we are going to start this GameThread ? Ofcourse from GameView.

    Try giving it a run on an Android Phone. If all you are seeing is a black screen! Good. We have successfully learned to use create a SurfaceView. And we have prepared our playground. Now its time to introduce some players on to the field.

  6. Introducing Sprites.

    Now, we shall introduce a sprite that moves on the Canvas. And keeping the idea that our game should be maintainable, when it becomes huge with code, we shall try to abstract a game object with a Class. We shall use the class to hold bitmap image, its position and state through the game. We shall take a Grenade as a Sprite. Following is Grenade.kt

    Take away from this class is that, we have two functions namely update() and draw(). We shall instantiate Grenade when Surface is created, and call Grenade.update() from GameView.update() and Grenade.draw() from GameView.draw().

    This is kind of our scaling dimension. We can add as many types of sprites as per the game plan (script), and instantiate the sprites in GameView.surfaceCreated(), update the sprites’ state in GameView.update() and draw the sprite on to Canvas in GameView.draw().

    Add the following image file to res/drawable

    Android Game ResourceWhen run the application in an Android device, the result would look like below : Android Game Example

  7. Introducing another Sprite – Our next and ofcourse the most important Game Object.

    For the player, we shall use a circle kind of image and make it track the touch on the screen. And like Grenade.kt, we are going to have update() and draw() functions.

    Now we have to make use of Player in our GameView, just as we did for Grenade.kt. But we are going to learn one more thing in this step. That is, how to track user touch input. And we shall make our Player track the touch. To track touch input, we shall override onTouchEvent(event: MotionEvent) method in GameView Class. And with the introduction of Player.kt to GameView, GameView.kt may look like as below :

Run the application and you will observe that the circle (Player bitmap), shall track your touch.

Conclusion :

There are many other techniques that we could learn to use in Android Game Development. In our subsequent Kotlin Android Tutorials, we shall learn some of them and make our Game awesome, or you may use them to make your Game Script come to life. Happy coding Android.