Hands-on teaching of Android games--Steam Wars mini-game (there is code at the end of the article)

        

Table of contents

1.1 The purpose of course design

1.2 Content requirements of this topic

1.3 Software development and operating environment

2.1 Design ideas

2.2 The overall structure of the software

2.3 Design of main functional modules

3.1 Start interface module

3.1.1 Entering Game Design

3.1.2 Exit Game Design

3.1.3 The main code of the start interface

3.2 Game main interface display module

3.2.1 Game interface design

3.2.2 Design of game interface torpedoes, bombs and ships

3.2.3 The main code of the game interface

3.3 Game button module design

3.3.1 Game start button function design

3.3.2 Function design of game pause button

3.3.3 Game rule button function design

3.3.4 Game restart button function design

3.3.5 Game exit button function design

3.3.6 Game continue button function design


1. Introduction

1.1 The purpose of course design

        Ship war game is a classic shooting game, players need to destroy the enemy and get high scores by controlling the battleship. This course is designed to help students master the use of Android Studio development tools, gain a deep understanding of the development process of the Android platform, and improve students' programming ability and practical experience through the development of the Ship War game.

1.2 Content requirements of this topic

        Game interface: The game interface should be aesthetically pleasing and user-friendly, able to attract players' attention and provide a good gaming experience.

        Gameplay: The gameplay should be easy to understand and easy to pick up, yet challenging enough for players to experience the fun and excitement of the game.

        Gameplay: Gameplay should be straightforward and easy to master without tiring or irritating the player.

       Game rules: The rules of the game should be clear and simple, so that players can clearly know how to score, how to win, how to lose, etc.

1.3 Software development and operating environment

The software development platform: Windows10 system

The software integrated development environment: Android studio

The software operating environment: Windows 7 or above

2. Overall design

2.1 Design ideas

        Game interface design: The game interface should have good visual effects and user experience to achieve smoother game effects and higher game performance. The game interface should focus on aesthetics and user experience to ensure the playability and fun of the game.

        Game play design: The game play should be simple and easy to understand, easy to use, and at the same time have enough challenges to allow players to experience the fun and excitement of the game.

        Game operation design: The game operation should be simple and clear. Players can control their ships to move and attack through the screen.

        Code quality design: The code should have good readability and maintainability, and conform to the norms and standards of Android development. The code should focus on code reusability and modular design to make the code easier to maintain and expand.

2.2 The overall structure of the software

Figure 2-1 The overall structure of the ship battle

2.3 Design of main functional modules

  1. start interface module

           Module layout: The implementation of this module is to enter the game interface and exit the game interface. Click to enter the game interface, and the game scene will be drawn, as well as score, level, life, etc.

  1. Game main interface display module

         The layout of the main interface: from top to bottom are the sky, player ships and seabed, and randomly generated enemy ships.

  1. Game button module design

        Game button module layout: they are the start button and the exit button respectively. If the start button is clicked, the game starts, and the text of the start button becomes a pause button. If you continue to click the pause button, a dialog box will pop up, displaying the game to continue, game rules, restart Start and exit the game.

3. Detailed design and implementation

3.1 Start interface module

3.1.1 Entering Game Design

        The game start interface module mainly includes game entry and exit game buttons, which are convenient for players to enter and exit the game.

3.1.2 Exit Game Design

        The main interface of starting the game is shown in the figure below. In the main interface, the parameters of the buttons have been adjusted to make it more beautiful. The overall layout adopts vertical arrangement.

Figure 3-1 Start interface diagram

3.1.3 The main code of the start interface

(1) The click event of the button to enter the game and exit the game  

 public void onClick(View v) {
         switch (v.getId()){
            case R.id.start_btn:
                //跳转界面GameActivity
                startActivity(new  
Intent(MainActivity.this,GameActivity.class));
                //当您的活动完成并应该关闭时调用此函数。
                finish();
                break;
            case R.id.exit_btn:
                System.exit(0);
                break;
           }
         }

3.2 Game main interface display module

3.2.1 Game interface design

 

        The top left is the display of score, level and life, and the top right is the display of the start and exit buttons, realizing the function of starting and exiting the game. Below are the sky, ships, enemy ships and bombs to realize the scene display and give users a more visual experience. There are three buttons at the bottom: move left, move right and shoot, which are convenient for users to operate.

3.2.2 Design of game interface torpedoes, bombs and ships

1. Torpedo design: The torpedo in the ship battle is sent by the player. The player can use the torpedo to fight the enemy warship. The number of torpedoes will be every 2s. When the player launches a torpedo and hits the enemy warship, the enemy warship will explode. Players score points, and as they accumulate, levels increase.

Figure 3-3 Torpedo design

2. Score, level, and life design: In the ship battle, players can destroy enemy ships by launching torpedoes, and the level will increase.

 

Figure 3-4 Score, grade and life design diagram

    3. The player's ship design diagram, used to display the player's ship:

 Figure 3-5 Player Ship Design Drawing

       4. Design drawing of left, right and launch buttons: Players can control the ship by moving left or right to avoid damage from bombs, and attack enemy ships through the launch button to get points and level up.

 Figure 3-6 Design diagram of the left, right and launch buttons

       5. Design drawing of start and exit buttons: Players can exit the game by clicking the exit button. It is also possible to start the game by clicking the start button. When the player clicks the start button, the text of the start button changes to Pause and the game starts.

 Figure 3-7 Start and exit button design

3.2.3 The main code of the game interface

//绘制
@Override
protected void onDraw(Canvas canvas) {
    //绘制天空
    Rect say = new Rect(0,0,width,420);
    colors = new int[]{Color.parseColor("#FF5ED6C2"),Color.parseColor("#FFC1EFE8"),Color.WHITE};
    GradientDrawable mDrawable1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,colors);
    mDrawable1.setShape(GradientDrawable.RECTANGLE);
    mDrawable1.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    //设置矩形绘制区域为say
    mDrawable1.setBounds(say);
    mDrawable1.draw(canvas); //绘制
    //绘制海洋
    Rect sea = new Rect(0,420,width,height);
    colors = new int[]{Color.parseColor("#FF3C60D5"),Color.BLUE};
    GradientDrawable mDrawable2 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,colors);
    mDrawable2.setShape(GradientDrawable.RECTANGLE); //形状
    mDrawable2.setGradientType(GradientDrawable.LINEAR_GRADIENT); //渐变
    mDrawable2.setBounds(sea); //边界框
    mDrawable2.draw(canvas);
    //绘制数字
    paint.setColor(Color.YELLOW);
    paint.setTextSize(100f); //设置字体大小
    //285-255
    //分别画
    canvas.drawText(String.valueOf(score),305,155,paint);
    canvas.drawText(String.valueOf(pass),675,155,paint);
    canvas.drawText(String.valueOf(liveNum),1050,155,paint);
    //绘制玩家船舰
    ship.drawShip(canvas);
    drawBombNum(canvas);
    /*
        游戏触发事件检测
     */
    //炸弹
    if(!bumbArray.isEmpty()){
        for(int i = 0;i < bumbArray.size();i++){
            if(!bumbArray.get(i).flag){
                bumbArray.get(i).drawBumb(canvas);
            }
            if(bumbArray.get(i).flag){
                bumbArray.remove(i);
            }
        }
    }
    //爆炸
    if(!blastArray.isEmpty()) {
        for(int i = 0; i < blastArray.size(); i++)
        {
            if(!blastArray.get(i).isFlag()) {
                blastArray.get(i).drawBlast(canvas);
            }
            if(blastArray.get(i).isFlag()) {
                blastArray.remove(i);
            }
        }
    }
    //鱼雷
    if(!torpedoArray.isEmpty()){
        for(int i = 0; i < torpedoArray.size(); i ++) {
            if(!torpedoArray.get(i).isFlag()) {
                torpedoArray.get(i).drawTorpedo(canvas);
            }
            if(torpedoArray.get(i).isFlag()) {
                torpedoArray.remove(i);
            }
        }
    }
    //潜艇
    if(!submarineArray.isEmpty()){
        for(int i = 0; i < submarineArray.size(); i ++) {
            if(!submarineArray.get(i).isFlag()) {
                submarineArray.get(i).drawSubmarine(canvas);
            }
            if(submarineArray.get(i).isFlag()) {
                submarineArray.remove(i);
            }
        }
    }
    //有效攻击
    if(!hitArray.isEmpty()){
        for(int i = 0; i < hitArray.size(); i ++) {
            if(!hitArray.get(i).isRunning()) {
                hitArray.get(i).drawHitting(canvas);
            }
            if(hitArray.get(i).isRunning()) {
                hitArray.remove(i);
            }
        }
    }
}

//绘制指定数量炸弹
public void drawBombNum(Canvas canvas) {
    Bitmap bomb= BitmapFactory.decodeResource(getContext().getResources(), R.drawable.bomb);
    for(int i = 0;i < ship.getBombNum();i ++) {
        canvas.drawBitmap(bomb,width/2+100 + 50*i + 70, 75,null);
    }
}

3.3 Game button module design

3.3.1 Game start button function design

        When the player clicks the start button: the text of the start button is replaced with pause, the game starts immediately, enemy ships and bombs are randomly generated, and the number of player torpedoes increases until the maximum number of torpedoes is 5.

The main code of the game start button:

public void onClick(View v) {
    switch (v.getId()){
        case R.id.pause_text:
            if(gameText.getText().equals("开始")){
                startGame();
            }else{
                //不是开始,置标志为false,暂停
                //绘制暂停界面
                gameView.getShip().setRunning(false);
                showPauseDialog();
            }
            break;
        case R.id.exit_text:
            System.exit(0);
            break;
        case R.id.left_btn:
            gameView.getShip().moveLeft();
            //船左移动,重新绘制界面
            gameView.invalidate();
            break;
        case R.id.right_btn:
            gameView.getShip().moveRight();
            gameView.invalidate();
            break;
        case R.id.shoot_btn:
            //获取船的子弹数量
            int bombNun = gameView.getShip().getBombNum();
            //判断开始游戏
            if(isRunning){
                if( bombNun> 0 && bombNun <= 5) { //子弹判断
                    //新建子弹类
                    Bumb bumb = new  

Bumb(GameActivity.this,gameView.getShip(),gameView);
                    bumbArray.add(bumb);
                    gameView.setBumbArray(bumbArray);
                    Thread t1 =  new Thread(bumb); //创建线程
                    t1.start(); //启动
                    bombNun -- ; //发射子弹后,对数量 - 1
                    gameView.getShip().setBombNum(bombNun); //重新设置子弹数量
                }
            }
            break;
    }
}
//游戏运行
private void startGame(){
    //初始未点击开始按钮状态
    if(isRunning){
        isRunning = false;
        gameView.setLiveNum(3);
        gameView.setPass(1);
        gameView.setScore(0);
        tm1.setSpeed(1000);
        gameView.getShip().setBombNum(5);
        isRunning = true;
        gameView.getShip().setRunning(true);
        for(int i = 0; i < gameView.getBumbArray().size(); i++)
        {
            ((Bumb) gameView.getBumbArray().get(i)).flag = true;
        }
        for(int i = 0; i <  gameView.getSubmarineArray().size(); i++)
        {
            ((Submarine)gameView.getSubmarineArray().get(i)).setFlag(true);
        }
        for(int i = 0; i <gameView.getTorpedoArray().size(); i++)
        {
            ((Torpedo)gameView.getTorpedoArray().get(i)).setFlag(true);
        }
    }
    //开始游戏
    if(!isRunning){
        gameText.setText("暂停");
        gameView.getShip().setRunning(true);
        isRunning = true;
        tm1 = new TimeManager(gameView.getShip(),GameActivity.this,submarine,gameView);
        Thread t = new Thread(tm1);
        t.start();
        for(int i = 0; i < gameView.getBumbArray().size(); i++)
        {
            gameView.getBumbArray().get(i).flag = true;
            gameView.getBumbArray().remove(i);
        }
        for(int i = 0; i < gameView.getSubmarineArray().size(); i++)
        {
            gameView.getSubmarineArray().get(i).setFlag(true);
            gameView.getSubmarineArray().remove(i);
        }
        for(int i = 0; i < gameView.getTorpedoArray().size(); i++)
        {
            gameView.getTorpedoArray().get(i).setFlag(true);
            gameView.getTorpedoArray().remove(i);
        }
    }
}

3.3.2 Function design of game pause button

       When the player clicks the pause button: the main interface ships and bombs stop moving, the page is still, a dialog box pops up, and the display picture is as follows.

 

Figure 3-8 Pause button function design interface

The main code of the game pause button:

//绘制暂停界面
private void showPauseDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
    final AlertDialog dialog = builder.create();
    dialog.setCancelable(false);
    //动态加载布局
    View view = View.inflate(GameActivity.this, R.layout.pausedialog, null);
    Button rule = (Button)view.findViewById(R.id.rule);
    Button contiueBtn = (Button) view.findViewById(R.id.contiue);
    rule.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showRuleDialog();
            dialog.dismiss();
        }
    });

3.3.3 Game rule button function design

When the player clicks the game rules button: a dialog game rules pops up, as shown in the figure below.

Figure 3-9 Game rule button function design diagram

The main code of the game rule button function:

//绘制暂停界面
private void showPauseDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
    final AlertDialog dialog = builder.create();
    dialog.setCancelable(false);
    //动态加载布局
    View view = View.inflate(GameActivity.this, R.layout.pausedialog, null);
    Button rule = (Button)view.findViewById(R.id.rule);
    Button contiueBtn = (Button) view.findViewById(R.id.contiue);
    rule.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showRuleDialog();
            dialog.dismiss();
        }
    });
    //继续

3.3.4 Game restart button function design

When the player clicks the restart button: player score, level, life, reset, game reload starts.

The main code of the restart button function:

//重新开始
Button again = (Button)view.findViewById(R.id.about);
again.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startGame();
        synchronized(GameActivity.subLock) {
            GameActivity.subLock.notifyAll();
        }
        dialog.dismiss();
        gameView.invalidate();
    }
});

3.3.5 Game exit button function design

When the player clicks the exit button: the game exits. If you want to play the game again, just restart it.

The main code of the game exit button function:

//退出
Button exit =(Button)view.findViewById(R.id.tuichu);
exit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        System.exit(0);
    }
});

3.3.6 Game continue button function design

When the player clicks the continue button: the game continues and the dialog is closed.

Figure 3-10 Continue button function design diagram

The main code of the game continue button function:

//继续
contiueBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        gameText.setText("暂停");
        gameView.getShip().setRunning(true);

        synchronized(GameActivity.subLock) {
            //线程同步
            GameActivity.subLock.notifyAll();
        }
        dialog.dismiss();
    }
});

The code is posted on my homepage

Guess you like

Origin blog.csdn.net/m0_65635427/article/details/131213743