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

2013/5/20

Pacman ghosts problems

chicarito chicarito

2013/5/20

#
Hey guys, im making a pacman game but im stuck because my ghosts will only change direction when they hit a wall. Seeing i have a world with walls in circles, they will only go around constantly. I hope you guys know what i mean... Heres my code until now: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GhostBlue here. * * @author (your name) * @version (a version number or a date) */ public class GhostBlue extends Ghost { private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; public int xturn; private int direction; public GhostBlue() { setDirection(NORTH); } public void act() { if(canMove()) { move(); } else{ turn(); } } public void move() { World myWorld = getWorld(); int x = getX(); int y = getY(); if (direction == SOUTH && WallSouth()){ setLocation(getX(), getY() + 1); } else if ( direction == EAST && WallEast()){ setLocation(getX() + 1, getY()); } else if ( direction == NORTH && WallNorth()){ setLocation(getX(), getY() - 1); } else if ( direction == WEST && WallWest()){ setLocation(getX() - 1, getY()); } else{ turn(); } } public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); if (x >= myWorld.getWidth() || y >= myWorld.getHeight()){ return false; } else if (x < 0 || y < 0){ return false; } return true; } public void turn() { xturn = Greenfoot.getRandomNumber(2); if (xturn == 0){ turnRight(); } else if (xturn == 1){ turnLeft(); } } public void turnRight() { if (direction == SOUTH){ setDirection(WEST); } else if ( direction == EAST){ setDirection(SOUTH); } else if ( direction == NORTH){ setDirection(EAST); } else if ( direction == WEST){ setDirection(NORTH); } } public void turnLeft() { if (direction == SOUTH){ setDirection(EAST); } else if ( direction == EAST){ setDirection(NORTH); } else if ( direction == NORTH){ setDirection(WEST); } else if ( direction == WEST){ setDirection(SOUTH); } } public void setDirection(int direction) { this.direction = direction; } public boolean WallNorth() { Actor Around = getOneObjectAtOffset(0, -15, Around.class); Actor WallIn = getOneObjectAtOffset(0, -15, WallIn.class); if(Around != null){ return false; } else if (WallIn != null){ return false; }else{ return true; } } public boolean WallEast() { Actor Around = getOneObjectAtOffset(+15, 0, Around.class); Actor WallIn = getOneObjectAtOffset(+15, 0, WallIn.class); if(Around != null){ return false; } else if (WallIn != null){ return false; }else{ return true; } } public boolean WallSouth() { Actor Around = getOneObjectAtOffset(0, +15, Around.class); Actor WallIn = getOneObjectAtOffset(0, +15, WallIn.class); if(Around != null){ return false; } else if (WallIn != null){ return false; }else{ return true; } } public boolean WallWest() { Actor WallIn = getOneObjectAtOffset(-15, 0, WallIn.class); Actor Around = getOneObjectAtOffset(-15, 0, Around.class); if(WallIn != null){ return false; } else if (Around != null){ return false; }else{ return true; } } }
You need to login to post a reply.