/// <summary> /// 开始新游戏 /// </summary> public static void StartNewGame() { AllShapes.AutoDown(false); if (State != GameStates.Stoped && MessageBox.Show("\n 确实要重新开始游戏吗? \n ", "重新开始", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { if (State != GameStates.Paused) { AllShapes.AutoDown(true); } return; } Ini(); CanOp = true; State = GameStates.Playing; AllShapes.MakeNewShape(); AllShapes.AutoDown(true); ClassMain.formMain.labelInfo.Text = "Playing...."; if (Globals.IsPlayBackMusic) { Sound.PlayMusic(Globals.PathOfBackMusic, true); } }
//升级时播放音乐 private static void LevelSound() { Sound.PlaySound(Application.StartupPath + @"\Sounds\Level.wav", IntPtr.Zero, 1); //升级音乐 AllShapes.AutoDown(false); CanOp = false; System.Threading.Thread.Sleep(7000); AllShapes.AutoDown(true); CanOp = true; }
/// <summary> /// 开始游戏,但不还原数据(要还原数据并开始新游戏,请使用StartNewGame()) /// </summary> public static void Start() { Globals.BasePoint = new GirdPoint(0, 0); CanOp = true; State = GameStates.Playing; AllShapes.MakeNewShape(); AllShapes.AutoDown(true); ClassMain.formMain.labelInfo.Text = "Playing...."; if (Globals.IsPlayBackMusic) { Sound.PlayMusic(Globals.PathOfBackMusic, true); } }
/// <summary> /// 产生新形状并下落 /// </summary> public static void MakeNewShape() { if (Game.State == GameStates.Playing) { Globals.IndexOfCurrentShape = Globals.NextIndexOfShape; Random rand = new Random(); Globals.NextIndexOfShape = rand.Next(18); //MakeNewViewShape(Globals.NextIndexOfShape);//显示预览形状 //由于产生新形状时采用了AllShapes.Eddy(Globals.IndexOfCurrentShape);所以这里采用了下面的一句,而不是上面注释掉的 //否则出现的新形状与预览形状相差90度 MakeNewViewShape(AllShapes.Shapes[Globals.NextIndexOfShape].EddiedIndex); Globals.BasePoint = new GirdPoint(0, 0); AllShapes.Eddy(Globals.IndexOfCurrentShape); } }
//删除指定行 private static void DelLine(int indexOfLine) { AllShapes.AutoDown(false); Game.CanOp = false; //熄灭该行上的背景块,表现为移出了该行上的块 for (int i = 0; i < Globals.CountOfTier; i++) { if (indexOfLine % 2 == 0) //偶数行从左向由消失 { Globals.BackBlocks[indexOfLine, i].BackColor = Globals.ColorOfBackBlock; //释放被占据的网格 Globals.GirdArray[indexOfLine, i] = 0; } else //奇数行从右向左消失 { Globals.BackBlocks[indexOfLine, Globals.CountOfTier - 1 - i].BackColor = Globals.ColorOfBackBlock; //释放被占据的网格 Globals.GirdArray[indexOfLine, Globals.CountOfTier - 1 - i] = 0; } //表现为从左向右逐个消失(原来的问题是当一直按住向下键时,没有效果,解决方法是建立新线程) System.Threading.Thread.Sleep(30); } //该行以上的所有行整体下落 for (int i = indexOfLine - 1; i >= 3; i--) { for (int j = 0; j < Globals.CountOfTier; j++) { if (Globals.GirdArray[i, j] == 1) { //此块熄灭,其正下方的块点亮,表现为下落了一格 Globals.GirdArray[i, j] = 0; Globals.BackBlocks[i, j].BackColor = Globals.ColorOfBackBlock; Globals.GirdArray[i + 1, j] = 1; Globals.BackBlocks[i + 1, j].BackColor = Globals.ColorOfFixedBlock; } } } AllShapes.AutoDown(false); Game.CanOp = true; }
//开始新游戏前的数据还原 private static void Ini() { //还原网格数组,还原背景方块,还原基础点 for (int i = 0; i < Globals.CountOfRow - 1; i++) { for (int j = 0; j < Globals.CountOfTier; j++) { Globals.GirdArray[i, j] = 0; Globals.BackBlocks[i, j].BackColor = Globals.ColorOfBackBlock; } } Globals.BasePoint = new GirdPoint(0, 0); ClassMain.formMain.labelScore.Text = "0"; Score = 0; SpeedLevel = 1; //MakeNewViewShape(Globals.NextIndexOfShape);//显示预览形状 //由于产生新形状时采用了AllShapes.Eddy(Globals.IndexOfCurrentShape);所以这里采用了下面的一句,而不是上面注释掉的 AllShapes.MakeNewViewShape(AllShapes.Shapes[Globals.NextIndexOfShape].EddiedIndex); }
//按键 private void FormMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Left) { if (Game.CanOp) { AllShapes.ToLeft(Globals.IndexOfCurrentShape); Sound.PlaySound(Application.StartupPath + @"\Sounds\Left.wav", IntPtr.Zero, 1); } } else if (e.KeyCode == Keys.Right) { if (Game.CanOp) { AllShapes.ToRight(Globals.IndexOfCurrentShape); Sound.PlaySound(Application.StartupPath + @"\Sounds\Right.wav", IntPtr.Zero, 1); } } else if (e.KeyCode == Keys.Down) { if (Game.CanOp) { AllShapes.timer.Stop(); AllShapes.ToDown(Globals.IndexOfCurrentShape); AllShapes.timer.Start(); } } else if (e.KeyCode == Keys.Up) { if (Game.CanOp) { AllShapes.Eddy(Globals.IndexOfCurrentShape); Sound.PlaySound(Application.StartupPath + @"\Sounds\Up.wav", IntPtr.Zero, 1); } } else if (e.KeyCode == Keys.Q) { Game.StartNewGame(); } else if (e.KeyCode == Keys.P) { if (Game.State == GameStates.Playing) { Game.Pause(); } else if (Game.State == GameStates.Paused) { Game.Resume(); } } else if (e.KeyCode == Keys.O) //设置 { Game.Pause(); FormSet fs = new FormSet(); fs.ShowDialog(); } else if (e.KeyCode == Keys.S) //save { Game.Pause(); FormSave fs = new FormSave(); fs.ShowDialog(); } else if (e.KeyCode == Keys.L) //Load { Game.Pause(); FormLoad fL = new FormLoad(); fL.ShowDialog(); } else if (e.KeyCode == Keys.D1) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(1); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D2) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(2); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D3) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(3); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D4) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(4); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D5) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(5); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D6) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(6); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D7) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(7); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D8) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(8); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } else if (e.KeyCode == Keys.D9) { if (Game.State == GameStates.Stoped) { Game.ChangeLevel(9); } else { this.labelTempInfo.Text = "游戏过程中不可设置速度级别"; } } }