/// <summary>
/// 變形
/// </summary>
public void Rotate()
{
if (GameStatus != GameStatus.Play) return;
if (!IsBoundary(_currentPiece.GetRotate(), 0, 0))
{
RemovePiece();
_currentPiece.Rotate();
AddPiece(0, 0);
}
}
/// <summary>
/// 清除俄羅斯方塊容器
public void Clear()
for (int x = 0; x < _columns; x++)
for (int y = 0; y < _rows; y++)
{
Container[y, x].Color = null;
}
/// 邊界判斷(是否超過邊界)
/// <param name="matrix">目前操作的形狀的4×4矩陣</param>
/// <param name="offsetX">矩陣 X 方向的偏移量</param>
/// <param name="offsetY">矩陣 Y 方向的偏移量</param>
/// <returns></returns>
private bool IsBoundary(int[,] matrix, int offsetX, int offsetY)
RemovePiece();
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (matrix[i, j] == 1)
{
if (j + _positionX + offsetX > _columns - 1 // 超過列的右邊界
|| i + _positionY + offsetY > _rows - 1 // 超過行的下邊界
|| j + _positionX + offsetX < 0 // 超過列的左邊界
|| Container[i + _positionY + offsetY, j + _positionX + offsetX].Color != null) // matrix 所需偏移的地方已經有 Block 占着了
{
AddPiece(0, 0);
return true;
}
}
AddPiece(0, 0);
return false;
/// 設定“下一個形狀的容器”的 UI
private void SetNextContainerUI()
// 清空
foreach (Block block in NextContainer)
block.Color = null;
// 根據 _nextPiece 的矩陣設定相對應的 Block 對象的呈現
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
if (_nextPiece.Matrix[x, y] == 1)
NextContainer[x, y].Color = _nextPiece.Color;
/// 移除 _currentPiece 在界面上的呈現
private void RemovePiece()
if (_currentPiece.Matrix[i, j] == 1)
Container[i + _positionY, j + _positionX].Color = null;
/// 增加 _currentPiece 在界面上的呈現
/// <param name="offsetX">X 方向上的偏移量</param>
/// <param name="offsetY">Y 方向上的偏移量</param>
private void AddPiece(int offsetX, int offsetY)
Container[i + _positionY + offsetY, j + _positionX + offsetX].Color = _currentPiece.Color;
_positionX += offsetX;
_positionY += offsetY;
/// 根據遊戲規則,如果某行出現連續的直線則将其删除,該線以上的部分依次向下移動
private void RemoveRow()
// 删除的行數
int removeRowCount = 0;
// 行的周遊(Y 方向)
for (int y = 0; y < _rows; y++)
// 該行是否是一條連續的直線
bool isLine = true;
// 列的周遊(X 方向)
for (int x = 0; x < _columns; x++)
if (Container[y, x].Color == null)
// 出現斷行,則繼續周遊下一行
isLine = false;
break;
// 該行是一條連續的直線則将其删除,并将該行以上的部分依次向下移動
if (isLine)
removeRowCount++;
// 删除該行
for (int x = 0; x < _columns; x++)
Container[y, x].Color = null;
// 将被删除行的以上行依次向下移動
for (int i = y; i > 0; i--)
for (int x = 0; x < _columns; x++)
Container[i, x].Color = Container[i - 1, x].Color;
}
// 加分,計算方法: 2 的 removeRowCount 次幂 乘以 10
if (removeRowCount > 0)
Score += 10 * (int)Math.Pow(2, removeRowCount);
// 更新總的已消行數
RemoveRowCount += removeRowCount;
// 根據已消行數計算級别,依據丁學的建議,計算方法: 已消行數/5 的平方根 取整
Level = (int)Math.Sqrt(RemoveRowCount / 5);
// 根據級别計算速率,計算方法: 初始速率 減 (每多一個級别所需增加的速率 乘以 目前級别)
_timer.Interval = TimeSpan.FromMilliseconds(_initSpeed - _levelSpeed * Level > _levelSpeed ? _initSpeed - _levelSpeed * Level : _levelSpeed);
private int _score = 0;
/// 得分
public int Score
get { return _score; }
set
_score = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Score"));
private int _removeRowCount = 0;
/// 總共被消除的行數
public int RemoveRowCount
get { return _removeRowCount; }
_removeRowCount = value;
PropertyChanged(this, new PropertyChangedEventArgs("RemoveRowCount"));
private int _level = 0;
/// 級别(遊戲難度)
public int Level
get { return _level; }
_level = value;
PropertyChanged(this, new PropertyChangedEventArgs("Level"));
public event PropertyChangedEventHandler PropertyChanged;
/// 遊戲結束的事件委托
public event EventHandler GameOver;
/// 遊戲結束後所調用的方法,并觸發遊戲結束事件
/// <param name="e"></param>
private void OnGameOver(EventArgs e)
GameStatus = GameStatus.Over;
_timer.Interval = TimeSpan.FromMilliseconds(_initSpeed);
_timer.Stop();
EventHandler handler = GameOver;
if (handler != null)
handler(this, e);
}
}
OK
<a href="http://down.51cto.com/data/101271" target="_blank">[源碼下載下傳]</a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345632,如需轉載請自行聯系原作者