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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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:
1
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.