Hey guys I have an assignment where the goal is to imitate pacman, and I'm having trouble with programming the intersections. The ghosts just glitch at the intersections, and go out of control, rather than running aroud smoothly. My programming knowledge isn't strong as I wasn't able to make it for most classes and got left behind so any help would be greatly appreciated! Here's a copy of my code.
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 { //Movement of the ghosts public final int UPNUMBER = 0; public final int DOWNNUMBER = 1; public final int RIGHTNUMBER = 2; public final int LEFTNUMBER = 3; // Movement constants public final String UP = "up"; public final String DOWN = "down"; public final String LEFT = "left"; public final String RIGHT = "right"; //Add and initialise Ghost variables here public boolean IntroSongPlayed = false; int imageCounter = 0; /** * Act method, runs on every frame */ public void act() { //Make the Ghost do things here playSoundOnce(); animateGhost(); ghostMovement(); trapped(); wrapAroundWorld(); } //Give the Ghost functions here private void playSoundOnce() { if (!IntroSongPlayed) { playPacmanIntro(); IntroSongPlayed = true; } } private void animateGhost() { if (imageCounter==10) //Every 10 frames, she will animate. { animate(); imageCounter=0; } else imageCounter++; } private void ghostMovement() { Random rnd = new Random(); int randomNumber = rnd.nextInt(4)-1; int direction = Greenfoot.getRandomNumber(4); if (canGhostMove() && corners()) { Greenfoot.getRandomNumber(4); move(1); } if (canGhostMove() && straightLine()) { Greenfoot.getRandomNumber(4); move(1); } if (canGhostMove() && isIntersection()) { Greenfoot.getRandomNumber(4); move(1); } if (canGhostMove() && twoDirections()) { Greenfoot.getRandomNumber(4); move(1); } if (canGhostMove() && threeDirections()) { Greenfoot.getRandomNumber(4); move(1); } if (twoDirections()) { switch (direction) { case UPNUMBER: setDirection(UP); break; case DOWNNUMBER: setDirection(DOWN); break; case RIGHTNUMBER: setDirection(RIGHT); break; case LEFTNUMBER: setDirection(LEFT); break; } } if (threeDirections()) { switch (direction) { case UPNUMBER: setDirection(UP); break; case DOWNNUMBER: setDirection(DOWN); break; case RIGHTNUMBER: setDirection(RIGHT); break; case LEFTNUMBER: setDirection(LEFT); break; } } if (corners()) { switch (direction) { case UPNUMBER: setDirection(UP); break; case DOWNNUMBER: setDirection(DOWN); break; case RIGHTNUMBER: setDirection(RIGHT); break; case LEFTNUMBER: setDirection(LEFT); break; } } if (isIntersection()) { switch (direction) { case UPNUMBER: setDirection(UP); break; case DOWNNUMBER: setDirection(DOWN); break; case RIGHTNUMBER: setDirection(RIGHT); break; case LEFTNUMBER: setDirection(LEFT); break; } } } public void trapped () { if (treeAbove() && treeToRight() && treeBelow()) { setDirection(LEFT); } } private boolean canGhostMove() { if(!isPacmanIntroStillPlaying() && !treeFront() ) { return true; } return false; } private boolean isIntersection() { if (!treeAbove() && !treeToRight() && !treeBelow() && !treeToLeft()) { return true; } return false; } private void WhenGhostisScared() { if(isScared()) { //move(1); } } private boolean straightLine() { if ((treeAbove() && treeBelow()) || (treeToLeft() && treeToRight())) { return true; } return false; } private boolean threeDirections() { if (!treeAbove() && !treeBelow() && !treeToLeft() || (!treeAbove() && !treeBelow() && !treeToRight() || (!treeAbove() && !treeToRight() && !treeToLeft() || !treeBelow() && !treeToRight() && !treeToLeft()))) { return true; } return false; } private boolean corners() { if (treeAbove() && treeToLeft() || (treeToLeft() && treeBelow() || (treeBelow() && treeToRight() || (treeToRight() && !treeAbove())))) { return true; } return false; } private boolean twoDirections() { if (!treeToLeft() && !treeToRight() && treeFront() || !treeAbove() && !treeBelow() && treeFront()) { return true; } return false; } }