// 创建一个新的形状
CreatePiece();
// 呈现当前创建出的形状
AddPiece(0, 0);
// Timer 用于定时向下移动形状
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(_initSpeed);
_timer.Tick += new EventHandler(_timer_Tick);
GameStatus = GameStatus.Ready;
}
/**//// <summary>
/// 开始游戏(启动计时器)
/// </summary>
public void Play()
{
GameStatus = GameStatus.Play;
_timer.Start();
}
/**//// <summary>
/// 暂停游戏(停止计时器)
/// </summary>
public void Pause()
{
GameStatus = GameStatus.Pause;
_timer.Stop();
}
/**//// <summary>
/// 创建一个新的形状
/// </summary>
private void CreatePiece()
{
// 逻辑移到 下坠后 的逻辑内
for (int x = 0; x < _columns; x++)
{
if (Container[0, x].Color != null)
{
OnGameOver(null);
break;
}
}
// 计算 当前形状 和 下一个形状
Random random = new Random();
_currentPiece = _nextPiece == null ? _pieces[random.Next(0, 7)] : _nextPiece;
_nextPiece = _pieces[random.Next(0, 7)];
// 形状所属的 4×4 容器的 X 坐标和 Y 坐标
_positionX = 3;
_positionY = 0;
// 设置“下一个形状的容器”的 UI
SetNextContainerUI();
}
private void _timer_Tick(object sender, EventArgs e)
{
MoveToDown();
}
/**//// <summary>
/// 向左移动
/// </summary>
public void MoveToLeft()
{
if (GameStatus != GameStatus.Play) return;
if (!IsBoundary(_currentPiece.Matrix, -1, 0))
{
RemovePiece();
AddPiece(-1, 0);
}
}
/**//// <summary>
/// 向右移动
/// </summary>
public void MoveToRight()
{
if (GameStatus != GameStatus.Play) return;
if (!IsBoundary(_currentPiece.Matrix, 1, 0))
{
RemovePiece();
AddPiece(1, 0);
}
}
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页
责任编辑:小草