-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.java
executable file
·126 lines (112 loc) · 2.86 KB
/
Ghost.java
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
/**
* Ghost class for PacMan game.
*
* @author horn5
*/
public class Ghost {
protected int x, y;
protected boolean edible = false;
protected String imageFile;
// Ghost constructor
public Ghost(int a, int b, String image) {
x = a;
y = b;
imageFile = image;
}
// Copy constructor; makes a copy of the given ghost
public Ghost(Ghost other) {
x = other.x;
y = other.y;
imageFile = other.imageFile;
edible = other.edible;
}
// Getters
public int getX(){
return x;
}
public int getY(){
return y;
}
public boolean isEdible() {
return edible;
}
public String getImage() {
return imageFile;
}
// Draw the Ghost
public void paint() {
Zen.drawImage(imageFile,x,y);
}
// When ghost is eaten, it returns to user-specified location and becomes non-edible again.
public void getEaten(int a, int b, int i){
// Make sure that the ghost is actually edible when this method is called.
if(edible) {
toggleEdible(i);
x = a;
y = b;
}
}
// Moves ghost
public void moveGhost(PacMan pac, int lowerX, int upperX, int lowerY, int upperY) {
if(this != null) {
if(isEdible())
edibleMove(pac, lowerX, upperX, lowerY, upperY);
else move(pac, lowerX, upperX, lowerY, upperY);
}
}
// Ghost randomly moves in the x and y directions, remaining within the user-specified bounds
public void move(PacMan pac, int lowerX, int upperX, int lowerY, int upperY){
if(x > lowerX) {
if(x < upperX) {
if(Math.random() > 0.5) x+=10;
else x-=10;
}
else x-=10;
}
// If the ghost is against a wall, it will move away from the wall
else x+=10;
if(y > lowerY) {
if(y < upperY) {
if(Math.random() > 0.5) y+=10;
else y-=10;
}
else y-=10;
}
// If the ghost is against a wall, it will move away from the wall
else y+=10;
}
// Toggles the 'edible' variable (specific occurrences dictated in the Game class)
public void toggleEdible(int i) {
if(edible) imageFile = (i == 0 ? "ghost_inky.png" : "ghost_pinky.png");
else imageFile = "ghost_edible.png";
edible = !edible;
}
// When the ghost is edible, it moves away from the PacMan while still remaining within the
// user-specified bounds
public void edibleMove(PacMan pac, int lowerX, int upperX, int lowerY, int upperY) {
if(x > lowerX) {
if(x < upperX) {
if(pac.getX() > x) x-=5;
else x+=5;
}
else x-=5;
}
else x+=5;
if(y > lowerY) {
if(y < upperY) {
if(pac.getY() > y) y-=5;
else y+=5;
}
else y-=5;
}
else y+=5;
}
// Creates and returns a new array of ghosts that is identical to the given array with the given
// ghost parameter added to the end
public static Ghost[] addGhost(Ghost g, Ghost[] list) {
Ghost[] addedGhost = new Ghost[list.length + 1];
for(int i = 0; i < list.length; i++) addedGhost[i] = list[i];
addedGhost[addedGhost.length - 1] = g;
return addedGhost;
}
}