疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

android开发—连连看开发技巧(6)


 

android游戏开发之连连看(6)

实现游戏Activity

前面已经给出了游戏界面的布局文件,该布局文件需要使用一个Activity来负责显示,除此之外,Activity还需要为游戏界面的按钮、GameView组件的事件提供事件监听器。

尤其是对于GameView组件,程序需要监听用户的触碰动作,当用户触碰屏幕时,程序需要获取用户触碰的是哪个方块,并判断是否需要“消除”该方块。为了判断能否消除该方块,程序需要进行如下判断:

q  如果程序之前已经选中了某个方块,那就判断当前触碰的方块是否能与之前的方块“相连”,如果可以相连,消除两个方块;如果两个方块不可以相连,那把当前方块设置选中方块。

q  如果程序之前没有选中方块,直接将当前方块设置选中方块。

下面是该程序的Activity的代码:

程序清单:codes18LinksrcorgcrazyitlinkLink.java

public class Link extends Activity

{

         // 游戏配置对象

         private GameConf config;

         // 游戏业务逻辑接口

         private GameService gameService;

         // 游戏界面

         private GameView gameView;

         // 开始按钮

         private Button startButton;

         // 记录剩余时间的TextView

         private TextView timeTextView;

         // 失败后弹出的对话框

         private AlertDialog.Builder lostDialog;

         // 游戏胜利后的对话框

         private AlertDialog.Builder successDialog;

         // 定时器

         private Timer timer = new Timer();

         // 记录游戏的剩余时间

         private int gameTime;

         // 记录是否处于游戏状态

         private boolean isPlaying;

         // 振动处理类

         private Vibrator vibrator;

         // 记录已经选中的方块

         private Piece selected = null;

         private Handler handler = new Handler()

         {

                   public void handleMessage(Message msg)

                   {

                            switch (msg.what)

                            {

                                     case 0x123:

                                               timeTextView.setText("剩余时间: " + gameTime);

                                               gameTime--;

                                               // 时间小于0, 游戏失败

                                               if (gameTime < 0)

                                               {

                                                        stopTimer();

                                                        // 更改游戏的状态

                                                        isPlaying = false;

                                                        lostDialog.show();

                                                        return;

                                               }

                                             break;

                            }

                   }

         };

         @Override

         public void onCreate(Bundle savedInstanceState)

         {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.main);

                   // 初始化界面

                   init();

         }

         // 初始化游戏的方法

         private void init()

         {

                   config = new GameConf(8, 9, 2, 10 , 100000, this);

                   // 得到游戏区域对象

                   gameView = (GameView) findViewById(R.id.gameView);

                   // 获取显示剩余时间的文本框

                   timeTextView = (TextView) findViewById(R.id.timeText);

                   // 获取开始按钮

                   startButton = (Button) this.findViewById(R.id.startButton);

                   // 获取振动器

                   vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

                   gameService = new GameServiceImpl(this.config);

                   gameView.setGameService(gameService);

                   // 为开始按钮的单击事件绑定事件监听器

                   startButton.setOnClickListener(new View.OnClickListener()

                   {

                            @Override

                            public void onClick(View source)

                            {

                                     startGame(GameConf.DEFAULT_TIME);

                            }

                   });

                   // 为游戏区域的触碰事件绑定监听器

                   this.gameView.setOnTouchListener(new View.OnTouchListener()

                   {

                            public boolean onTouch(View view, MotionEvent e)

                            {

                                     if (e.getAction() == MotionEvent.ACTION_DOWN)

                                     {

                                               gameViewTouchDown(e);

                                     }

                                     if (e.getAction() == MotionEvent.ACTION_UP)

                                     {

                                               gameViewTouchUp(e);

                                     }

                                     return true;

                            }

                   });

                   // 初始化游戏失败的对话框

                   lostDialog = createDialog("Lost", "游戏失败! 重新开始", R.drawable.lost)

                            .setPositiveButton("确定", new DialogInterface.OnClickListener()

                            {

                                     public void onClick(DialogInterface dialog, int which)

                                     {

                                               startGame(GameConf.DEFAULT_TIME);

                                     }

                            });

                   // 初始化游戏胜利的对话框

                   successDialog = createDialog("Success", "游戏胜利! 重新开始",

                            R.drawable.success).setPositiveButton("确定",

                            new DialogInterface.OnClickListener()

                            {

                                     public void onClick(DialogInterface dialog, int which)

                                     {

                                               startGame(GameConf.DEFAULT_TIME);

                                     }

                            });

         }

         @Override

         protected void onPause()

         {

                   // 暂停游戏

                   stopTimer();

                   super.onPause();

         }

         @Override

         protected void onResume()

         {

                   // 如果处于游戏状态中

                   if (isPlaying)

                   {

                            // 以剩余时间重写开始游戏

                            startGame(gameTime);

                   }

                   super.onResume();

}

(未完.摘自[疯狂Android讲义.李刚])