Hey guys. I'm currently doing an assignment for uni, where we are asked to imitate a PacMan game using the given vocabulary (the relevant vocab necessary to help answer my question):
2. Even though I set the turning counter, they still seem to get stuck in random areas (bouncing left and right repeatedly). To be completely honest, I'm unsure as to what the '25' stands for, as initially I thought it symbolised how many squares (?) but I appear to be wrong. So the number I chose is completely random and based on how well the ghosts were running.
So my main question is how can I change my code to make the ghosts run normally like the ghosts in class Pacman? Below is the code currently in the ghost class.
To give a better idea on what the program looks like heres a print screen of the world:
http://i.imgur.com/EYcV99h.png
An example of the issues I'm dealing with is for example, in the ghost base they seem to be unable to exit the cave and instead continuously bounce between the trees.
By the way, I'm relatively bad at java.
treeFront(): Checks if there is a tree infront of the ghost.
treeAbove(): Checks if there is a tree above the ghost. (Regardless of the direction of the ghost and relative to the screen).
treeBelow: Checks if there is a tree below the ghost. (Regardless of the direction of the ghost and relative to the screen).
treeToLeft: Checks if there is a tree to the left of the ghost. (Regardless of the direction of the ghost and relative to the screen).
treeToRight: Checks if there is a tree to the right of the ghost. (Regardless of the direction of the ghost and relative to the screen).
setDirection(string): Makes the ghost face a specific direction. "up" - face toward the top of the screen, "down" - face toward the bottom of the screen, "right" - face toward the right, "left" - face toward the left.
I've had some difficulties coding the ghosts as randomly moving objects. The issues were:
1. They were glitching at intersections (which are sections where they can move top/left/right/down):
I fixed this by creating a turning counter, so that they could only turn once every so often.
1 2 3 4 5 6 | //a variable to help keep the ghost from turning too much private boolean turning = false ; //a counter for how much the ghost turns private int turningCounter = 25 ; //how much the ghost will turn private int turningSpeed = 25 ; |
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 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 { //Add and initialise Ghost variables here //Part5: Speed of Ghost's movement. public int speedGhosts = 1 ; //Part5: Ghost's animation private int imageCounter= 0 ; private int moveConter= 0 ; //Part5: Intro music public boolean hasIntroPlayed; //Part1: Has the actor reached the edge of the world. public boolean isAtEdge= false ; public int number; //The direction Ghost is moving. private boolean left= true ; private boolean right= true ; private boolean down= false ; private boolean up= false ; //Stuck in the ghost base private boolean out; //a variable to help keep the ghost from turning too much private boolean turning = false ; //a counter for how much the ghost turns private int turningCounter = 25 ; //how much the ghost will turn privateint turningSpeed = 25 ; /** * Act method, runs on every frame */ public void act() { //Make the Ghost do things here if ( isPacmanIntroStillPlaying()== false ) { //if ( treeFront() ) //{ ai(); ghostMove(); //} //else if ( !treeFront() ) //{ move ( speedGhosts ); animateGhost(); port(); ghostMoveConditions(); //} } } //Give the Ghost functions here /** * Part 5: Ghost's animation */ private void animateGhost() { if (imageCounter== 10 ) //Every 10 frames, she will animate. { animate(); imageCounter= 0 ; } else imageCounter++; } /** * Part 5: The ghost's will wrap around. */ private void port() { int x=getX(); //Get the X location of Clara. int y=getY(); //Get the Y location of Clara. if ( x<= 0 || x>= 370 || y<= 0 || y>= 430 ) { isAtEdge= true ; } else isAtEdge= false ; if ( isAtEdge== true ) { wrapAroundWorld(); isAtEdge= false ; } } /** * Intro music */ private void introMusic() { hasIntroPlayed= false ; if ( isPacmanIntroStillPlaying()== false && hasIntroPlayed== false ) { hasIntroPlayed= true ; } } /** * If there is a tree infront, choose a random direction, */ private void ai() { Random rand = new Random(); int randomNumber = rand.nextInt( 4 )+ 1 ; if ( randomNumber == 1 && !turning ) { left= true ; } else if ( randomNumber == 2 && !turning ) { right= true ; } else if ( randomNumber == 3 && !turning ) { up= true ; } else if ( randomNumber == 4 && !turning ) { down= true ; } } private void ghostMove() { if ( left && !treeToLeft() && turning == false ) { setDirection( "left" ); turning = true ; } else if ( right && !treeToRight() && turning == false ) { setDirection( "right" ); turning = true ; } else if ( up && !treeAbove() && turning == false ) { setDirection( "up" ); turning = true ; } else if ( down && !treeBelow() && turning == false ) { setDirection( "down" ); turning = true ; } turningCounter(); } private void ghostMoveConditions() { if ( left ) { //80% chance to go up if able to if ( !treeAbove() && Greenfoot.getRandomNumber( 100 )> 80 ) { up = true ; left = false ; } //75% chance to go down if able to else if ( !treeBelow()&& Greenfoot.getRandomNumber( 100 )> 75 ) { down = true ; left = false ; } //99% else if ( !treeToRight() && Greenfoot.getRandomNumber( 100 )> 50 ) { right = true ; left = false ; } //To ensure the ghost does not walk backward //Otherwise, left remains true and the direction will be set to left. } //No tree to the right else if ( right ) { //80% chance to go up if able to if ( !treeAbove() && Greenfoot.getRandomNumber( 100 )> 80 ) { up = true ; right = false ; } //75% chance to go down if able to else if ( !treeBelow() && Greenfoot.getRandomNumber( 100 )> 75 ) { down = true ; right = false ; } //99% chance to go left if able to else if ( !treeToLeft() && Greenfoot.getRandomNumber( 100 )> 50 ) { left = true ; right = false ; } //Otherwise right remains and true and the direction wil be set to right } else if ( up ) { //80% chance to go left if ( !treeToLeft() && Greenfoot.getRandomNumber( 1000 )> 80 ) { left = true ; up = false ; } //75% chance to go right else if ( !treeToRight() && Greenfoot.getRandomNumber( 100 )> 75 ) { right = true ; up = false ; } //99% chance to go down else if ( !treeBelow() && Greenfoot.getRandomNumber( 100 )> 50 ) { down = true ; up = false ; } } else if ( down ) { //80% chance to go left if ( !treeToLeft() && Greenfoot.getRandomNumber( 100 )> 80 ) { left = true ; down = false ; } //75% chance to go right else if ( !treeToRight() && Greenfoot.getRandomNumber( 100 )> 75 ) { right = true ; down = false ; } //99% chance to go up else if ( !treeAbove() && Greenfoot.getRandomNumber( 100 )> 50 ) { up = true ; down = false ; } } ghostMove(); } /** * a counter that makes sure that the object does not turn serveral times in the same second */ private void turningCounter() { if ( turning == true && turningCounter < 0 ) { turning = false ; turningCounter = turningSpeed; } else if ( turning == true && turningCounter >= 0 ) { turningCounter --; } } } |