private int _step = 8; //交换移动步长 public void BuildMap()//随机生成地图 { Random myRand = new Random(DateTime.Now.Second); for (int i = 1; i <= _height; i++) { for (int j = 1; j <= _width; j++) { _map[i, j] = new Jelly((jcolor)myRand.Next(1, 7), new Point(_topleft.X + (j - 1) * _size, _topleft.Y + (i - 1) * _size), _size, 0); } } }
//执行交换 private void timer2_Tick(object sender, EventArgs e) { pictureBox1.Invalidate(); int flag = 0; if (_y1 == _y2) { if (_x1 < _x2) { j1._Top = new Point(j1._Top.X + _step, j1._Top.Y); j2._Top = new Point(j2._Top.X - _step, j2._Top.Y); if (j1._Top.X >= _x2) { flag = 1; } } else { j1._Top = new Point(j1._Top.X - _step, j1._Top.Y); j2._Top = new Point(j2._Top.X + _step, j2._Top.Y); if (j1._Top.X <= _x2) { flag = 1; } } } else if (_x1 == _x2) { if (_y1 < _y2) { j1._Top = new Point(j1._Top.X, j1._Top.Y + _step); j2._Top = new Point(j2._Top.X, j2._Top.Y - _step); if (j1._Top.Y >= _y2) { flag = 1; } } else { j1._Top = new Point(j1._Top.X, j1._Top.Y - _step); j2._Top = new Point(j2._Top.X, j2._Top.Y + _step); if (j1._Top.Y <= _y2) { flag = 1; } } } if (flag == 1) //如果交换好了 { timer2.Enabled = false; //关闭交换计时器 timer1.Enabled = true; //继续计时 _map[_pickRow, _pickCol]._Color = j2._Color; _map[_dropRow, _dropCol]._Color = j1._Color; pictureBox1.Invalidate(); if (_changeFlag == 0) { if (!clean())//如果不能消除那么换回来,重新标记 { j1 = new Jelly(_map[_pickRow, _pickCol]._Color, _map[_pickRow, _pickCol]._Top, _map[_pickRow, _pickCol]._Size, _map[_pickRow, _pickCol]._Flag); j2 = new Jelly(_map[_dropRow, _dropCol]._Color, _map[_dropRow, _dropCol]._Top, _map[_dropRow, _dropCol]._Size, _map[_dropRow, _dropCol]._Flag); // swap(_dropRow, _dropCol, _pickRow, _pickCol); _map[_pickRow, _pickCol]._Color = jcolor.无;//交换好了先隐藏 _map[_dropRow, _dropCol]._Color = jcolor.无; timer1.Enabled = false; timer2.Enabled = true; _map[_pickRow, _pickCol]._Flag = 0; _ispic = 0; _changeFlag = 1; } else//交换成功 { timer3.Enabled = true;//执行下落 _pickRow = 0; _pickCol = 0; _ispic = 0; } } } }
//鼠标点击 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (timer3.Enabled == false && _istart == 1 && _stop == 0) { if (_ispic == 0)//如果没有点击过,标记点击位置 { int row, col; bool value = ConvertPointToRowCol(new Point(e.X, e.Y), out row, out col); //捕捉行列 if (value == true) { if (_map[row, col]._Color != jcolor.无) //如果不是无,就选择 { _pickRow = row; _pickCol = col; _ispic = 1; _map[_pickRow, _pickCol].change();//将选择方块变样子 } } } else//如果已经点击过 { int row, col; bool value = ConvertPointToRowCol(new Point(e.X, e.Y), out row, out col); if (value == true) { if (_map[row, col]._Color != jcolor.无) { //如果点击和标记位置相连,交换 if ((row == _pickRow && Math.Abs(col - _pickCol) == 1) || (col == _pickCol && Math.Abs(row - _pickRow) == 1)) { _dropRow = row; _dropCol = col; _map[_pickRow, _pickCol]._Flag = 0; j1 = new Jelly(_map[_pickRow, _pickCol]._Color, _map[_pickRow, _pickCol]._Top, _map[_pickRow, _pickCol]._Size, _map[_pickRow, _pickCol]._Flag); j2 = new Jelly(_map[_dropRow, _dropCol]._Color, _map[_dropRow, _dropCol]._Top, _map[_dropRow, _dropCol]._Size, _map[_dropRow, _dropCol]._Flag); // swap(row, col, _pickRow, _pickCol); _map[_pickRow, _pickCol]._Color = jcolor.无;//交换好了先隐藏 _map[_dropRow, _dropCol]._Color = jcolor.无; pictureBox1.Invalidate(); _x1 = j1._Top.X;//记录一系列临时变量 _y1 = j1._Top.Y; _x2 = j2._Top.X; _y2 = j2._Top.Y; _changeFlag = 0; timer1.Enabled = false; //暂停计时 timer2.Enabled = true; //执行交换 } else//不相邻就重新标记 { _map[_pickRow, _pickCol]._Flag = 0; _pickRow = row; _pickCol = col; _map[_pickRow, _pickCol].change(); _ispic = 1; } } } } } pictureBox1.Invalidate(); }