public Game() { InitializeComponent(); pacman = new PacmanClass(); ghostsMoving = new GhostsMoving(this, pacman); ghostMoveTypeTimer = new System.Windows.Forms.Timer { Interval = 10000 }; ghostMoveTypeTimer.Tick += new EventHandler(GhostMoveTypeTimer_Tick); redGhost = new Ghost(); yellowGhost = new Ghost(); pinkGhost = new Ghost(); }
public GhostsMoving(Game game, PacmanClass pacman) { this.pacman = pacman; this.game = game; }
private void GhostMove(Image Ghost, object _game, object _pmc) { Game game = (Game)_game; PacmanClass pmc = (PacmanClass)_pmc; Thread.Sleep(8); Random rnd = new Random(); switch (rnd.Next(1, 5)) { case 1: Ghost.Dispatcher.BeginInvoke((Action)(() => { if (Ghost.Margin.Top - 10 >= 0) { int x1 = (int)(Ghost.Margin.Left / 30); int y = (int)((Ghost.Margin.Top - 10) / 30); int x2 = (int)((Ghost.Margin.Left + 29) / 30); if ((game.map[y, x1] == '0' || game.map[y, x1] == '2' || game.map[y, x1] == '3') && (game.map[y, x2] == '0' || game.map[y, x2] == '2' || game.map[y, x2] == '3')) { if (Ghost.Margin.Top + 10 <= (630 - pmc.ImgPacman.Margin.Bottom) && Ghost.Margin.Top >= pmc.ImgPacman.Margin.Top && (x1 == (int)(pmc.ImgPacman.Margin.Left / 30) || x2 == (int)((pmc.ImgPacman.Margin.Left + 25) / 30))) { game.is_lose = true; game.IsWin(); return; } Ghost.Margin = new Thickness(Ghost.Margin.Left, Ghost.Margin.Top - 10, Ghost.Margin.Right, Ghost.Margin.Bottom + 10); } } })); break; case 2: Ghost.Dispatcher.BeginInvoke((Action)(() => { if (Ghost.Margin.Bottom - 10 >= 0) { int x1 = (int)(Ghost.Margin.Left / 30); int y = (int)((Ghost.Margin.Top + 30) / 30); int x2 = (int)((Ghost.Margin.Left + 29) / 30); if ((game.map[y, x1] == '0' || game.map[y, x1] == '2' || game.map[y, x1] == '3') && (game.map[y, x2] == '0' || game.map[y, x2] == '2' || game.map[y, x2] == '3')) { if (Ghost.Margin.Bottom + 10 <= (630 - pmc.ImgPacman.Margin.Top) && Ghost.Margin.Bottom >= pmc.ImgPacman.Margin.Bottom && (x1 == (int)(pmc.ImgPacman.Margin.Left / 30) || x2 == (int)((pmc.ImgPacman.Margin.Left + 29) / 30))) { game.is_lose = true; game.IsWin(); return; } Ghost.Margin = new Thickness(Ghost.Margin.Left, Ghost.Margin.Top + 10, Ghost.Margin.Right, Ghost.Margin.Bottom - 10); } } })); break; case 3: Ghost.Dispatcher.BeginInvoke((Action)(() => { if (Ghost.Margin.Right - 10 >= 0) { int x = (int)((Ghost.Margin.Left + 30) / 30); int y1 = (int)(Ghost.Margin.Top / 30); int y2 = (int)((Ghost.Margin.Top + 29) / 30); if ((game.map[y1, x] == '0' || game.map[y1, x] == '2' || game.map[y1, x] == '3') && (game.map[y2, x] == '0' || game.map[y2, x] == '2' || game.map[y2, x] == '3')) { if (Ghost.Margin.Right + 10 <= (630 - pmc.ImgPacman.Margin.Left) && Ghost.Margin.Right >= pmc.ImgPacman.Margin.Right && (y1 == (int)(pmc.ImgPacman.Margin.Top / 30) || y2 == (int)((pmc.ImgPacman.Margin.Top + 25) / 30))) { game.is_lose = true; game.IsWin(); return; } Ghost.Margin = new Thickness(Ghost.Margin.Left + 10, Ghost.Margin.Top, Ghost.Margin.Right - 10, Ghost.Margin.Bottom); } } })); break; case 4: Ghost.Dispatcher.BeginInvoke((Action)(() => { if (Ghost.Margin.Left - 10 >= 0) { int x = (int)((Ghost.Margin.Left - 10) / 30); int y1 = (int)(Ghost.Margin.Top / 30); int y2 = (int)((Ghost.Margin.Top + 29) / 30); if ((game.map[y1, x] == '0' || game.map[y1, x] == '2' || game.map[y1, x] == '3') && (game.map[y2, x] == '0' || game.map[y2, x] == '2' || game.map[y2, x] == '3')) { if (Ghost.Margin.Left + 10 <= (630 - pmc.ImgPacman.Margin.Right) && Ghost.Margin.Left >= pmc.ImgPacman.Margin.Left && (y1 == (int)(pmc.ImgPacman.Margin.Top / 30) || y2 == (int)((pmc.ImgPacman.Margin.Top + 25) / 30))) { game.is_lose = true; game.IsWin(); return; } Ghost.Margin = new Thickness(Ghost.Margin.Left - 10, Ghost.Margin.Top, Ghost.Margin.Right + 10, Ghost.Margin.Bottom); } } })); break; } }