Simplified Coding

  • About
  • Contact Us
  • Advertise
  • Privacy Policy
You are here: Home / Android Application Development / Android Advance / Android Game Development Tutorial – Simple 2d Game Part 2

Android Game Development Tutorial – Simple 2d Game Part 2

October 27, 2016 by Manish Kumar 24 Comments

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

Contents

  • 1 Android Game Development Tutorial Part 2
    • 1.1 Adding Friend
    • 1.2 Detecting Collision
    • 1.3 Making Game Over Condition
    • 1.4 Adding Scores
    • 1.5 Making High Scores
    • 1.6 Adding Sounds
    • 1.7 Sharing is Caring:
    • 1.8 Related

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.

Friend.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package net.simplifiedcoding.spacefighter;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
 
import java.util.Random;
 
/**
* Created by Manish on 10/24/2016.
*/
 
public class Friend {
 
    private Bitmap bitmap;
    private int x;
    private int y;
    private int speed = 1;
 
    private int maxX;
    private int minX;
 
    private int maxY;
    private int minY;
 
 
    public Friend(Context context, int screenX, int screenY) {
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.friend);
        maxX = screenX;
        maxY = screenY;
        minX = 0;
        minY = 0;
        Random generator = new Random();
        speed = generator.nextInt(6) + 10;
        x = screenX;
        y = generator.nextInt(maxY) - bitmap.getHeight();
 
    }
 
    public void update(int playerSpeed) {
        x -= playerSpeed;
        x -= speed;
        if (x < minX - bitmap.getWidth()) {
            Random generator = new Random();
            speed = generator.nextInt(10) + 10;
            x = maxX;
            y = generator.nextInt(maxY) - bitmap.getHeight();
        }
    }
 
    //one more getter for getting the rect object
    public Rect getDetectCollision() {
        return detectCollision;
    }
 
    //getters
    public Bitmap getBitmap() {
        return bitmap;
    }
 
    public int getX() {
        return x;
    }
 
    public int getY() {
        return y;
    }
 
}

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

GameView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package net.simplifiedcoding.spacefighter;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class GameView extends SurfaceView implements Runnable {
 
    volatile boolean playing;
    private Thread gameThread = null;
    private Player player;
 
    private Paint paint;
    private Canvas canvas;
    private SurfaceHolder surfaceHolder;
 
    private Enemy enemies;
 
    //created a reference of the class Friend
    private Friend friend;
 
 
    private ArrayList<Star> stars = new
            ArrayList<Star>();
 
    //defining a boom object to display blast
    private Boom boom;
 
 
    public GameView(Context context, int screenX, int screenY) {
        super(context);
        player = new Player(context, screenX, screenY);
 
        surfaceHolder = getHolder();
        paint = new Paint();
 
        int starNums = 100;
        for (int i = 0; i < starNums; i++) {
            Star s = new Star(screenX, screenY);
            stars.add(s);
        }
 
        //single enemy initialization
        enemies = new Enemy(context, screenX, screenY);
 
        //initializing boom object
        boom = new Boom(context);
 
        //initializing the Friend class object
        friend = new Friend(context, screenX, screenY);
    }
 
 
    @Override
    public void run() {
        while (playing) {
            update();
            draw();
            control();
        }
    }
 
    private void update() {
        player.update();
        //setting boom outside the screen
        boom.setX(-250);
        boom.setY(-250);
 
        for (Star s : stars) {
            s.update(player.getSpeed());
        }
 
        enemies.update(player.getSpeed());
        //if collision occurs with player
        if (Rect.intersects(player.getDetectCollision(), enemies.getDetectCollision())) {
            //displaying boom at that location
            boom.setX(enemies.getX());
            boom.setY(enemies.getY());
            //will play a sound at the collision between player and the enemy
            enemies.setX(-200);
        }
 
        //updating the friend ships coordinates
        friend.update(player.getSpeed());
    }
 
    private void draw() {
        if (surfaceHolder.getSurface().isValid()) {
            canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.BLACK);
 
            paint.setColor(Color.WHITE);
            paint.setTextSize(20);
 
            for (Star s : stars) {
                paint.setStrokeWidth(s.getStarWidth());
                canvas.drawPoint(s.getX(), s.getY(), paint);
            }
 
            canvas.drawBitmap(
                    player.getBitmap(),
                    player.getX(),
                    player.getY(),
                    paint);
 
            canvas.drawBitmap(
                    enemies.getBitmap(),
                    enemies.getX(),
                    enemies.getY(),
                    paint
            );
 
            //drawing boom image
            canvas.drawBitmap(
                    boom.getBitmap(),
                    boom.getX(),
                    boom.getY(),
                    paint
            );
 
            //drawing friends image
            canvas.drawBitmap(
 
                    friend.getBitmap(),
                    friend.getX(),
                    friend.getY(),
                    paint
            );
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
 
    private void control() {
        try {
            gameThread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    public void pause() {
        playing = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) {
        }
    }
 
    public void resume() {
        playing = true;
        gameThread = new Thread(this);
        gameThread.start();
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                player.stopBoosting();
                break;
            case MotionEvent.ACTION_DOWN:
                player.setBoosting();
                break;
        }
        return true;
    }
}

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.

Friend.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package net.simplifiedcoding.spacefighter;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
 
import java.util.Random;
 
/**
* Created by Manish on 10/24/2016.
*/
 
public class Friend {
 
    private Bitmap bitmap;
    private int x;
    private int y;
    private int speed = 1;
 
    private int maxX;
    private int minX;
 
    private int maxY;
    private int minY;
 
    //creating a rect object for a friendly ship
    private Rect detectCollision;
 
 
    public Friend(Context context, int screenX, int screenY) {
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.friend);
        maxX = screenX;
        maxY = screenY;
        minX = 0;
        minY = 0;
        Random generator = new Random();
        speed = generator.nextInt(6) + 10;
        x = screenX;
        y = generator.nextInt(maxY) - bitmap.getHeight();
 
        //initializing rect object
        detectCollision = new Rect(x, y, bitmap.getWidth(), bitmap.getHeight());
    }
 
    public void update(int playerSpeed) {
        x -= playerSpeed;
        x -= speed;
        if (x < minX - bitmap.getWidth()) {
            Random generator = new Random();
            speed = generator.nextInt(10) + 10;
            x = maxX;
            y = generator.nextInt(maxY) - bitmap.getHeight();
        }
 
        //Adding the top, left, bottom and right to the rect object
        detectCollision.left = x;
        detectCollision.top = y;
        detectCollision.right = x + bitmap.getWidth();
        detectCollision.bottom = y + bitmap.getHeight();
    }
 
 
 
    //one more getter for getting the rect object
    public Rect getDetectCollision() {
        return detectCollision;
    }
 
    //getters
    public Bitmap getBitmap() {
        return bitmap;
    }
 
    public int getX() {
        return x;
    }
 
    public int getY() {
        return y;
    }
 
}

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:-

1
2
3
4
5
6
7
8
9
10
11
//a screenX holder
int screenX;
 
//to count the number of Misses
int countMisses;
 
//indicator that the enemy has just entered the game screen
boolean flag ;
 
//an indicator if the game is Over
private boolean isGameOver ;

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

1
2
3
4
5
this.screenX = screenX;
 
countMisses = 0;
 
isGameOver = false;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
private void update() {
    player.update();
 
    //setting boom outside the screen
    boom.setX(-250);
    boom.setY(-250);
 
    for (Star s : stars) {
        s.update(player.getSpeed());
    }
 
    //setting the flag true when the enemy just enters the screen
    if(enemies.getX()==screenX){
        flag = true;
    }
 
    enemies.update(player.getSpeed());
    //if collision occurs with player
    if (Rect.intersects(player.getDetectCollision(), enemies.getDetectCollision())) {
        //displaying boom at that location
        boom.setX(enemies.getX());
        boom.setY(enemies.getY());
        //will play a sound at the collision between player and the enemy
 
 
        enemies.setX(-200);
    }// the condition where player misses the enemy
    else{
        //if the enemy has just entered
        if(flag){
            //if player's x coordinate is more than the enemies's x coordinate.i.e. enemy has just passed across the player
            if(player.getDetectCollision().exactCenterX() >= enemies.getDetectCollision().exactCenterX()){
                //increment countMisses
                countMisses++;
 
                //setting the flag false so that the else part is executed only when new enemy enters the screen
                flag = false;
                //if no of Misses is equal to 3, then game is over.
                if(countMisses==3){
                    //setting playing false to stop the game.
                    playing = false;
                    isGameOver = true;
                }
            }
        }
    }
 
    //updating the friend ships coordinates
    friend.update(player.getSpeed());
    //checking for a collision between player and a friend
    if(Rect.intersects(player.getDetectCollision(),friend.getDetectCollision())){
 
        //displaying the boom at the collision
        boom.setX(friend.getX());
        boom.setY(friend.getY());
        //setting playing false to stop the game
        playing = false;
        //setting the isGameOver true as the game is over
        isGameOver = true;
    }
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
private void draw() {
    if (surfaceHolder.getSurface().isValid()) {
        canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(Color.BLACK);
 
        paint.setColor(Color.WHITE);
        paint.setTextSize(20);
 
        for (Star s : stars) {
            paint.setStrokeWidth(s.getStarWidth());
            canvas.drawPoint(s.getX(), s.getY(), paint);
        }
 
        canvas.drawBitmap(
                player.getBitmap(),
                player.getX(),
                player.getY(),
                paint);
 
 
        canvas.drawBitmap(
                enemies.getBitmap(),
                enemies.getX(),
                enemies.getY(),
                paint
        );
 
        //drawing boom image
        canvas.drawBitmap(
                boom.getBitmap(),
                boom.getX(),
                boom.getY(),
                paint
        );
 
        //drawing friends image
        canvas.drawBitmap(
 
                friend.getBitmap(),
                friend.getX(),
                friend.getY(),
                paint
        );
 
        //draw game Over when the game is over
        if(isGameOver){
            paint.setTextSize(150);
            paint.setTextAlign(Paint.Align.CENTER);
 
            int yPos=(int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
            canvas.drawText("Game Over",canvas.getWidth()/2,yPos,paint);
        }
        surfaceHolder.unlockCanvasAndPost(canvas);
    }
}

  • 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

1
2
3
4
5
6
7
8
//the score holder
int score;
 
//the high Scores Holder
int highScore[] = new int[4];
 
//Shared Prefernces to store the High Scores
SharedPreferences sharedPreferences;

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

1
2
3
4
5
6
7
8
9
10
//setting the score to 0 initially
score = 0;
 
sharedPreferences = context.getSharedPreferences("SHAR_PREF_NAME",Context.MODE_PRIVATE);
 
//initializing the array high scores with the previous values
highScore[0] = sharedPreferences.getInt("score1",0);
highScore[1] = sharedPreferences.getInt("score2",0);
highScore[2] = sharedPreferences.getInt("score3",0);
highScore[3] = sharedPreferences.getInt("score4",0);

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
private void update() {
    //incrementing score as time passes
    score++;
 
    player.update();
 
    //setting boom outside the screen
    boom.setX(-250);
    boom.setY(-250);
 
    for (Star s : stars) {
 
        s.update(player.getSpeed());
    }
 
    //setting the flag true when the enemy just enters the screen
    if(enemies.getX()==screenX){
        flag = true;
    }
 
    enemies.update(player.getSpeed());
    //if collision occurs with player
    if (Rect.intersects(player.getDetectCollision(), enemies.getDetectCollision())) {
 
        //displaying boom at that location
        boom.setX(enemies.getX());
        boom.setY(enemies.getY());
 
 
        //playing a sound at the collision between player and the enemy
 
 
        enemies.setX(-200);
    }// the condition where player misses the enemy
    else{
 
        //if the enemy has just entered
        if(flag){
 
         //if player's x coordinate is more than the enemies's x coordinate.i.e. enemy has just passed across the player
         if(player.getDetectCollision().exactCenterX()&gt;=enemies.getDetectCollision().exactCenterX()){
 
                //increment countMisses
                countMisses++;
 
 
                //setting the flag false so that the else part is executed only when new enemy enters the screen
                flag = false;
                //if no of Misses is equal to 3, then game is over.
                if(countMisses==3){
 
                    //setting playing false to stop the game.
                    playing = false;
                    isGameOver = true;
 
                    //Assigning the scores to the highscore integer array
                    for(int i=0;i&lt;4;i++){
                        if(highScore[i]&lt;score){
 
                            final int finalI = i;
                            highScore[i] = score;
                            break;
                        }
                    }
 
                    //storing the scores through shared Preferences
                    SharedPreferences.Editor e = sharedPreferences.edit();
                    for(int i=0;i&lt;4;i++){
                        int j = i+1;
                        e.putInt("score"+j,highScore[i]);
                    }
                    e.apply();
                }
            }
        }
    }
 
    //updating the friend ships coordinates
    friend.update(player.getSpeed());
    //checking for a collision between player and a friend
    if(Rect.intersects(player.getDetectCollision(),friend.getDetectCollision())){
 
        //displaying the boom at the collision
        boom.setX(friend.getX());
        boom.setY(friend.getY());
        //setting playing false to stop the game
        playing = false;
        //setting the isGameOver true as the game is over
        isGameOver = true;
 
        //Assigning the scores to the highscore integer array
        for(int i=0;i&lt;4;i++){
            if(highScore[i]&lt;score){
 
                final int finalI = i;
                highScore[i] = score;
                break;
            }
        }
 
        //storing the scores through shared Preferences
        SharedPreferences.Editor e = sharedPreferences.edit();
        for(int i=0;i&lt;4;i++){
            int j = i+1;
            e.putInt("score"+j,highScore[i]);
        }
        e.apply();
    }
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
private void draw() {
    if (surfaceHolder.getSurface().isValid()) {
        canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(Color.BLACK);
 
 
        paint.setColor(Color.WHITE);
        paint.setTextSize(20);
 
        for (Star s : stars) {
            paint.setStrokeWidth(s.getStarWidth());
            canvas.drawPoint(s.getX(), s.getY(), paint);
        }
 
        //drawing the score on the game screen
        paint.setTextSize(30);
        canvas.drawText("Score:"+score,100,50,paint);
 
        canvas.drawBitmap(
                player.getBitmap(),
                player.getX(),
                player.getY(),
                paint);
 
 
        canvas.drawBitmap(
                enemies.getBitmap(),
                enemies.getX(),
                enemies.getY(),
                paint
        );
 
 
        //drawing boom image
        canvas.drawBitmap(
                boom.getBitmap(),
                boom.getX(),
                boom.getY(),
                paint
        );
 
 
        //drawing friends image
        canvas.drawBitmap(
 
                friend.getBitmap(),
                friend.getX(),
                friend.getY(),
                paint
        );
 
        //draw game Over when the game is over
        if(isGameOver){
            paint.setTextSize(150);
            paint.setTextAlign(Paint.Align.CENTER);
 
            int yPos=(int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
            canvas.drawText("Game Over",canvas.getWidth()/2,yPos,paint);
        }
 
        surfaceHolder.unlockCanvasAndPost(canvas);
 
    }
}

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

activity_high_score.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_high_score"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:paddingTop="150dp"
    android:orientation="vertical"
    tools:context="net.simplifiedcoding.spacefighter.HighScore"
    android:background="@drawable/splash">
 
    <TextView
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="30dp"
        android:id="@+id/textView" />
 
    <TextView
        android:textColor="@android:color/white"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/textView2"
        />
 
    <TextView
        android:textColor="@android:color/white"
        android:gravity="center"
        android:textAlignment="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:id="@+id/textView3"
        />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textColor="@android:color/white"
        android:gravity="center"
        android:textAlignment="center"
        android:id="@+id/textView4"
        />
</LinearLayout>

 

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package net.simplifiedcoding.spacefighter;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
 
import org.w3c.dom.Text;
 
public class HighScore extends AppCompatActivity {
 
    TextView textView,textView2,textView3,textView4;
 
    SharedPreferences sharedPreferences;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high_score);
 
        //initializing the textViews
        textView = (TextView) findViewById(R.id.textView);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        textView4 = (TextView) findViewById(R.id.textView4);
 
        sharedPreferences  = getSharedPreferences("SHAR_PREF_NAME", Context.MODE_PRIVATE);
 
        //setting the values to the textViews
        textView.setText("1."+sharedPreferences.getInt("score1",0));
        textView2.setText("2."+sharedPreferences.getInt("score2",0));
        textView3.setText("3."+sharedPreferences.getInt("score3",0));
        textView4.setText("4."+sharedPreferences.getInt("score4",0));
 
 
    }
}

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

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package net.simplifiedcoding.spacefighter;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.Image;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
 
 
public class MainActivity extends Activity implements View.OnClickListener {
 
    // play image button
    private ImageButton buttonPlay;
    //high score button
    private ImageButton buttonScore;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //setting the orientation to landscape
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 
        //getting the button
        buttonPlay = (ImageButton) findViewById(R.id.buttonPlay);
 
        //initializing the highscore button
        buttonScore = (ImageButton) findViewById(R.id.buttonScore);
 
        //setting the on click listener to high score button
        buttonScore.setOnClickListener(this);
        //setting the on click listener to play now button
        buttonPlay.setOnClickListener(this);
    }
 
    // the onclick methods
    @Override
    public void onClick(View v) {
 
        if (v == buttonPlay) {
            //the transition from MainActivity to GameActivity
            startActivity(new Intent(MainActivity.this, GameActivity.class));
        }
        if (v == buttonScore) {
 
            //the transition from MainActivity to HighScore activity
            startActivity(new Intent(MainActivity.this, HighScore.class));
        }
 
 
    }
 
}

  • 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:-

1
2
3
4
//the mediaplayer objects to configure the background music
static MediaPlayer gameOnsound;
final MediaPlayer killedEnemysound;
final MediaPlayer gameOversound;

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

1
2
3
4
5
6
7
//initializing the media players for the game sounds
gameOnsound = MediaPlayer.create(context,R.raw.gameon);
killedEnemysound = MediaPlayer.create(context,R.raw.killedenemy);
gameOversound = MediaPlayer.create(context,R.raw.gameover);
 
//starting the game music as the game starts
gameOnsound.start();

  • 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
private void update() {
    //incrementing score as time passes
    score++;
 
    player.update();
 
    //setting boom outside the screen
    boom.setX(-250);
    boom.setY(-250);
 
    for (Star s : stars) {
        s.update(player.getSpeed());
    }
 
    //setting the flag true when the enemy just enters the screen
    if(enemies.getX()==screenX){
        flag = true;
    }
 
    enemies.update(player.getSpeed());
            //if collision occurs with player
            if (Rect.intersects(player.getDetectCollision(), enemies.getDetectCollision())) {
                //displaying boom at that location
                boom.setX(enemies.getX());
                boom.setY(enemies.getY());
 
                //playing a sound at the collision between player and the enemy
                killedEnemysound.start();
                enemies.setX(-200);
            }
 
            else{// the condition where player misses the enemy
                //if the enemy has just entered
                if(flag){
                    //if player's x coordinate is equal to enemies's y coordinate
                    if(player.getDetectCollision().exactCenterX()&gt;=enemies.getDetectCollision().exactCenterX()){
 
                        //increment countMisses
                        countMisses++;
 
                        //setting the flag false so that the else part is executed only when new enemy enters the screen
                        flag = false;
 
                        //if no of Misses is equal to 3, then game is over.
                        if(countMisses==3){
 
                            //setting playing false to stop the game.
                            playing = false;
                            isGameOver = true;
 
 
                            //stopping the gameon music
                            gameOnsound.stop();
                            //play the game over sound
                            gameOversound.start();
 
                            //Assigning the scores to the highscore integer array
                            for(int i=0;i&lt;4;i++){
                                if(highScore[i]&lt;score){
 
                                    final int finalI = i;
                                    highScore[i] = score;
                                    break;
                                }
                            }
 
                            //storing the scores through shared Preferences
                            SharedPreferences.Editor e = sharedPreferences.edit();
 
                            for(int i=0;i&lt;4;i++){
 
                                int j = i+1;
                                e.putInt("score"+j,highScore[i]);
                            }
                            e.apply();
 
                        }
 
                    }
                    }
 
            }
 
    //updating the friend ships coordinates
    friend.update(player.getSpeed());
            //checking for a collision between player and a friend
            if(Rect.intersects(player.getDetectCollision(),friend.getDetectCollision())){
 
                //displaying the boom at the collision
                boom.setX(friend.getX());
                boom.setY(friend.getY());
                //setting playing false to stop the game
                playing = false;
                //setting the isGameOver true as the game is over
                isGameOver = true;
 
                //stopping the gameon music
                gameOnsound.stop();
                //play the game over sound
                gameOversound.start();
 
                //Assigning the scores to the highscore integer array
                for(int i=0;i&lt;4;i++){
 
                    if(highScore[i]&lt;score){
 
                        final int finalI = i;
                        highScore[i] = score;
                        break;
                    }
                }
                //storing the scores through shared Preferences
                SharedPreferences.Editor e = sharedPreferences.edit();
 
                for(int i=0;i&lt;4;i++){
 
                    int j = i+1;
                    e.putInt("score"+j,highScore[i]);
                }
                e.apply();
            }
 
}

  • 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:-

1
2
3
4
//stop the music on exit
public static void stopMusic(){
    gameOnsound.stop();
}

  • 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:-

1
2
//context to be used in onTouchEvent to cause the activity transition from GameAvtivity to MainActivity.
Context context;

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

1
2
//initializing context
this.context = context;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
    switch (motionEvent.getAction() &amp; MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_UP:
            player.stopBoosting();
            break;
        case MotionEvent.ACTION_DOWN:
            player.setBoosting();
            break;
 
    }
    //if the game's over, tappin on game Over screen sends you to MainActivity
    if(isGameOver){
        if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
            context.startActivity(new Intent(context,MainActivity.class));
        }
    }
    return true;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
 
                    GameView.stopMusic();
                    Intent startMain = new Intent(Intent.ACTION_MAIN);
                    startMain.addCategory(Intent.CATEGORY_HOME);
                    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(startMain);
                    finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
 
}

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

Android Game Development Tutorial Source Code

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

Sharing is Caring:

  • Tweet
  • Share on Tumblr
  • More
  • Pocket
  • Print
  • Email

Related

Filed Under: Android Advance, Android Application Development Tagged With: android game development, android game development tutorial

About Manish Kumar

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.

Comments

  1. nani says

    December 30, 2016 at 7:21 am

    Good job thank you

    Reply
  2. Keith says

    February 3, 2017 at 9:08 pm

    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.

    Reply
    • Marcelo Kiilian Heinsberg says

      December 13, 2017 at 1:39 pm

      I’m starting now with games, how to fix this bug

      Reply
  3. Mikhael says

    March 27, 2017 at 6:44 am

    I want to ask a question. Do highscores reset when i close the app and open it again ?? or are they permanent

    Reply
    • Shoaib Ahmad says

      May 3, 2017 at 12:13 pm

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

      Reply
  4. Tarun Umath says

    June 16, 2017 at 5:35 am

    Please give more tutorial of gaming Application

    Reply
  5. damith says

    June 22, 2017 at 5:33 am

    thank you…keep it up

    Reply
  6. Ranveer says

    June 23, 2017 at 8:00 pm

    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

    Reply
  7. Aditya Jain says

    August 9, 2017 at 2:40 pm

    How can we add textview to GameView class
    Plz explain.

    Reply
  8. varun says

    September 17, 2017 at 10:49 am

    where highscores are stored is there a database???

    Reply
  9. shayne says

    September 23, 2017 at 3:38 pm

    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?

    Reply
  10. Saradhi says

    September 27, 2017 at 4:35 pm

    Thanks a lot for writing this tutorial. Really a good job. Keep writing.

    Reply
  11. Panji says

    January 11, 2018 at 9:40 pm

    Hello, thanks before. But can you reupload the finished apk, please?

    Reply
  12. AHMAD NAWAZ says

    January 27, 2018 at 8:19 am

    Excellent work

    Reply
  13. Marcia says

    February 14, 2018 at 4:47 am

    Awesome tutorial. Thank you so very much!

    Reply
  14. Tanu says

    February 25, 2018 at 1:44 pm

    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

    Reply
  15. manish says

    March 23, 2018 at 11:07 am

    Thanks for the tutorial really helpful,can you post some more tutorials on game

    Reply
  16. Mohit says

    March 28, 2018 at 8:29 am

    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 ?

    Reply
  17. Pearl says

    July 7, 2018 at 9:28 pm

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

    Reply
  18. ponez says

    July 12, 2018 at 8:29 am

    in frist tutorial in GameView.java you used
    for (int i = 0; i < enemyCount; i++) {
    enemies[i].update(player.getSpeed());
    in second tutorial it's removed
    ?

    Reply
  19. Sufiyan says

    September 9, 2018 at 6:11 pm

    works like charm, I got it in my first run, previous and this tutorial is awesome … you are great

    Reply
  20. Sufiyan says

    September 9, 2018 at 6:15 pm

    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.

    Reply
  21. Mike says

    October 15, 2018 at 1:52 am

    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 {
    jcenter()
    google()
    }
    dependencies {
    classpath ‘com.android.tools.build:gradle:3.2.1’

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

    allprojects {
    repositories {
    jcenter()
    google()
    maven { url “https://jitpack.io” }
    }
    }

    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!
    Mike

    Reply
  22. Atul says

    November 3, 2018 at 2:11 pm

    //if player’s x coordinate is equal to enemies’s y coordinate
    if(player.getDetectCollision().exactCenterX()>=enemies.getDetectCollision().exactCenterX())

    ERRORS:-
    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++){
    if(highScore[i]<score){

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

    ERRORS:-
    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

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_UP:
    player.stopBoosting();
    break;
    case MotionEvent.ACTION_DOWN:android.view.MotionEvent
    player.setBoosting();
    break;

    }

    ERRORS:-
    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

    Reply

Leave a Reply to Keith Cancel reply

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

Search




Download our Android App

Simplified Coding in Google Play

About Me

Belal Khan

Hello I am Belal Khan, founder and owner of Simplified Coding. I am currently pursuing MCA from St. Xavier's College, Ranchi. I love to share my knowledge over Internet.

Connect With Me

Follow @codesimplified
Simplified Coding

Popular Tutorials

  • Android JSON Parsing – Retrieve From MySQL Database
  • Android Login and Registration Tutorial with PHP MySQL
  • Android Volley Tutorial – Fetching JSON Data from URL
  • Android Upload Image to Server using Volley Tutorial
  • Android TabLayout Example using ViewPager and Fragments
  • Retrieve Data From MySQL Database in Android using Volley
  • Firebase Cloud Messaging Tutorial for Android
  • Android Volley Tutorial – User Registration and Login
  • Android Upload Image to Server Using PHP MySQL
  • Android Navigation Drawer Example using Fragments




About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap

Quick Links

  • Advertise Here
  • Privacy Policy
  • Disclaimer
  • About
  • Contact Us
  • Write for Us

Categories

Android Advance Android Application Development Android Beginners Android Intermediate Ionic Framework Tutorial JavaScript Kotlin Android Others PHP Advance PHP Tutorial React Native

Copyright © 2017 · Simplified Coding· All rights Reserved. And Our Sitemap.All Logos & Trademark Belongs To Their Respective Owners·

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.