Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android 遊戲開發實作:打地鼠(二)玩法設計與主畫面

編輯:Android 遊戲開發

有了需求,接下來就是把每個功能翻譯成具體的 Android 元件。這篇把設計決策攤開來講,並附上 BaseActivity、主畫面與 About 三個類別的程式碼。

玩法與畫面的對應關係

開始遊戲:主畫面點擊後進入打地鼠畫面。場景有 12 個地洞,時間 30 秒(可自行調整),每 0.5 秒有一隻地鼠隨機出現在某個洞,玩家點擊打中加 10 分,沒打中不加分。時間到跳出一個視窗顯示分數,玩家輸入姓名後按確定,存進本機資料庫。

實作上,每個地洞就是一個 ImageButton:預設背景是地洞圖,地鼠出現時換成地鼠圖,每個按鈕都掛同一個點擊事件。點擊後依結果換上「打中」或「沒打中」的圖片。遊戲結束再開啟記錄視窗收集玩家資料。

排行榜:查詢資料庫並取回一個 ArrayList。若長度為 0 就提示「暫無排行」,否則跳轉到新 Activity,依分數由高到低列出玩家。

關於:單純的 Activity 跳轉,介紹文字寫在 XML 裡。

退出:呼叫 finish()。

音樂圖示:App 啟動時預設播放背景音樂,點一下圖示把背景音樂與音效關掉,再點一下恢復。實作方式是拿一個 ToggleButton,背景設成音樂圖示,並傾聽它的點擊事件。

返回鍵:按下實體返回鍵結束遊戲,在 onDestroy() 中釋放資源。

為什麼要自己寫一個 BaseActivity

遊戲裡所有 Activity 都繼承自 BaseActivity,而 BaseActivity 只是簡單地繼承 Activity。原因出在音效:背景音樂在所有畫面都會播放,一旦 App 跑到背景卻沒暫停,音樂就會繼續響。如果每個 Activity 都要在自己的生命週期回呼裡處理一次音樂,邏輯會散落在四、五個地方,日後改動非常痛苦。把這段邏輯集中到 BaseActivity 的 onUserLeaveHint() 與 onResume(),子類別就完全不必操心。

順帶一提,Controller 必須宣告成 static,否則每個子類別各持有 independently 建立的一一份,音樂狀態就會互相打架。

BaseActivity

package cn.com.cyj.mouse.ui;

import android.app.Activity;
import android.os.Bundle;

public class BaseActivity extends Activity {

    // 音樂播放標記
    protected Boolean isLive = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 若進入背景前音樂正在播放,回到前台就繼續播
        if (isLive) {
            MouseStart.controller.continuePlay();
        }
    }

    // 進入背景時由系統呼叫
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
        // 正在播放就暫停,並記下狀態
        if (MouseStart.controller.isPlay()) {
            MouseStart.controller.pauseMusic();
            isLive = true;
        } else {
            isLive = false;
        }
    }
}

主畫面 MouseStart

package cn.com.cyj.mouse.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import cn.com.cyj.mouse.R;
import cn.com.cyj.mouse.controller.Controller;
import cn.com.cyj.mouse.services.GameRun;

/** 遊戲主畫面 */
public class MouseStart extends BaseActivity {

    ImageButton start;      // 開始遊戲
    ImageButton rank;       // 排行榜
    ImageButton about;      // 關於
    ImageButton exit;       // 退出
    ToggleButton music;     // 音樂開關
    Intent intent;

    // 只有 Controller 需要被其他 Activity 共用,因此宣告為 static
    public static Controller controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gamestart);
        controller = new Controller(this);

        // 初始化各按鈕
        start = (ImageButton) findViewById(R.id.startgame);
        rank = (ImageButton) findViewById(R.id.rank);
        about = (ImageButton) findViewById(R.id.about);
        exit = (ImageButton) findViewById(R.id.exit);
        music = (ToggleButton) findViewById(R.id.musical);

        // 掛上點擊與觸摸事件
        GameStartOnClick game = new GameStartOnClick();
        start.setOnClickListener(game);  start.setOnTouchListener(game);
        rank.setOnClickListener(game);   rank.setOnTouchListener(game);
        about.setOnClickListener(game);  about.setOnTouchListener(game);
        exit.setOnClickListener(game);   exit.setOnTouchListener(game);
        music.setOnClickListener(game);

        // 預設播放背景音樂
        controller.play();
    }

    class GameStartOnClick implements OnClickListener, OnTouchListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.startgame:
                    // 進入遊戲畫面
                    intent = new Intent(MouseStart.this, GameRun.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
                    MouseStart.this.startActivity(intent);
                    break;

                case R.id.rank:
                    // 查詢全部玩家;查無資料就提示
                    if (!controller.query()) {
                        Toast.makeText(MouseStart.this, "暫無排行", Toast.LENGTH_SHORT).show();
                    }
                    break;

                case R.id.about:
                    intent = new Intent(MouseStart.this, About.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
                    startActivity(intent);
                    break;

                case R.id.exit:
                    finish();
                    break;

                case R.id.musical:
                    if (music.isChecked()) {
                        controller.stop();
                    } else {
                        controller.play();
                    }
                    break;

                default:
                    break;
            }
        }

        /** 讓按鈕在按下與抬起時換圖,做出回饋感 */
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int id = v.getId();
            switch (id) {
                case R.id.startgame:
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        start.setBackgroundResource(R.drawable.startgamean);
                    }
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        start.setBackgroundResource(R.drawable.startgame);
                    }
                    break;

                case R.id.rank:
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        rank.setBackgroundResource(R.drawable.rankan);
                    }
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        rank.setBackgroundResource(R.drawable.rank);
                    }
                    break;

                case R.id.about:
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        about.setBackgroundResource(R.drawable.aboutan);
                    }
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        about.setBackgroundResource(R.drawable.about);
                    }
                    break;

                case R.id.exit:
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        exit.setBackgroundResource(R.drawable.exitan);
                    }
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        exit.setBackgroundResource(R.drawable.exit);
                    }
                    break;

                default:
                    break;
            }
            return false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        controller.close();
    }
}

關於畫面 About

這個類別最單純,載入對應的 XML 版面即可,介紹文字直接寫在 XML 裡。

package cn.com.cyj.mouse.ui;

import android.os.Bundle;
import cn.com.cyj.mouse.R;

/** 關於畫面 */
public class About extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }
}

值得留意的是 onTouch 回傳 false:這樣事件才會繼續往下傳給 onClick,兩者不會互相吃掉。若回傳 true,點擊事件就會失效——這是初學者常踩的坑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved