세상이야기

비행기게임 sample

쿠카곰돌이 2022. 7. 15. 23:42
반응형

https://www.simplifiedcoding.net/android-game-development-tutorial-2/ 

 

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

www.simplifiedcoding.net

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 Friend.java and add the following code.
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
 
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.
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
186
187
 
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.
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
 
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
12
13
 
//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
6
7
 
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
62
63
 
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
56
57
 
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.

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
9
10
 
//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
11
12
 
//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
110
111
 
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()>=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<4;i++){
                        if(highScore[i]<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<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<4;i++){
            if(highScore[i]<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<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
65
66
 
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:-

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

 

  • 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
39
40
 
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:-
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
 
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
5
6
 
//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
8
9
 
//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
124
125
 
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()>=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<4;i++){
                                if(highScore[i]<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<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<4;i++){
 
                    if(highScore[i]<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<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
5
6
 
//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
3
4
 
//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
3
4
 
//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
20
21
 
@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;
 
    }
    //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
26
27
 
@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.
반응형