游戏的基本功能都已经实现了,最后来说一说排行榜的显示和游戏音效的添加。
排行榜的显示主要用的Android中一个比较重要的控件ListView。ListView的使用还是比较简单的,第一步在布局文件中建立一个ListView的节点,在代码中通过ID得到该控件。第二步给该控件设置一个适配器,适配器写一个类,该类继承BaseAdapter并实现未实现的方法,一共有4个为实现的方法,getCount()获得数据总数,getItem(int position)根据位置获得某条数据,getItemId(int position)根据位置获得某条数据id,getView(),得到相应位置的Item视图。可以通过contentView对ListView进行优化,如果contentView为空,通过inflate填充view,否则不填充,这样减少了填充次数,提高了效率。代码如下:
Java代码
- package cn.com.cyj.mouse.ui;
-
- import java.util.ArrayList;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import cn.com.cyj.mouse.R;
- import cn.com.cyj.mouse.enity.Gamer;
-
-
-
-
-
-
-
- public class ShowRank extends BaseActivity {
-
- ListView lv;
- ArrayList<Gamer> gamerList;
- TextView gamerName;
- TextView gamerScore;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_showrank);
-
- gamerList = new ArrayList<Gamer>();
- Intent intent = getIntent();
- gamerList = (ArrayList<Gamer>) intent.getSerializableExtra("gamerlist");
-
- lv = (ListView) findViewById(R.id.lv);
-
- lv.setAdapter(new MyAdapter());
- }
-
- class MyAdapter extends BaseAdapter {
-
-
- @Override
- public int getCount() {
- return gamerList.size();
- }
-
-
- @Override
- public Object getItem(int position) {
- return null;
- }
-
-
- @Override
- public long getItemId(int position) {
-
- return 0;
- }
-
- @Override
- public View getView(int position, View contentView, ViewGroup parent) {
- View v = contentView;
- if (v == null) {
-
- v = View.inflate(ShowRank.this, R.layout.list_item, null);
- gamerName = (TextView) v.findViewById(R.id.gamername);
- gamerScore = (TextView) v.findViewById(R.id.gamerscore);
- }
-
- Gamer gamer = gamerList.get(position);
- gamerName.setText(gamer.getName());
- gamerScore.setText(gamer.getScore() + "");
- return v;
- }
-
- }
- }
一个游戏没有音效无可厚非,但是有了音效会更有乐趣。本游戏采用了MediaPlayer+SoundPool的形式,前者播放背景音乐,后者播放游戏的打击音效,关于MediaPlayer的使用,没有什么比官方的图来的更简单粗暴了,这里不再赘述。SoundPool的使用,第一步new SoundPool();第二步load(),通常用一个HashMap存放音乐文件id和load的映射;第三步play()。代码如下:
Java代码
- package cn.com.cyj.mouse.services;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import android.content.Context;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.media.SoundPool;
- import cn.com.cyj.mouse.R;
-
-
-
-
-
- public class MusicService {
-
-
- MediaPlayer player;
-
- SoundPool pool;
- Context context;
-
- Map<Integer, Integer> soundMap;
- public MusicService(Context context) {
- this.context = context;
- initMedia();
- initSound();
- }
- private void initSound() {
- pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
- soundMap = new HashMap<Integer, Integer>();
-
- soundMap.put(R.raw.dismistake, pool.load(context, R.raw.dismistake, 1));
- soundMap.put(R.raw.hathit, pool.load(context, R.raw.hathit, 1));
- }
-
-
-
-
- public void playSound(int resId){
-
- Integer soundId = soundMap.get(resId);
- if(soundId != null){
- pool.play(soundId, 1, 1, 1, 0, 1);
- }
- }
- private void initMedia(){
-
- player = MediaPlayer.create(context, R.raw.bg);
-
- player.setLooping(true);
- }
- public void play() {
-
- player.start();
- }
-
-
-
- public void stop() {
- if (player.isPlaying()) {
- player.stop();
- try {
- player.prepare();
-
- player.seekTo(0);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- }
-
-
-
- public void pause(){
- if(player.isPlaying()){
- player.pause();
- }
- }
-
-
-
- public void close() {
- if(player == null)
- return ;
- if(player.isPlaying()){
-
- player.stop();
- }
-
- player.release();
- player = null;
- }
-
-
-
- public void continuePlay() {
- player.start();
- }
-
-
-
-
- public Boolean isNowPlay(){
- if(player.isPlaying()){
- return true;
- }
- return false;
- }
- }
游戏中的控制类,该类处理玩家对界面的操作和游戏响应,把界面操作和游戏逻辑分开设计,降低耦合性。代码如下: