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. …

android game development

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 GameView.java 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 GameView.java. GameView.java was finally added to the GameActivity.java. 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 GameView.java. 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 GameView.java
  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.

friend

  • Now, create a class Friend.java 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 GameView.java 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 GameView.java as follows.
  • First declare the following parameters in GameView.java as follows:-

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

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

  • As the game Over gets over, we need  to draw a big Game Over text in the draw() method of GameView.java. To do so, update the draw() method in GameView.java 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 GameView.java

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

  • Next, update the update() method in GameView.java 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 GameView.java 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 MainActivity.java. 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 HighScore.java 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 HighScore.java. It simply fetches the scores through shared Preferences and sets them to the textViews. So, go ahead and add the following code to the HighScore.java.

  • Now that our HighScore activity is ready, define the onClick method of highscore button in MainActivity.java 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 GameView.java 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 GameView.java 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 GameView.java which will be used later in the MainActivity.java.
  • So, go ahead and add a method named stopMusic() to the class GameView.java 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 GameView.java as follows:-

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

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

  • Finally, we need to add  exit confirmation dialog boxes to the MainActivity and GameActivity.java. 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.

[sociallocker id=1372] Android Game Development Tutorial Source Code [/sociallocker]

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

Hello, I am Manish Kumar. I am a B.Tech Student at NIT Jamshedpur. Fortunately, I find myself quite passionate about Computers and Technology. Android is my most recent Crush.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x