android游戏开发之我的小小游戏1——五子棋游戏3之悔棋与重新开始

首先我们在画板上绘制两个按钮,我们用两张图片表示,一张代表悔棋,一张代表重新开始,我们用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;
				}

 

效果图:

悔棋后:

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值