Hey guys. I'm currently doing an assignment for uni, where we are asked to imitate a PacMan game using the given vocabulary (the relevant vocab necessary to help answer my question):
2. Even though I set the turning counter, they still seem to get stuck in random areas (bouncing left and right repeatedly). To be completely honest, I'm unsure as to what the '25' stands for, as initially I thought it symbolised how many squares (?) but I appear to be wrong. So the number I chose is completely random and based on how well the ghosts were running.
So my main question is how can I change my code to make the ghosts run normally like the ghosts in class Pacman? Below is the code currently in the ghost class.
To give a better idea on what the program looks like heres a print screen of the world:
http://i.imgur.com/EYcV99h.png
An example of the issues I'm dealing with is for example, in the ghost base they seem to be unable to exit the cave and instead continuously bounce between the trees.
By the way, I'm relatively bad at java.
treeFront(): Checks if there is a tree infront of the ghost.
treeAbove(): Checks if there is a tree above the ghost. (Regardless of the direction of the ghost and relative to the screen).
treeBelow: Checks if there is a tree below the ghost. (Regardless of the direction of the ghost and relative to the screen).
treeToLeft: Checks if there is a tree to the left of the ghost. (Regardless of the direction of the ghost and relative to the screen).
treeToRight: Checks if there is a tree to the right of the ghost. (Regardless of the direction of the ghost and relative to the screen).
setDirection(string): Makes the ghost face a specific direction. "up" - face toward the top of the screen, "down" - face toward the bottom of the screen, "right" - face toward the right, "left" - face toward the left.
I've had some difficulties coding the ghosts as randomly moving objects. The issues were:
1. They were glitching at intersections (which are sections where they can move top/left/right/down):
I fixed this by creating a turning counter, so that they could only turn once every so often.
//a variable to help keep the ghost from turning too much private boolean turning = false; //a counter for how much the ghost turns private int turningCounter = 25; //how much the ghost will turn private int turningSpeed = 25;
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.Random; /** * Ghost Class * * Available functions (see Assignment document for explanations on what each function does): * treeFront, treeAbove, treeBelow, treeToLeft, treeToRight, * getDirection, setDirection, * move, * isScared, * animate, animateDead, animateScared, * getClara, getGhostHealer, * isAboveMe, isBelowMe, isToMyLeft, isToMyRight, * makeClaraDead, * playGhostEatenSound, * isPacmanIntroStillPlaying, * wrapAroundWorld */ public class Ghost extends Character { //Add and initialise Ghost variables here //Part5: Speed of Ghost's movement. public int speedGhosts =1; //Part5: Ghost's animation private int imageCounter=0; private int moveConter=0; //Part5: Intro music public boolean hasIntroPlayed; //Part1: Has the actor reached the edge of the world. public boolean isAtEdge=false; public int number; //The direction Ghost is moving. private boolean left=true; private boolean right=true; private boolean down=false; private boolean up=false; //Stuck in the ghost base private boolean out; //a variable to help keep the ghost from turning too much private boolean turning = false; //a counter for how much the ghost turns private int turningCounter = 25; //how much the ghost will turn privateint turningSpeed = 25; /** * Act method, runs on every frame */ public void act() { //Make the Ghost do things here if ( isPacmanIntroStillPlaying()==false ) { //if ( treeFront() ) //{ ai(); ghostMove(); //} //else if ( !treeFront() ) //{ move ( speedGhosts ); animateGhost(); port(); ghostMoveConditions(); //} } } //Give the Ghost functions here /** * Part 5: Ghost's animation */ private void animateGhost() { if (imageCounter==10) //Every 10 frames, she will animate. { animate(); imageCounter=0; } else imageCounter++; } /** * Part 5: The ghost's will wrap around. */ private void port() { int x=getX(); //Get the X location of Clara. int y=getY(); //Get the Y location of Clara. if ( x<=0 || x>=370 || y<=0 || y>=430 ) { isAtEdge=true; } else isAtEdge=false; if ( isAtEdge==true ) { wrapAroundWorld(); isAtEdge=false; } } /** * Intro music */ private void introMusic() { hasIntroPlayed=false; if ( isPacmanIntroStillPlaying()== false && hasIntroPlayed==false ) { hasIntroPlayed=true; } } /** * If there is a tree infront, choose a random direction, */ private void ai() { Random rand = new Random(); int randomNumber = rand.nextInt(4)+1; if ( randomNumber == 1 && !turning ) { left=true; } else if ( randomNumber == 2 && !turning ) { right=true; } else if ( randomNumber == 3 && !turning ) { up=true; } else if ( randomNumber == 4 && !turning ) { down=true; } } private void ghostMove() { if ( left && !treeToLeft() && turning == false ) { setDirection("left"); turning = true; } else if ( right && !treeToRight() && turning == false ) { setDirection("right"); turning = true; } else if ( up && !treeAbove() && turning == false ) { setDirection("up"); turning = true; } else if ( down && !treeBelow() && turning == false ) { setDirection("down"); turning = true; } turningCounter(); } private void ghostMoveConditions() { if ( left ) { //80% chance to go up if able to if ( !treeAbove() && Greenfoot.getRandomNumber(100)>80 ) { up = true; left = false; } //75% chance to go down if able to else if ( !treeBelow()&& Greenfoot.getRandomNumber(100)>75 ) { down = true; left = false; } //99% else if ( !treeToRight() && Greenfoot.getRandomNumber(100)>50 ) { right = true; left = false; } //To ensure the ghost does not walk backward //Otherwise, left remains true and the direction will be set to left. } //No tree to the right else if ( right ) { //80% chance to go up if able to if ( !treeAbove() && Greenfoot.getRandomNumber(100)>80 ) { up = true; right = false; } //75% chance to go down if able to else if ( !treeBelow() && Greenfoot.getRandomNumber(100)>75 ) { down = true; right = false; } //99% chance to go left if able to else if ( !treeToLeft() && Greenfoot.getRandomNumber(100)>50 ) { left = true; right = false; } //Otherwise right remains and true and the direction wil be set to right } else if ( up ) { //80% chance to go left if ( !treeToLeft() && Greenfoot.getRandomNumber(1000)>80 ) { left = true; up = false; } //75% chance to go right else if ( !treeToRight() && Greenfoot.getRandomNumber(100)>75 ) { right = true; up = false; } //99% chance to go down else if ( !treeBelow() && Greenfoot.getRandomNumber(100)>50 ) { down = true; up = false; } } else if ( down ) { //80% chance to go left if ( !treeToLeft() && Greenfoot.getRandomNumber(100)>80 ) { left = true; down = false; } //75% chance to go right else if ( !treeToRight() && Greenfoot.getRandomNumber(100)>75 ) { right = true; down = false; } //99% chance to go up else if ( !treeAbove() && Greenfoot.getRandomNumber(100)>50) { up = true; down = false; } } ghostMove(); } /** * a counter that makes sure that the object does not turn serveral times in the same second */ private void turningCounter() { if( turning == true && turningCounter < 0) { turning = false; turningCounter = turningSpeed; } else if( turning == true && turningCounter >= 0) { turningCounter --; } } }