This site requires JavaScript, please enable it in your browser!
Greenfoot back
madhu99
madhu99 wrote ...

2021/2/9

help woth pacman ghosts

madhu99 madhu99

2021/2/9

#
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; } } this is the code till now. can somebody help me what are missing?
Super_Hippo Super_Hippo

2021/2/9

#
Are you trying to copy the original behavior?
You need to login to post a reply.