private void Animation_Tick(object sender, EventArgs e) { _dBgrfx.DrawImage(Properties.Resources.Menu, -10, 0, Width, Height - 50); PM1.SetX(PM1.GetX() + 15); RG.SetX(RG.GetX() + 15); BG.SetX(BG.GetX() + 15); YG.SetX(YG.GetX() + 15); PG.SetX(PG.GetX() + 15); if (PacmanPicState == 1) { PM1.SetImage(Properties.Resources.Pacman2); PacmanPicState = 2; } else { PM1.SetImage(Properties.Resources.Pacman1); PacmanPicState = 1; } if (PM1.GetX() > Width) { PM1.SetX(0); } if (RG.GetX() > Width) { RG.SetX(0); } if (BG.GetX() > Width) { BG.SetX(0); } if (YG.GetX() > Width) { YG.SetX(0); } if (PG.GetX() > Width) { PG.SetX(0); } PM1.DrawItem(_dBgrfx); PM2.DrawItem(_dBgrfx); RG.DrawItem(_dBgrfx); BG.DrawItem(_dBgrfx); YG.DrawItem(_dBgrfx); PG.DrawItem(_dBgrfx); grfx.DrawImage(_backBuffer, 0, 0); }
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; }
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(); }