android遊戲開發之我的小小遊戲1——五子棋遊戲3之悔棋與重新開始

來源:互聯網
上載者:User

首先我們在畫板上繪製兩個按鈕,我們用兩張圖片表示,一張代表悔棋,一張代表重新開始,我們用LinkedList來儲存我們操作的每個步驟,然後從LinkedList中提取每一步的操作

//兩個按鈕的圖片Bitmap[] button = new Bitmap[2];
//為兩個按鈕綁定資源button[0] = BitmapFactory.decodeResource(this.getResources(), R.drawable.restart);button[1] = BitmapFactory.decodeResource(this.getResources(), R.drawable.regret);
//繪製按鈕canvas.drawBitmap(button[0], screenWidth/2-30-button[0].getWidth(), 40, paint);canvas.drawBitmap(button[1], screenWidth/2+30, 40, paint);

在觸摸事件函數中添加記錄操作的語句:

LinkedList<Map<String,Integer>> list = new LinkedList<Map<String,Integer>>();Map<String,Integer> map = new HashMap<String,Integer>();map.put("index_x", index_x);//二維數組中x值map.put("index_y", index_y);//二維數組中y值list.add(map);

 

對觸摸在按鈕區間的動作進行監聽:

//監聽按鈕事件,重新開始if(touchX >screenWidth/2-30-button[0].getWidth() && touchX<screenWidth/2-30 && touchY>40 &&touchY<40+button[0].getHeight()){for(int i=0;i<GRID_NUM;i++){for(int j=0;j<GRID_NUM;j++){chess[i][j] = 0;winFlag = false;chess_flag = 0;}}}//悔棋if(touchX >screenWidth/2+30 && touchX<screenWidth/2+30+button[1].getWidth() && touchY>40 &&touchY<40+button[1].getHeight()){int size = list.size();if(chess[list.get(size-1).get("index_x")][list.get(size-1).get("index_y")] == CHESS_BLACK){chess_flag = CHESS_WHITE;chess[list.get(size-1).get("index_x")][list.get(size-1).get("index_y")] = 0;}else if(chess[list.get(size-1).get("index_x")][list.get(size-1).get("index_y")] == CHESS_WHITE){chess_flag = CHESS_BLACK;chess[list.get(size-1).get("index_x")][list.get(size-1).get("index_y")] = 0;}list.removeLast();//刪除最後一次的操作winFlag = false;}

 

:

悔棋後:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.