scatter 7s
chase 20s
scatter 7sec
chase 20 sec
scatter 5 sec
chase 20sec
scatter 5sec
chase forever
Im including the GhostRed class which calculates the target of the red ghost and Ghost superclass which includes move ghost algo.
GHOST.java
package PacmanGame;
import java.util.Random;
import java.awt.Graphics;
public abstract class Ghost extends Character{
protected static final int DOWN = 0;
protected static final int UP = 1;
protected static final int LEFT = 2;
protected static final int RIGHT = 3;
protected Tiles target;
protected Tiles scatterTile;
protected boolean scatterMode;
protected boolean chaseMode;
protected boolean frightenedMode;
protected Tiles pacmanTile;
protected Tiles redGhostTile;
protected int pacmanDirection;
protected int redDirection;
protected int futureDirection;
protected int direction;
protected int timer;
protected Random randomNumGen = new Random();
public Ghost(int x, int y, Maze maze){
super(x, y, maze);
futureDirection = 0;
timer = 0;
scatterMode = true;
chaseMode = false;
frightenedMode = false;
}
public void move() {
target = calculateTarget();
moveGhost(target);
}
public void moveGhost(Tiles target) {
if(specialIntersections()){
direction = direction; //keeps going in the same direction
}
else{
int oppDir;
if(direction == UP){
oppDir = DOWN;
}
else if(direction == DOWN){
oppDir = UP;
}
else if(direction == LEFT){
oppDir = RIGHT;
}
else{
oppDir=LEFT;
}
double minDist = 10000.0;
Tiles potentialNext;
for(int i=0; i<4; i++){
if(i!=oppDir){
potentialNext = maze.nextTile(getCurrentPos(), i);
if(!(potentialNext.wall()) && check(potentialNext)){
if(calculateDistance(target, potentialNext) < minDist){
minDist = calculateDistance(target, potentialNext);
futureDirection = i;
}
}
}
}
}
changeDirection();
timer++;
increment();
x += xinc;
y += yinc;
tunnel();
}
public void increment(){
//...
}
public void changeDirection(){
if(timer==5){
direction = futureDirection;
timer=0;
}
}
public double calculateDistance(Tiles target, Tiles start) {
//...
return distance;
}
public int getDirection() {
return this.direction;
}
public boolean specialIntersections(){
//...
}
public void retrieve(Tiles t, int d1, Tiles r, int d2){
pacmanTile = t;
pacmanDirection = d1;
redGhostTile = r;
}
public void setTarget(Tiles t){
target = t;
}
/*public void setFrightened(){
if(scatterMode){
scatterMode = false;
}
else{
chaseMode = false;
}
frightenedMode = true;
if(direction == UP){
direction = DOWN;
}
else if(direction == DOWN){
direction = UP;
}
else if(direction == LEFT){
direction = RIGHT;
}
else{
direction = LEFT;
}
}*/
//DOESN'T WORK!
public void setChase(){
if (timer == 1)
chaseMode=true;
}
public Tiles generateRandomTile(){
//...
}
protected abstract Tiles calculateTarget();
protected abstract void paintComponent(Graphics G);
}
GHOSTRED.java
package PacmanGame;
import java.awt.*;
public class GhostRed extends Ghost{
public GhostRed(int x, int y, Maze maze) {
super(x, y, maze);
scatterTile = new Tiles(25, -1);
direction = 0;
}
//@Override
public void paintComponent(Graphics G){
if(frightenedMode){
G.setColor(Color.BLUE);
}
if(scatterMode){
G.setColor(Color.GREEN);
}
else{
G.setColor(Color.RED);
}
G.fillRect(x, y, 20, 20);
}
protected Tiles calculateTarget(){
if(scatterMode){
return scatterTile;
}
else if(frightenedMode){
return generateRandomTile();
}
return pacmanTile;
}
}

New Topic/Question
Reply



MultiQuote




|