public void SetNewLocation(int[,] maze, Pacman pman) { this.image = Properties.Resources.PinkGhost; int i, j; Random rnd = new Random(); again: i = rnd.Next((pman.GetY() - 140) / 20, (pman.GetY() + 140) / 20); j = rnd.Next((pman.GetX() - 140) / 20, (pman.GetX() + 140) / 20); if ((i > 0) && (i < maze.GetLength(0)) && (j > 0) && (j < maze.GetLength(1))) { if (maze[i, j] == 1) { goto again; } } else { goto again; } this.x = j * 20; this.y = i * 20; }
private void Menu_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { if (PM2.GetY() == 303) { PM2.SetY(503); } else { PM2.SetY(PM2.GetY() - 50); } } if (e.KeyCode == Keys.Down) { if (PM2.GetY() == 503) { PM2.SetY(303); } else { PM2.SetY(PM2.GetY() + 50); } } if (e.KeyCode == Keys.Enter) { if (PM2.GetY() == 303) { Animation.Stop(); Visible = false; GameForm gf = new GameForm(); gf.ShowDialog(); gf.Close(); Animation.Start(); Visible = true; TopMost = true; Focus(); } if (PM2.GetY() == 353) { MenuState = 2; Animation.Stop(); grfx.DrawImage(Properties.Resources.MenuHelp, -10, 0, Width, Height - 50); } if (PM2.GetY() == 403) { MenuState = 2; Animation.Stop(); HighScore hs = new HighScore(); hs.ShowDialog(); hs.Close(); Animation.Start(); } if (PM2.GetY() == 453) { MenuState = 2; Animation.Stop(); grfx.DrawImage(Properties.Resources.MenuAbout, -10, 0, Width, Height - 50); } if (PM2.GetY() == 503) { Application.Exit(); } } if (e.KeyCode == Keys.Escape) { if (MenuState == 1) { Application.Exit(); } else { Animation.Start(); MenuState = 1; grfx.DrawImage(Properties.Resources.Menu, -10, 0, Width, Height - 50); } } }
public void ChasePacman(Pacman pman, int[,] maze) { int i = this.y / 20; int j = this.x / 20; int k = pman.GetX() / 20; int l = pman.GetY() / 20; if (((maze[i - 1, j] != 1) && (maze[i, j + 1] != 1)) || ((maze[i, j + 1] != 1) && (maze[i + 1, j] != 1)) || ((maze[i + 1, j] != 1) && (maze[i, j - 1] != 1)) || ((maze[i, j - 1] != 1) && (maze[i - 1, j] != 1))) { if (this.direction == (int)Directions.Up) { this.OppositeDir = (int)Directions.Down; } if (this.direction == (int)Directions.Down) { this.OppositeDir = (int)Directions.Up; } if (this.direction == (int)Directions.Left) { this.OppositeDir = (int)Directions.Right; } if (this.direction == (int)Directions.Right) { this.OppositeDir = (int)Directions.Left; } int moveScoreUp = this.OppositeDir != (int)Directions.Up ? EvaluateMove(k, l, j, i - 1, maze) : -1; int moveScoreDown = this.OppositeDir != (int)Directions.Down ? EvaluateMove(k, l, j, i + 1, maze) : -1; int moveScoreLeft = this.OppositeDir != (int)Directions.Left ? EvaluateMove(k, l, j - 1, i, maze) : -1; int moveScoreRight = this.OppositeDir != (int)Directions.Right ? EvaluateMove(k, l, j + 1, i, maze) : -1; int score = Max(moveScoreUp, moveScoreDown, moveScoreLeft, moveScoreRight); if (score == moveScoreUp) { direction = (int)Directions.Up; } if (score == moveScoreDown) { direction = (int)Directions.Down; } if (score == moveScoreLeft) { direction = (int)Directions.Left; } if (score == moveScoreRight) { direction = (int)Directions.Right; } } if (this.direction == (int)Directions.Up) { this.y -= 20; } if (this.direction == (int)Directions.Down) { this.y += 20; } if (this.direction == (int)Directions.Left) { this.x -= 20; } if (this.direction == (int)Directions.Right) { this.x += 20; } j = this.x / 20; if (j > maze.GetLength(1) - 2) { this.x = 20; } if (j < 1) { this.x = (maze.GetLength(1) - 2) * 20; } }
private void GameForm_KeyDown(object sender, KeyEventArgs e) { int i = Pman.GetY() / 20; int j = Pman.GetX() / 20; if (Dead) { return; } if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.W)) { if (maze.GetMaze()[i - 1, j] != 1) { Pman.SetDirection((int)Directions.Up); } PacmanTimer.Start(); GhostTimer.Start(); } if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.S)) { if (maze.GetMaze()[i + 1, j] != 1) { Pman.SetDirection((int)Directions.Down); } PacmanTimer.Start(); GhostTimer.Start(); } if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.A)) { if (maze.GetMaze()[i, j - 1] != 1) { Pman.SetDirection((int)Directions.Left); } PacmanTimer.Start(); GhostTimer.Start(); } if ((e.KeyCode == Keys.Right) || (e.KeyCode == Keys.D)) { if (maze.GetMaze()[i, j + 1] != 1) { Pman.SetDirection((int)Directions.Right); } PacmanTimer.Start(); GhostTimer.Start(); } if (e.KeyCode != Keys.Space || Pman.GetAmmo() <= 0 || ShootOnce) { return; } bullet.SetImage(Properties.Resources.Bullet); Pman.ReduceAmmo(); bullet.SetX(Pman.GetX() + 5); bullet.SetY(Pman.GetY() + 5); ShootOnce = true; bullet.SetDirection(Pman.GetDirection()); bullet.RotateBullet(); BulletTimer.Start(); }