private void Timer_Elapsed(object sender, ElapsedEventArgs e) { mainWindow.Dispatcher.Invoke(new Action(() => { if (MoveDown()) { return; } else { //下落完成 Insert(nowBlock, top, left); nowBlock = nextBlock; top = 0; left = column / 2 - nowBlock.size / 2; nextBlock.Remove(mainWindow.nextBlockCanvas); nextBlock = GameBlock.NewBlock(); nextBlock.Draw(mainWindow.nextBlockCanvas, 6, 6, fillBrush1, edgeBrush1, 1, 1); UpdateBoard(); if (!CanInsert(nowBlock, top, left)) { timer.Stop(); MessageBox.Show("游戏结束"); nowBlock = null; File.WriteAllText("score", highScore.ToString()); Clear(); } else { nowBlock.Draw(mainWindow.gameCanvas, row, column, fillBrush1, edgeBrush1, top, left); //更新interval timer.Stop(); timer.Interval = Math.Max(minInterval, initInterval - score * gapInterval); timer.Start(); } } })); }
/// <summary> /// 开始游戏 /// </summary> public void Start() { timer.Stop(); if (nowBlock != null) { nowBlock.Remove(mainWindow.gameCanvas); } if (nextBlock != null) { nextBlock.Remove(mainWindow.nextBlockCanvas); } random = new Random(); Clear(); nowBlock = GameBlock.NewBlock(); nextBlock = GameBlock.NewBlock(); top = 0; left = column / 2 - nowBlock.size / 2; nowBlock.Draw(mainWindow.gameCanvas, row, column, fillBrush1, edgeBrush1, top, left); nextBlock.Draw(mainWindow.nextBlockCanvas, 6, 6, fillBrush1, edgeBrush1, 1, 1); score = 0; if (!File.Exists("score")) { highScore = 0; } else { Int32.TryParse(File.ReadAllText("score"), out highScore); } mainWindow.highScore.Text = "最高分:\n" + highScore.ToString(); mainWindow.score.Text = "得分:\n" + score.ToString(); timer.Interval = initInterval; timer.Start(); }