private void SetBomb(System.Drawing.Point p, int ms, TypeOfBomb t) { ThreadPool.QueueUserWorkItem(new WaitCallback(( object state ) => { Bomb bomb = null; Image rect = null; this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart) delegate() { bomb = new Bomb(p, TimeSpan.FromMilliseconds(ms), t, testMap); rect = bomb.rect; Carrier.Children.Add(rect); Canvas.SetLeft(rect, Bomb.gridSize * bomb.point.X); Canvas.SetTop(rect, Bomb.gridSize * bomb.point.Y); Canvas.SetZIndex(rect, p.Y); testMap.bombBlocks.Add(bomb); testMap.blocks[p.Y, p.X].isExistBomb = true; }); Thread.Sleep((int)bomb.time.TotalMilliseconds - 5); List <System.Drawing.Point> bomb_points = null; List <Image> bomb_effect = null; this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { source.musicsource.Bomb.Stop(); source.musicsource.Bomb.Play(); bomb_points = bomb.getExplodePoint(); bomb.frameCounter.Stop(); Carrier.Children.Remove(rect); testMap.bombBlocks.Remove(bomb); testMap.blocks[p.Y, p.X].isExistBomb = false; testMap.blocks[p.Y, p.X].isExistFire = true; bomb_effect = new List <Image>(); for (int i = 0; i < bomb_points.Count; i++) { bomb_effect.Add(new Image() { Source = bomb.bomb_eff[0].Source, Width = gridSize }); Carrier.Children.Add(bomb_effect[i]); Canvas.SetLeft(bomb_effect[i], bomb_points[i].X * gridSize); Canvas.SetTop(bomb_effect[i], bomb_points[i].Y * gridSize); testMap.blocks[bomb_points[i].Y, bomb_points[i].X].isExistFire = true; testMap.blocks[bomb_points[i].Y, bomb_points[i].X].HPrecover -= 10; } }); Thread.Sleep(3000); this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { for (int i = 0; i < bomb_points.Count; i++) { Carrier.Children.Remove(bomb_effect[i]); testMap.blocks[bomb_points[i].Y, bomb_points[i].X].isExistFire = false; testMap.blocks[bomb_points[i].Y, bomb_points[i].X].HPrecover += 10; } }); }), null); }
public Bomb(System.Drawing.Point p, TimeSpan ti, TypeOfBomb ty, MyMap map) { point = p; time = TimeSpan.FromMilliseconds(ti.TotalMilliseconds); type = ty; this.map = map; bomb_eff = new List<Image>(); rect = new Image(); Counter = 0; rect_eff = bomb_img[0]; rect.Width = gridSize; rect.Source = rect_eff[Counter++]; for (int i = 0; i < bomb_effects.Count; i++) { Image img = new Image(); img.Source = bomb_effects[i]; img.Height = gridSize; bomb_eff.Add(img); } startFrameCounter(); }
public Bomb(System.Drawing.Point p, TimeSpan ti, TypeOfBomb ty, MyMap map) { point = p; time = TimeSpan.FromMilliseconds(ti.TotalMilliseconds); type = ty; this.map = map; bomb_eff = new List <Image>(); rect = new Image(); Counter = 0; rect_eff = bomb_img[0]; rect.Width = gridSize; rect.Source = rect_eff[Counter++]; for (int i = 0; i < bomb_effects.Count; i++) { Image img = new Image(); img.Source = bomb_effects[i]; img.Height = gridSize; bomb_eff.Add(img); } startFrameCounter(); }
private void SetBomb( System.Drawing.Point p, int ms, TypeOfBomb t ) { ThreadPool.QueueUserWorkItem( new WaitCallback( ( object state ) => { Bomb bomb = null; Image rect = null; this.Dispatcher.Invoke( DispatcherPriority.Normal, (ThreadStart)delegate () { bomb = new Bomb( p, TimeSpan.FromMilliseconds( ms ), t, testMap ); rect = bomb.rect; Carrier.Children.Add( rect ); Canvas.SetLeft( rect, Bomb.gridSize * bomb.point.X ); Canvas.SetTop( rect, Bomb.gridSize * bomb.point.Y ); Canvas.SetZIndex( rect, p.Y ); testMap.bombBlocks.Add( bomb ); testMap.blocks[p.Y, p.X].isExistBomb = true; } ); Thread.Sleep( (int)bomb.time.TotalMilliseconds - 5 ); List<System.Drawing.Point> bomb_points = null; List<Image> bomb_effect = null; this.Dispatcher.BeginInvoke( DispatcherPriority.Normal, (ThreadStart)delegate () { source.musicsource.Bomb.Stop(); source.musicsource.Bomb.Play(); bomb_points = bomb.getExplodePoint(); bomb.frameCounter.Stop(); Carrier.Children.Remove( rect ); testMap.bombBlocks.Remove( bomb ); testMap.blocks[p.Y, p.X].isExistBomb = false; testMap.blocks[p.Y, p.X].isExistFire = true; bomb_effect = new List<Image>(); for ( int i = 0; i < bomb_points.Count; i++ ) { bomb_effect.Add( new Image() { Source = bomb.bomb_eff[0].Source, Width = gridSize } ); Carrier.Children.Add( bomb_effect[i] ); Canvas.SetLeft( bomb_effect[i], bomb_points[i].X * gridSize ); Canvas.SetTop( bomb_effect[i], bomb_points[i].Y * gridSize ); testMap.blocks[bomb_points[i].Y, bomb_points[i].X].isExistFire = true; testMap.blocks[bomb_points[i].Y, bomb_points[i].X].HPrecover -= 10; } } ); Thread.Sleep( 3000 ); this.Dispatcher.BeginInvoke( DispatcherPriority.Normal, (ThreadStart)delegate () { for ( int i = 0; i < bomb_points.Count; i++ ) { Carrier.Children.Remove( bomb_effect[i] ); testMap.blocks[bomb_points[i].Y, bomb_points[i].X].isExistFire = false; testMap.blocks[bomb_points[i].Y, bomb_points[i].X].HPrecover += 10; } } ); } ), null ); }
private void Carrier_KeyDown(object sender, KeyEventArgs e) { //对键盘事件的响应处理 if (isServer) { switch (e.Key) { case Key.Left: if (testMap.isBlockFree(person1.position.Y, person1.position.X - 1)) { person1.position.X--; person1.direction = MyPerson.Direction.Left; networkcontrol.AddGameCommand("person;" + 1 + ";" + person1.position.X + ";" + person1.position.Y + ";" + "left"); MoveOfPerson(person1); } break; case Key.Right: if (testMap.isBlockFree(person1.position.Y, person1.position.X + 1)) { person1.position.X++; person1.direction = MyPerson.Direction.Right; networkcontrol.AddGameCommand("person;" + 1 + ";" + person1.position.X + ";" + person1.position.Y + ";" + "right"); MoveOfPerson(person1); } break; case Key.Up: if (testMap.isBlockFree(person1.position.Y - 1, person1.position.X)) { person1.position.Y--; person1.direction = MyPerson.Direction.Up; networkcontrol.AddGameCommand("person;" + 1 + ";" + person1.position.X + ";" + person1.position.Y + ";" + "up"); MoveOfPerson(person1); } break; case Key.Down: if (testMap.isBlockFree(person1.position.Y + 1, person1.position.X)) { person1.position.Y++; person1.direction = MyPerson.Direction.Down; networkcontrol.AddGameCommand("person;" + 1 + ";" + person1.position.X + ";" + person1.position.Y + ";" + "down"); MoveOfPerson(person1); } break; case Key.Space: { if (!testMap.isBombExist(getExactPosition(person1).Y, getExactPosition(person1).X) && person1.mp > 20) { System.Drawing.Point p = getExactPosition(person1); networkcontrol.AddGameCommand("bomb;" + "1;" + p.X + ";" + p.Y + ";" + CurrentSkill); person1.mp -= 20; TypeOfBomb type = TypeOfBomb.normal; switch (CurrentSkill) { case 0: type = TypeOfBomb.normal; break; case 1: type = TypeOfBomb.around_small; break; case 2: type = TypeOfBomb.around_big; break; case 3: type = TypeOfBomb.row; break; case 4: type = TypeOfBomb.column; break; case 5: type = TypeOfBomb.row_column; break; case 6: type = TypeOfBomb.diagonal; break; } SetBomb(p, 3000, type); } } break; case Key.Z: CurrentSkill = 0; Canvas.SetLeft(sm, 60); break; case Key.X: CurrentSkill = 1; Canvas.SetLeft(sm, 110); break; case Key.C: CurrentSkill = 2; Canvas.SetLeft(sm, 160); break; case Key.V: CurrentSkill = 3; Canvas.SetLeft(sm, 210); break; case Key.B: CurrentSkill = 4; Canvas.SetLeft(sm, 260); break; case Key.N: CurrentSkill = 5; Canvas.SetLeft(sm, 310); break; case Key.M: CurrentSkill = 6; Canvas.SetLeft(sm, 360); break; default: ; break; } } else { switch (e.Key) { case Key.Left: networkcontrol.AddGameRequest("person;" + 2 + ";" + "left"); break; case Key.Right: networkcontrol.AddGameRequest("person;" + 2 + ";" + "right"); break; case Key.Up: networkcontrol.AddGameRequest("person;" + 2 + ";" + "up"); break; case Key.Down: networkcontrol.AddGameRequest("person;" + 2 + ";" + "down"); break; case Key.Space: networkcontrol.AddGameRequest("person;" + 2 + ";" + "bomb" + ";" + CurrentSkill); break; case Key.Z: CurrentSkill = 0; Canvas.SetLeft(sm, 60); break; case Key.X: CurrentSkill = 1; Canvas.SetLeft(sm, 110); break; case Key.C: CurrentSkill = 2; Canvas.SetLeft(sm, 160); break; case Key.V: CurrentSkill = 3; Canvas.SetLeft(sm, 210); break; case Key.B: CurrentSkill = 4; Canvas.SetLeft(sm, 260); break; case Key.N: CurrentSkill = 5; Canvas.SetLeft(sm, 310); break; case Key.M: CurrentSkill = 6; Canvas.SetLeft(sm, 360); break; } } e.Handled = true; //事件路由终止 }
void networkcontrol_NewRequest() { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { String str; lock (networkcontrol.RequestList_receive) { str = networkcontrol.RequestList_receive.Dequeue(); } String[] chips = str.Split(';'); if (chips[0] == "person" && chips[1] == "2") { if (chips[2] == "left") { if (testMap.isBlockFree(person2.position.Y, person2.position.X - 1)) { person2.position.X--; person2.direction = MyPerson.Direction.Left; networkcontrol.AddGameCommand("person;" + 2 + ";" + person2.position.X + ";" + person2.position.Y + ";" + "left"); MoveOfPerson(person2); } } else if (chips[2] == "right") { if (testMap.isBlockFree(person2.position.Y, person2.position.X + 1)) { person2.position.X++; person2.direction = MyPerson.Direction.Right; networkcontrol.AddGameCommand("person;" + 2 + ";" + person2.position.X + ";" + person2.position.Y + ";" + "right"); MoveOfPerson(person2); } } else if (chips[2] == "up") { if (testMap.isBlockFree(person2.position.Y - 1, person2.position.X)) { person2.position.Y--; person2.direction = MyPerson.Direction.Up; networkcontrol.AddGameCommand("person;" + 2 + ";" + person2.position.X + ";" + person2.position.Y + ";" + "up"); MoveOfPerson(person2); } } else if (chips[2] == "down") { if (testMap.isBlockFree(person2.position.Y + 1, person2.position.X)) { person2.position.Y++; person2.direction = MyPerson.Direction.Down; networkcontrol.AddGameCommand("person;" + 2 + ";" + person2.position.X + ";" + person2.position.Y + ";" + "down"); MoveOfPerson(person2); } } else if (chips[2] == "bomb") { if (!testMap.isBombExist(getExactPosition(person2).Y, getExactPosition(person2).X) && person2.mp > 20) { System.Drawing.Point p = getExactPosition(person2); networkcontrol.AddGameCommand("bomb;" + "2;" + p.X + ";" + p.Y + ";" + chips[3]); person2.mp -= 20; int i = Int32.Parse(chips[3]); TypeOfBomb type = TypeOfBomb.normal; switch (i) { case 0: type = TypeOfBomb.normal; break; case 1: type = TypeOfBomb.around_small; break; case 2: type = TypeOfBomb.around_big; break; case 3: type = TypeOfBomb.row; break; case 4: type = TypeOfBomb.column; break; case 5: type = TypeOfBomb.row_column; break; case 6: type = TypeOfBomb.diagonal; break; } SetBomb(p, 3000, type); } } } else if (chips[0] == "ready") { Console.WriteLine("ready"); creatGameDialog.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { creatGameDialog.Confirm.IsEnabled = true; }); } else { Paragraph p = new Paragraph(new Run(str)); Message.Document.Blocks.Add(p); Message.ScrollToEnd(); } }); }
void networkcontrol_NewCommand() { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart) delegate() { String str; lock (networkcontrol.CommandList_receive) { str = networkcontrol.CommandList_receive.Dequeue(); } String[] chips = str.Split(';'); MyPerson person = null; if (chips[0] == "person") { if (chips[1] == "1") { person = person1; } else if (chips[1] == "2") { person = person2; } person.position = new System.Drawing.Point(Int32.Parse(chips[2]), Int32.Parse(chips[3])); if (chips[4] == "left") { person.direction = MyPerson.Direction.Left; } if (chips[4] == "right") { person.direction = MyPerson.Direction.Right; } if (chips[4] == "up") { person.direction = MyPerson.Direction.Up; } if (chips[4] == "down") { person.direction = MyPerson.Direction.Down; } MoveOfPerson(person); } else if (chips[0] == "bomb") { if (chips[1] == "1") { person1.mp -= 20; } else if (chips[1] == "2") { person2.mp -= 20; } int i = Int32.Parse(chips[4]); TypeOfBomb type = TypeOfBomb.normal; switch (i) { case 0: type = TypeOfBomb.normal; break; case 1: type = TypeOfBomb.around_small; break; case 2: type = TypeOfBomb.around_big; break; case 3: type = TypeOfBomb.row; break; case 4: type = TypeOfBomb.column; break; case 5: type = TypeOfBomb.row_column; break; case 6: type = TypeOfBomb.diagonal; break; } SetBomb(new System.Drawing.Point(Int32.Parse(chips[2]), Int32.Parse(chips[3])), 3000, type); Paragraph p = new Paragraph(new Run("->请注意炸弹!")); Tip.Document.Blocks.Add(p); Tip.ScrollToEnd(); } else if (chips[0] == "brick") { List <System.Drawing.Point> temp = new List <System.Drawing.Point>(); for (int i = 3; i < chips.Length; i++) { String[] s = chips[i].Split(','); temp.Add(new System.Drawing.Point(Int32.Parse(s[0]), Int32.Parse(s[1]))); } if (chips[1] == "creat") { RefreshBlock(temp, true); } else if (chips[1] == "delete") { RefreshBlock(temp, false); } } else if (chips[0] == "drug") { int pid = Int32.Parse(chips[2]); if (chips[1] == "h") { setHpDrug(pid); } else if (chips[1] == "m") { setMpDrug(pid); } Paragraph p = new Paragraph(new Run("->新的药品出现")); Tip.Document.Blocks.Add(p); Tip.ScrollToEnd(); } else if (chips[0] == "reset") { if (chips[1] == "1") { person1.hp = Int32.Parse(chips[2]); person1.mp = Int32.Parse(chips[3]); } else if (chips[1] == "2") { person2.hp = Int32.Parse(chips[2]); person2.mp = Int32.Parse(chips[3]); } } else if (chips[0] == "start!") { Paragraph p = new Paragraph(new Run("->游戏开始!")); Tip.Document.Blocks.Add(p); Tip.ScrollToEnd(); GameInit(); } else { Paragraph p; if (str.StartsWith("Client")) { p = new Paragraph(new Run(str) { Foreground = new SolidColorBrush(Colors.Green) }); } else { p = new Paragraph(new Run(str) { Foreground = new SolidColorBrush(Colors.Red) }); } Message.Document.Blocks.Add(p); Message.ScrollToEnd(); } }); }