public Form1() { InitializeComponent(); rand = new Random(); dataGrid = new Grid("TileMap.csv"); Controls.Add(dataGrid); sound = new System.Windows.Media.MediaPlayer(); sound.Open(new Uri("pacmanDubHeavy.mp3", UriKind.Relative)); sound.Play(); gameEngine = new GameEngine(dataGrid, rand, timer1.Interval); //pacman font PrivateFontCollection fontCollection = new PrivateFontCollection(); fontCollection.AddFontFile("crackman.ttf"); FontFamily ff = fontCollection.Families[0]; int fontsize = 12; Font pacmanFont = new Font(ff, fontsize, FontStyle.Bold); label2.Font = pacmanFont; label3.Font = pacmanFont; label4.Font = pacmanFont; lifeCounter = new PictureBox[3]; lifeCounter[0] = pictureBox1; lifeCounter[1] = pictureBox2; lifeCounter[2] = pictureBox3; }
public GameEngine(Grid dataGrid, Random rand, int tickInterval) { this.dataGrid = dataGrid; this.rand = rand; dataGrid.Paint += new PaintEventHandler(dataGrid_Paint); pacman = new PacMan(); ghost = new Ghost[4]; ghost[0] = new Ghost("Purple"); ghost[1] = new Ghost("Red"); ghost[2] = new Ghost("Blue"); ghost[3] = new Ghost("Green"); gameOver = false; }
/// <summary> /// Changes PacMan's position to a new valid position /// </summary> /// <param name="dataGrid"></param> public void Move(Grid dataGrid) { //checks to see if pacman is aligned with a cell if ((location.X % IMAGE_SIZE == 0) && (location.Y % IMAGE_SIZE == 0)) { Point location_cellIndex; location_cellIndex = new Point((location.X / IMAGE_SIZE), (location.Y / IMAGE_SIZE)); Point attemptedLocation = location_cellIndex; //defines the location the user is attempting to move to switch (attemptedDirection) { case Direction.Up: attemptedLocation.Y--; break; case Direction.Left: attemptedLocation.X--; break; case Direction.Right: attemptedLocation.X++; break; case Direction.Down: attemptedLocation.Y++; break; default: break; } //checks to see if the next cell in the direction pacman wants to go in is valid if (dataGrid.CheckForWall(attemptedLocation) == false) { direction = attemptedDirection; imageToggle++; imageToggle %= ANIMATION_FRAMES; } attemptedLocation = location_cellIndex; //attempted direction shift was unsuccessful, so instead pacman carries on the way he was going switch (direction) { case Direction.Up: attemptedLocation.Y--; break; case Direction.Left: attemptedLocation.X--; break; case Direction.Right: attemptedLocation.X++; break; case Direction.Down: attemptedLocation.Y++; break; default: break; } if (dataGrid.CheckForWall(attemptedLocation) == false) { switch (direction) { case Direction.Up: location.Y -= speed; break; case Direction.Left: location.X -= speed; break; case Direction.Right: location.X += speed; break; case Direction.Down: location.Y += speed; break; default: break; } imageToggle++; imageToggle %= ANIMATION_FRAMES; } } else { switch (direction) { case Direction.Up: location.Y -= speed; break; case Direction.Left: location.X -= speed; break; case Direction.Right: location.X += speed; break; case Direction.Down: location.Y += speed; break; default: break; } imageToggle++; imageToggle %= ANIMATION_FRAMES; } }
/// <summary> /// PacMan eats the contents of his location. /// </summary> /// <param name="dataGrid"></param> public bool Eat(Grid dataGrid) { bool bigKibbleEaten = false; if ((location.X % IMAGE_SIZE == 0) && (location.Y % IMAGE_SIZE == 0)) { Point location_cellIndex; location_cellIndex = new Point((location.X / IMAGE_SIZE), (location.Y / IMAGE_SIZE)); if (dataGrid.ClearCell(location_cellIndex) == true) { bigKibbleEaten = true; } } return bigKibbleEaten; }
/// <summary> /// Randomises the direction the ghost moves in. /// Specifies that a ghost should not move in the opposite direction of the one he's currently moving in. /// </summary> /// <param name="rand"></param> /// <param name="dataGrid"></param> public void RandomiseDirection(Random rand, Grid dataGrid) { int nPossibleDirections = Enum.GetNames(typeof(Direction)).Length; Direction tempDirection = (Direction)rand.Next(nPossibleDirections); Point attemptedLocation = GetNextLocation(tempDirection); //while the direction randomised is the opposite direction of the one he's currently moving in, randomise again. while (((int)tempDirection == (((int)direction + 2) % nPossibleDirections)) || (dataGrid.CheckForWall(attemptedLocation) == true)) { tempDirection = (Direction)rand.Next(nPossibleDirections); attemptedLocation = GetNextLocation(tempDirection); } direction = tempDirection; }
/// <summary> /// Changes the ghost's position to a new valid position /// </summary> /// <param name="rand"></param> /// <param name="dataGrid"></param> public void Move(Random rand, Grid dataGrid) { //checks to see if the ghost is perfectly aligned with a cell if ((location.X % IMAGE_SIZE == 0) && (location.Y % IMAGE_SIZE == 0)) { location_cellIndex = new Point((location.X / IMAGE_SIZE), (location.Y / IMAGE_SIZE)); RandomiseDirection(rand, dataGrid); switch (direction) { case Direction.Up: location.Y -= speed; break; case Direction.Left: location.X -= speed; break; case Direction.Right: location.X += speed; break; case Direction.Down: location.Y += speed; break; default: break; } imageToggle++; imageToggle %= ANIMATION_FRAMES; } else { switch (direction) { case Direction.Up: location.Y -= speed; break; case Direction.Left: location.X -= speed; break; case Direction.Right: location.X += speed; break; case Direction.Down: location.Y += speed; break; default: break; } imageToggle++; imageToggle %= ANIMATION_FRAMES; } }