/// <summary> /// 固定当前形状在屏幕上 /// </summary> public static void FixShape() { timer.Stop(); //停止下落 for (int i = 0; i < 4; i++) { //点亮对应的背景方块 Globals.BackBlocks[Globals.DynamicBlocksArray[i].GLocation.Y , Globals.DynamicBlocksArray[i].GLocation.X].BackColor = Globals.ColorOfFixedBlock; //将对应的网格值设置为1,表示此网格已经被占据 Globals.GirdArray[Globals.DynamicBlocksArray[i].GLocation.Y, Globals.DynamicBlocksArray[i].GLocation.X] = 1; } int temp = Globals.BasePoint.Y; MoveAndRotate.MakeNewShape(); //扫描当前形状所在的四行,但最多达到第22行 DelLines(temp, temp + 3 > 22 ? 22 : temp + 3); if (IsGameOver()) { Game.Over(); } timer.Start(); }
/// <summary> /// 开始新游戏 /// </summary> public static void StartNewGame() { MoveAndRotate.AutoDown(false); if (State != GameStates.Stoped && MessageBox.Show("\n 确实要重新开始游戏吗? \n ", "重新开始", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { if (State != GameStates.Paused) { MoveAndRotate.AutoDown(true); } return; } Ini(); CanOp = true; State = GameStates.Playing; MoveAndRotate.MakeNewShape(); MoveAndRotate.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); //升级音乐 MoveAndRotate.AutoDown(false); CanOp = false; System.Threading.Thread.Sleep(7000); MoveAndRotate.AutoDown(true); CanOp = true; }
/// <summary> /// 开始游戏,但不还原数据(要还原数据并开始新游戏,请使用StartNewGame()) /// </summary> public static void Start() { Globals.BasePoint = new GirdPoint(0, 0); CanOp = true; State = GameStates.Playing; MoveAndRotate.MakeNewShape(); MoveAndRotate.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); MoveAndRotate.Eddy(Globals.IndexOfCurrentShape); } }
//删除指定行 private static void DelLine(int indexOfLine) { MoveAndRotate.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; } } } MoveAndRotate.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);所以这里采用了下面的一句,而不是上面注释掉的 MoveAndRotate.MakeNewViewShape(AllShapes.Shapes[Globals.NextIndexOfShape].EddiedIndex); }
private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { MoveAndRotate.ToDown(Globals.IndexOfCurrentShape); }
//按键 private void FormMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Left) { if (Game.CanOp) { MoveAndRotate.ToLeft(Globals.IndexOfCurrentShape); Sound.PlaySound(Application.StartupPath + @"\Sounds\Left.wav", IntPtr.Zero, 1); } } else if (e.KeyCode == Keys.Right) { if (Game.CanOp) { MoveAndRotate.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(); MoveAndRotate.ToDown(Globals.IndexOfCurrentShape); AllShapes.timer.Start(); } } else if (e.KeyCode == Keys.Up) { if (Game.CanOp) { MoveAndRotate.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 = "游戏过程中不可设置速度级别"; } } }