Android Game Development Tutorial – Simple 2d Game Part 2

Hello friends, here comes the part 2 of Simple 2D Android Game Development Tutorial. Before we get started, let me do a quick recap of what we went through in part 1 of this project.

So we began with designing the StartScreen. Then we created the class in which we drew the Player, Enemy and the Stars in background. Also, we created methods to update the position of the the player and all so that it looked like it was moving in was finally added to the Then we wrote conditions to detect the collision between the player and the enemy and also added controls. This is where we drew the Boom in the I guess that was enough to sum up of what we did in part 1. You can also visit the part 1 from the below given link.

Android Game Development Tutorial – Simple 2d Game Part 1

You can also download the finished apk from the below link to test the game in your device.

[download id=”3641″]

In part 2, we shall begin with the following objectives in mind:

  1. Drawing the friendly spaceships and setting motion to them.
  2. Detecting their collision and configuring the game over condition.
  3. Adding the gameOver condition due to the miss of 3 enemies.
  4. Drawing the “Game Over ” text in
  5. Adding a parameter score to calculate the score.
  6. Adding the HighScore activity to view the the four highest scores in the history of game.
  7. Adding background music to the game

We have the above seven objectives in this final part of Android Game Development Tutorial. So lets begin.

Android Game Development Tutorial Part 2

Adding Friend

Let us begin with the addition of friendly ships on the GameScreen. According to the game story we decided in part 1, we would have a friendly ship in the game. If the player collides with the friendly ship then GAME OVER.  So lets add a friendly ship in our game.

  • Download the image below add it to the drawables which will be used as the image for friend  ship.


  • Now, create a class and add the following code.

  • As we added the above class, now we need to add the friend ships to GameView.
  • Hence, update your class with the code given below. To lower the difficulty of the game, the number of enemies entering the GameScreen at a time has been changed from three to one which can be noticed as the enemies are added to GameView below.

Detecting Collision

Again we need to detect collision between Player and Friend ships to make our Game Over.

  • To detect collision betweeen Player and Friend, we will again use Rect Object. To do so, update the code of Friend class as follows.

Making Game Over Condition

As per the story of  this project, this game can get over in two ways.  Either the player misses a total number of 3 enemies or the player collides with friend. So lets make this condition working in our game.

  • To make the above two conditions of game over, update the as follows.
  • First declare the following parameters in as follows:-

  • Now initialize the above parameters in the GameView constructor as follows.

  • flag will be initialized later.
  • Now, Update the update() method in as follows:-

  • As the game Over gets over, we need  to draw a big Game Over text in the draw() method of To do so, update the draw() method in as follows.

  • After this the Game Over screen would appear as follows.
android game development tutorial game over
Game Over

Adding Scores

  • Now as the GameOver has been configured, its time to configure the scores achieved. To do so, begin with declaring the following variables in

Next, initialize the above parameters in the GameView constructor as follows:-

  • Next, update the update() method in as follows.

  • By now, the score is being the calculated and being score into the high scores. Now, to see the increment of score live on the game screen, update the draw() method in as follows.

  • The preview of score being shown on the game screen can be seen as follows:-

android game development tutorial game over

Making High Scores

  • Now that we have all the high scores set in shared Preferences, its time to configure the high score button in Its click should should take the game to another activity where the highest four scores would be listed.
  • To do so, begin with creating a new activity called HighScore. As the new activity is loaded,  configure the and activity_high_score.xml as follows.
  • Here’s the activity_high_score.xml. It consistes of four textViews showing the highest four scores in the history of game. So, go ahead and add the following code to the activity_high_score.xml.



  • The above code will generate the following preview.


android game development high scores

  • Here’s the It simply fetches the scores through shared Preferences and sets them to the textViews. So, go ahead and add the following code to the

  • Now that our HighScore activity is ready, define the onClick method of highscore button in as follows:-

  • By now, we have drawn the friends onto the game screen, configured the game Over, configured the score and the HighScore Activity.
  • The game is playable but is yet boring without any sound. It calls for some backgroung music as the game is played.

Adding Sounds

  • To add sounds, firstly download the audio tracks from the link given below and add them to the raw folder(create it first) in resources.

Download Sound Files

  • Next, declare the following MediaPlayer objects in as follows:-

Next, intialize the above objects in GameView constructor  and start the game music method as shown below:-

  • Now, we need to manipulate the above MediaPlayer objects to start and stop the sounds as per required. To do so, update the update() method in as follows

  • We are almost done with this Android Game Development Tutorial. Just a few things remain to be configured.
  • One of which is to add a static method to the class which will be used later in the
  • So, go ahead and add a method named stopMusic() to the class as shown below:-

  • Next, we need to configure the tap on the GameOver screen such that it takes you to MainActivity. To do so, declare a variable context of the type Context in the  class as follows:-

  • Further, initialize it in the GameView constructor as follows:-

  • Now, update the onTouchEvent() method in as follows:-

  • Finally, we need to add  exit confirmation dialog boxes to the MainActivity and To do so, add the following method to both the above mentioned classes.

  • And you did it. You finally developed a fully functional 2d game in android. Play the game and have fun. If you find something fishy or you are stuck at something, feel free to tell me in the comments section.
  • And yes the source code is available of this Android Game Development Tutorial, in GitHub repository and you can get it from the below link.

So thats all for this Android Game Development Tutorial Series. Stay tuned and we will publish more interesting Android Game Development Tutorials. Thank You 🙂

24 thoughts on “Android Game Development Tutorial – Simple 2d Game Part 2”

  1. Bug on checking if three enemies were missed.

    Checking for if the center was met is a bad idea, as a hit can occur on the enemy after the player crosses the center lines.

    A better way to check if a miss occured could be to either:
    A) Insert a miss coutner increment for when the enemy goes off screen
    B) For when the right box of the enemy collision box passes/exceeds the left edge of the player collision box.

    • Closing app will not reset your highscores. However, clearing app data from Settings will reset your highscores.

  2. your update method having errors u r using gt; for >= symbol and amp for &
    u also said to add on back pressed method to both main activity and gavmeview its giving erros

  3. for(int i=0;i<4;i++){
    What is this? I have compile errors. Is this java?
    Please help. Are there any alternatives for write this?

  4. very nice tutorial but i am getting an unexpected error.
    The game is automatically over after some seconds not by the rules.
    Please help me

  5. How to identify the collision at the exact touch of the incoming Friend or enemy? I am using my own image for this and the collision is happening as soon as the rectangular side of the image is being touched..and that looks like collision has happened even before actually touching the incoming object.

    How can i rectify this ? Or Do I need to use some specific kind of image ?

  6. Game is excellent. But the enemy and friend are out of the screen which makes it impossible to know how many we have missed. Can we adjust them inside the device’s screen? Please do reply. 🙂

  7. in frist tutorial in you used
    for (int i = 0; i < enemyCount; i++) {
    in second tutorial it's removed

  8. I forgot to mention I changed the code little bit to stop and start music on pressing home button of user phone..

    and manage to add enemy and friends within the screen, cause with your great tutorial code sometime enemies and friends are getting half part on screen and half hidden into screen….

    although really enjoyed your tutorial… thanks a 101 times…. (y)

    please keep up the good work.

  9. This was such a smooth install, thanks so much! This was my first Android project. I installed java, Android Studio, and got your code from GitHub. It had one error in compiling, and I had to add the google() line in 2 places in build.gradle
    buildscript {
    repositories {
    dependencies {
    classpath ‘’

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files

    allprojects {
    repositories {
    maven { url “” }

    After that it compiled and runs on both the emulator and on my physical phone. It’s so encouraging to have something actually work so quick. I’ve had many experiences on other platforms where nothing works and it gets very frustrating. You made this super easy. Thanks!

  10. //if player’s x coordinate is equal to enemies’s y coordinate

    1) at p (of player) it is showing expression expected
    2) cannot resolve symbol gt
    3) at = it is showing unexpected token
    4) after closing if parenthesis it is showing ; expected

    //Assigning the scores to the highscore integer array
    for(int i=0;i<4;i++){

    final int finalI = i;
    highScore[i] = score;

    1) cannot resolve symbol lt
    2) at 4; it is showing Not a statement ‘)’ expected
    3) after closing for loop parenthesis ( for()) it is showing ; expected
    4) inside parenthesis of if at h (of highScore) it is showing expression expected
    5) after closing parenthesis of if it is showing ; expected
    6) break outside switch or loop

    public boolean onTouchEvent(MotionEvent motionEvent) {
    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_DOWN:android.view.MotionEvent


    1) inside parenthesis of switch at m (of motionEvent) it is showing expression expected
    2) cannot resolve symbol amp
    3) after closing parenthesis of switch it is showing ; expected
    5) at both case MotionEvent.ACTION_UP: and case MotionEvent.ACTION_DOWN: it is showing case
    statement outside switch
    6) break outside switch or loop


Leave a Comment