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

2015/5/29

Need help with pacman ghosts!

solidsnake solidsnake

2015/5/29

#
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;
    }
    

}
solidsnake solidsnake

2015/5/29

#
Here is an update on what I have done since posting it up:
    private void ghostMovement()
    {
        Random rnd = new Random();
        int randomNumber = rnd.nextInt(4);
        
        int direction = Greenfoot.getRandomNumber(4);
                 if (canGhostMove() && corners())
        {
            Greenfoot.getRandomNumber(4);
            move(1);
            getClara();
        }
        if (canGhostMove() && straightLine())
        {
            Greenfoot.getRandomNumber(4);
            move(1);
            getClara();
        }
        if (canGhostMove() && isIntersection())
        {
            Greenfoot.getRandomNumber(4);
            move(1);
            getClara();
        }
        if (canGhostMove() && twoDirections())
        {
            Greenfoot.getRandomNumber(4);
            move(1);
            getClara();
        }
        if (canGhostMove() && threeDirections())
        {
            Greenfoot.getRandomNumber(4);
            move(1);
            getClara();
        }
        
        if(treeFront() && corners() || twoDirections() || threeDirections())
        {
            if (direction == 0)
            {
                setDirection(UP);
                move(1);
            }
            if (direction == 1)
            {
                setDirection(DOWN);
                move(1);
            }
            if (direction == 2)
            {
                setDirection(RIGHT);
                move(1);
            }
            if (direction == 3)
            {
                setDirection(LEFT);
                move(1);
            }
            
            
        }
    }
Super_Hippo Super_Hippo

2015/5/29

#
I don't think I really understand the problem... but:
Greenfoot.getRandomNumber(4);
This line doesn't do anything. I don't see a 'getClara' method, but if it just does what the name suggest, then it doesn't do anything either.
danpost danpost

2015/5/29

#
There are major issues with the code you are giving -- mainly in the 'ghostMovement' method. One is that there is no guarantee that the randomly generated value of 'direction' will be in the direction of one of the available paths. You need to determine which paths are available and randomly choose from those. Also, it seems that it may be possible for multiple 'move(1)' statements are executed in the same act cycle. To be consistent, the actors should only be allowed to move once per act. Before you had updated your code, I was going to show a simplified version of your 'ghostMovement' method. I will go ahead a show you what it looks like anyway, so you can follow it through to see what it was doing at the time:
private void ghostMovement()
{
    if (canGhostMove() && corners()) move(1);
    if (canGhostMove() && straightLine()) move(1);
    if (canGhostMove() && isIntersection()) move(1);
    if (canGhostMove() && twoDirections()) move(1);
    if (canGhostMove() && threeDirections()) move(1);
    if (twoDirections() || threeDirections() || corners() || isIntersection())
    {
        switch (Greenfoot.getRandomNumber(4)) 
        {
            case    UPNUMBER: setDirection(UP); break;
            case  DOWNNUMBER: setDirection(DOWN); break;
            case RIGHTNUMBER: setDirection(RIGHT); break;
            case  LEFTNUMBER: setDirection(LEFT); break;
        }
    }
}
Here, lines 3 through 7 are questionable as far as what behavior your actors will have. If your world is gridded (the cell-size is greater than one), then if Clara was initially on a 'straightLine' and moves forward, and then ends up at an intersection, it will move forward again and be out of the intersection (there will be no chance to change directions there, which I am sure is not the behavior you want).
You need to login to post a reply.