//Get target coordinates //Отримання координатів цілі public override Point GetTarget() { if (travellingHome) { return(AboveHome); } else if (scatter) { return(scatterTarget); } else { Point myTarget = PacForm.pixelToCoord(form.pacman.location); switch (form.pacman.direction) { case 0: myTarget.Y -= 4; break; case 1: myTarget.X -= 4; break; case 2: myTarget.Y += 4; break; case 3: myTarget.X += 4; break; } return(myTarget); } }
//Find the path to the target that is the shortest //Знаходження найкоротшого шляху до цілі private int FindShortestPathToTarget(bool[] array, Point target) { int[] pathLengths = new int[4]; for (int i = 0; i < array.Length; i++) { if (array[i] == true) { Point p = PacForm.pixelToCoord(location); p.X += directions[i].X; p.Y += directions[i].Y; int distance = (p.X - target.X) * (p.X - target.X) + (p.Y - target.Y) * (p.Y - target.Y); pathLengths[i] = distance; } } int min = int.MaxValue; int minIndex = 0; for (int i = 0; i < pathLengths.Length; i++) { if (array[i] == true && pathLengths[i] < min) { min = pathLengths[i]; minIndex = i; } } return(minIndex); }
//Is ghost slowed inside a tunnel? Чи уповільнений привид у тунелі? bool insideTunnel() { if (form.items.itemsMap[PacForm.pixelToCoord(location).Y, PacForm.pixelToCoord(location).X] == 5) { return(true); } else { return(false); } }
//Get target coordinates //Отримання координатів цілі public override Point GetTarget() { if (travellingHome) { return(AboveHome); } else if (scatter) { return(scatterTarget); } else { return(PacForm.pixelToCoord(form.pacman.location)); } }
void ChooseRandomDirection() { currentCoordinate = PacForm.pixelToCoord(location); if (currentCoordinate != previousCoordinate) { previousCoordinate = currentCoordinate; bool[] whereToGo = LookAround(); int nDirections = CountDirections(whereToGo); if (nDirections > 1) { nextDirection = FindRandomDirection(whereToGo); } else { nextDirection = Math.Abs(nDirections); } } }
//Determines if movement is possible. Визначає, чи можливий рух public bool CanMove(int dir) { Point nextCoord = PacForm.pixelToCoord(location); switch (dir) { case 0: if (form.board.ValidMove(new Point(nextCoord.X, nextCoord.Y - 1))) { return(true); } break; case 1: if ((nextCoord.X - 1) < 0) { nextCoord = new Point(nextCoord.X + form.board.size.Width, nextCoord.Y); } if (form.board.ValidMove(new Point(nextCoord.X - 1, nextCoord.Y))) { return(true); } break; case 2: if (form.board.ValidMove(new Point(nextCoord.X, nextCoord.Y + 1))) { return(true); } break; case 3: if ((nextCoord.X + 1) >= form.board.size.Width) { nextCoord = new Point(nextCoord.X - form.board.size.Width, nextCoord.Y); } if (form.board.ValidMove(new Point(nextCoord.X + 1, nextCoord.Y))) { return(true); } break; } return(false); }
//Check for collisions with Pac-Man //Перевірка зіткнень привида і Пакмена private void PacManCollision() { //if (form.pacman.energized && PacForm.pixelToCoord(form.pacman.location) == PacForm.pixelToCoord(location)) //if (PacForm.pixelToCoord(location).X < 14) AboveHome = LeftAboveHome; //else AboveHome = RightAboveHome; if ((form.pacman.energized || form.energyBtn.Checked) && !travellingHome && !followingWaypoints && PacForm.pixelToCoord(form.pacman.location) == PacForm.pixelToCoord(location)) { wmp_eat_ghost.Stop(); wmp_eat_ghost.Open(new Uri("Sounds/eat_ghost.wav", UriKind.Relative)); //wmp_eat_ghost.Position = new TimeSpan(0); wmp_eat_ghost.Play(); travellingHome = true; if (travellingGhosts == 0) { eatenAll = false; } travellingGhostsPrivate = travellingGhosts; travellingGhosts++; form.player.adjustScore((int)Math.Pow(2, travellingGhosts) * 100); if (travellingGhosts == 4 && !eatenAll) { wmp_extend.Stop(); wmp_extend.Open(new Uri("Sounds/extend.wav", UriKind.Relative)); //wmp_extend.Position = new TimeSpan(0); wmp_extend.Play(); form.player.adjustScore(12000); eatenAll = true; } showScore = true; //form.LastTime = DateTime.Now; showScoreTimer.Start(); } if (!form.pacman.energized && !travellingHome && !followingWaypoints && PacForm.pixelToCoord(form.pacman.location) == PacForm.pixelToCoord(location)) { if (!form.invincBtn.Checked) { currentSprite = emptySprite; form.pacman.Die(); } } }
//Get target coordinates //Отримання координатів цілі public override Point GetTarget() { if (travellingHome) { return(AboveHome); } else if (scatter) { return(scatterTarget); } else { Point myTarget = new Point(); Point pacmanLoc = PacForm.pixelToCoord(form.pacman.location); Point blinkyLoc = PacForm.pixelToCoord(form.blinky.location); myTarget.X = pacmanLoc.X + (pacmanLoc.X - blinkyLoc.X); myTarget.Y = pacmanLoc.Y + (pacmanLoc.Y - blinkyLoc.Y); return(myTarget); } }
//Choose where to go. Вибір напрямку руху void ChooseDirection() { currentCoordinate = PacForm.pixelToCoord(location); if (currentCoordinate != previousCoordinate) { previousCoordinate = currentCoordinate; bool[] whereToGo = LookAround(); int nDirections = CountDirections(whereToGo); if (nDirections > 1) { nextDirection = FindShortestPathToTarget(whereToGo, GetTarget()); } else { nextDirection = Math.Abs(nDirections); } if (currentCoordinate == AboveHome && travellingHome) { followingWaypoints = true; } } }
//Get target coordinates //Отримання координатів цілі public override Point GetTarget() { if (travellingHome) { return(AboveHome); } else if (scatter) { return(scatterTarget); } else { Point pacmanLoc = PacForm.pixelToCoord(form.pacman.location); Point myLoc = PacForm.pixelToCoord(location); if (FindDistance(myLoc, pacmanLoc) < 9) { return(scatterTarget); } else { return(pacmanLoc); } } }
//Pac-Man's moving logic. Керує логікою руху пакмена public void Move(float elapsed) { if (sleepAfterLunch == 0) { GetLife(); if (energized) { speed = form.levels.speed * form.levels.get_frightPacmanSpeed() * elapsed; } else { speed = form.levels.speed * form.levels.get_pacmanSpeed() * elapsed; } //speed = 3f; form.items.Eat(PacForm.pixelToCoord(location)); if (CanMove(nextDirection)) { direction = nextDirection; } //else nextDirection = direction; if (CanMove(direction)) { moving = true; switch (direction) { case 0: location.Y -= speed; centerX(); break; case 1: location.X -= speed; centerY(); break; case 2: location.Y += speed; centerX(); break; case 3: location.X += speed; centerY(); break; } } else { moving = false; //don't center when game starts if (keyPressed) { centerX(); centerY(); } } if (location.X < 0) { location = new PointF(location.X + form.board.boardPBSize.Width, location.Y); } if (location.X >= form.board.boardPBSize.Width) { location = new PointF(location.X - form.board.boardPBSize.Width, location.Y); } } else { sleepAfterLunch--; } }
//Usual movement. Звичайний рух void NormalMovement() { switch (direction) { case 0: PacManCollision(); location = new PointF(location.X, location.Y -= speed); if (location.Y % PacForm.boardUnitSize < PacForm.unitCenter) { if (reverseDirection) { reverseDirection = false; ReverseDirection(); } else { direction = nextDirection; location.X = PacForm.coordToPixelCenter(PacForm.pixelToCoord(location)).X; } } PacManCollision(); break; case 1: PacManCollision(); location = new PointF(location.X -= speed, location.Y); if (location.X % PacForm.boardUnitSize < PacForm.unitCenter) { if (reverseDirection) { reverseDirection = false; ReverseDirection(); } else { direction = nextDirection; location.Y = PacForm.coordToPixelCenter(PacForm.pixelToCoord(location)).Y; } } PacManCollision(); break; case 2: PacManCollision(); location = new PointF(location.X, location.Y += speed); if (location.Y % PacForm.boardUnitSize > PacForm.unitCenter) { if (reverseDirection) { reverseDirection = false; ReverseDirection(); } else { direction = nextDirection; location.X = PacForm.coordToPixelCenter(PacForm.pixelToCoord(location)).X; } } PacManCollision(); break; case 3: PacManCollision(); location = new PointF(location.X += speed, location.Y); if (location.X % PacForm.boardUnitSize > PacForm.unitCenter) { if (reverseDirection) { reverseDirection = false; ReverseDirection(); } else { direction = nextDirection; location.Y = PacForm.coordToPixelCenter(PacForm.pixelToCoord(location)).Y; } } PacManCollision(); break; } if (location.X < 0) { location = new PointF(location.X + form.board.boardPBSize.Width, location.Y); } if (location.X >= form.board.boardPBSize.Width) { location = new PointF(location.X - form.board.boardPBSize.Width, location.Y); } }