遊戲設計思路:
主界面點擊開始遊戲:進入打地鼠界面遊戲中有12個地洞,遊戲時間爲30s(可以自己設置),每0.5s會有地鼠隨機出現在一個地洞中,玩家觸摸屏幕,打到地鼠加10分,否則不加分。30s後遊戲結束,彈出窗口顯示獲得分數,需要玩家輸入姓名後,點擊確定保存到本地數據庫中。
設計實現:每個地洞爲一個ImageButton,開始設置背景爲地洞圖片,地鼠出現則設置爲地鼠圖片,給每個按鈕添加點擊事件,當玩家點擊按鈕時,如果打到地鼠,該按鈕設置打中地鼠圖片,否則設置沒打中地鼠的圖片。遊戲結束開啓記錄窗口,記錄玩家信息。
主界面點擊排行榜:如果沒有記錄,提示暫無排行,有記錄就跳轉界面,按分數從高到低顯示玩家信息。
設計實現:通過對數據庫的查詢操作,返回一個ArrayList,如果ArrayList長度爲0,則提示“暫無排行”,否則開啓一個新的Activity顯示玩家信息。
主界面點擊關於:顯示遊戲的相關信息。
設計實現:Activity跳轉。
主界面點擊退出:遊戲退出
設計實現:調用finish()函數。
主界面點擊音樂圖標:遊戲打開默認播放音樂,點擊圖標背景音樂和音效會關閉,再次點擊會播放背景音樂和音效。
設計實現:一個ToggleButton(開關按鈕)背景設置成音樂圖標,點擊會觸發響應事件。
按物理返回鍵遊戲停止,在onDestroy()方法中做釋放資源等操作。
注:遊戲中所寫的Activity繼承BaseActivity,自己實現的一個繼承Activity的類。那麼爲什麼要實現這麼一個類呢?在遊戲的後期添加音效時,程序進入後臺,背景音樂會一直播放,因爲背景音樂在所有的Activity中都會播放,所以要在每個Activity的生命週期的回調函數中對音樂操作無疑是比較麻煩的,所以繼承自一個我們自己實現的BaseActivity,只需要在BaseActivity中來操作即可。
BaseActivity的代碼如下:
package cn.com.cyj.mouse.ui;
import android.app.Activity;
import android.os.Bundle;
/**
*
* @author cyj
*
*/
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();
}
}
<span style="white-space:pre"> </span>// 進入後臺時系統調用
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
// 如果音樂在播放就暫停
if (MouseStart.controller.isPlay()) {
MouseStart.controller.pauseMusic();
isLive = true;
} else {
isLive = false;
}
}
}
主界面代碼如下: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;
/**
* 遊戲主界面
*
* @author cyj
*
*/
public class MouseStart extends BaseActivity {
// 開始遊戲按鈕
ImageButton start;
// 排行榜按鈕
ImageButton rank;
// 關於按鈕
ImageButton about;
// 退出按鈕
ImageButton exit;
// 音樂開關
ToggleButton music;
Intent intent;
// 只有能一個controller定義成靜態共別的Activity調用,之前在BaseActivity創建controller的對象每個子類都會有一個controller,出現問題
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:
// 進入開始遊戲Activity
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:
// 打開關於Activity
intent = new Intent(MouseStart.this, About.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(intent);
break;
case R.id.exit:
// 關閉Activity
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() {
// TODO Auto-generated method stub
super.onDestroy();
controller.close();
}
}
順便貼一下About中的代碼:這個類比較簡單直接加載對應的xml文件就可以,一些介紹的話在xml中寫。
package cn.com.cyj.mouse.ui;
import android.os.Bundle;
import cn.com.cyj.mouse.R;
/**
* 關於界面
* @author cyj
*
*/
public class About extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
}