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

2013/5/26

Enemy starting spot help

jaydjay jaydjay

2013/5/26

#
I am trying to create a basic game of pacman. For the ghosts, it is moving however, it doesn't come out of the healer. I have tried to set the location but the ghost doesn't move after the game starts. What should i do for the ghost to move on the maze freely?
Gevater_Tod4711 Gevater_Tod4711

2013/5/26

#
The easyest way would be to use a random direction in which the gosts should move. But to improve your code it would be very helpfull to know how you code looks like until now.
jaydjay jaydjay

2013/5/27

#
Gevater_Tod4711 wrote...
The easyest way would be to use a random direction in which the gosts should move. But to improve your code it would be very helpfull to know how you code looks like until now.
public void act()
    {
        ghostAi();
        
        if ( !wallFront() )
        {
            int a = Greenfoot.getRandomNumber(100);
            if ( a <= 50 )
            {
                setDirection(LEFT);
                if( !wallFront() )
                {
                    move(3); 
                    animate();
                }
            }
            else 
            {
                setDirection(RIGHT);
                if ( !wallFront() )
                {
                    move(3);
                    animate();
                }
            }
        }
public void ghostAi()
    {
        if ( wallAbove() && !wallBelow() && !wallLeft() && !wallRight() )
        {
            int a = Greenfoot.getRandomNumber(100);
            {
                if ( a <= 33 )
                {
                    setDirection(RIGHT);
                }
                if ( a > 33 && a <=66 )
                {
                    setDirection(LEFT);
                }
                if ( a > 66 && a <=100)
                {
                    setDirection(DOWN);
                }
            }
        }
        
        if ( !wallAbove() && wallBelow() && !wallLeft() && !wallRight() )
        {
            int a = Greenfoot.getRandomNumber(100);
            {
                if ( a <= 33 )
                {
                    setDirection(RIGHT);
                }
                if ( a > 33 && a <=66 )
                {
                    setDirection(LEFT);
                }
                if ( a > 66 && a <=100)
                {
                    setDirection(UP);
                }
            }
        }
        
        if ( !wallAbove() && !wallBelow() && wallLeft() && !wallRight() )
        {
            int a = Greenfoot.getRandomNumber(100);
            {
                if ( a <= 33 )
                {
                    setDirection(RIGHT);
                }
                if ( a > 33 && a <=66 )
                {
                    setDirection(UP);
                }
                if ( a > 66 && a <=100)
                {
                    setDirection(DOWN);
                }
            }
        }
        
        if ( !wallAbove() && !wallBelow() && !wallLeft() && wallRight() )
        {
            int a = Greenfoot.getRandomNumber(100);
            {
                if ( a <= 33 )
                {
                    setDirection(UP);
                }
                if ( a > 33 && a <=66 )
                {
                    setDirection(LEFT);
                }
                if ( a > 66 && a <=100)
                {
                    setDirection(DOWN);
                }
            }
        }
        
        if ( !wallAbove() && wallBelow() && !wallLeft() && !wallRight() )
        {
            int a = Greenfoot.getRandomNumber(100);
            {
                if ( a <= 33 )
                {
                    setDirection(UP);
                }
                if ( a > 33 && a <=66 )
                {
                    setDirection(LEFT);
                }
                if ( a > 66 && a <=100)
                {
                    setDirection(RIGHT);
                }
            }
        }
jaydjay jaydjay

2013/5/28

#
anyone?
Gevater_Tod4711 Gevater_Tod4711

2013/5/28

#
The problem in your ghostAi method is that the gosts just move if there is only one possible direction. If there are two or more they don't do anything.
jaydjay jaydjay

2013/5/28

#
Gevater_Tod4711 wrote...
The problem in your ghostAi method is that the gosts just move if there is only one possible direction. If there are two or more they don't do anything.
However i have set it to allow the ghost to move if there is no wall on top of it. Also, it cant get past the ghost healer. Any reason why? When i am running my code right now it moves to the right and then goes back to the left and stops. ( it also turns 90degrees )
Gevater_Tod4711 Gevater_Tod4711

2013/5/28

#
I think your ghostAi method is the problem. Try using this method:
public void ghostAi() {
    boolean[] possibleDirections = new boolean[4];
    int possibleDirectionNumber;
    int random;
    int direction = -1;
    if (!wallRight()) {
        possibleDirections[0] = true;
    }
    if (!wallDown()) {
        possibleDirections[1] = true;
    }
    if (!wallLeft()) {
        possibleDirections[2] = true;
    }
    if (!wallUp()) {
        possibleDirections[3] = true;
    }
    for (boolean b : possibleDirections) {
        if (b) {
            possibleDirectionNumber++;
        }
    }
    if (possibleDirectionNumber > 0) {
        random = Greenfoot.getRandomNumber(possibleDirections);
        for (int i = 0, j = 0; i < possibleDirections.length; i++) {
            if (possibleDirections[i]) {
                if (random == j) {
                    direction = i;
                }
                j++;
            }
        }
        switch (direction) {
            case 0:
                setDirection(RIGHT);
                break;
            case 1:
                setDirection(DOWN);
                break;
            case 2:
                setDirection(LEFT);
                break;
            case 3:
                setDirection(UP);
                break;
        }
    }
}
You need to login to post a reply.