-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.java
177 lines (144 loc) · 4.58 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
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
// @author cfcurti2
//@author tdhowar2
public class Ghost {
protected int x;
protected int y;
protected int direction = 0;
private int id;
private static int count;
protected boolean eaten = false;
//sets and gets
public void setEaten(boolean eaten){
this.eaten = eaten;
}
public boolean getEaten(){
return eaten;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setDirection(int d) {
this.direction = d;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getDirection() {
return direction;
}
public int getId() {
return id;
}
//end sets and gets
public Ghost() { //default constructor- makes a ghost in the bottom middle of the map
this.x = 392;
this.y = 482;
id = count;
eaten = false;
}
public Ghost(int x, int y) { //set your own position
this.x = x;
this.y = y;
id = count;
eaten = false;
}
public Ghost(int x, int y, int direction){
this.x = x;
this.y = y;
this.direction = direction;
eaten = false;
}
public Ghost(int x, int y, int direction, boolean eaten){
this.x = x;
this.y = y;
this.direction = direction;
this.eaten = eaten;
}
public static void resetCount() {
count = 0;
}
public static int getNumGhostsCreated() {
return count;
}
public void move(PacMan p) { //ghost move method, is passed a pacman but does not use it because the super ghost move does use it
if (direction == 0
&& (Zen.getZenWidth() - x + 2 > 120 && ((y == 62) || (y == 482) //same logic as pacman for allowed movement
|| (y == 181 && x >= 182 && x <= 602)
|| (y == 363 && x >= 182 && x < 603)
|| (y >= 267 && y <= 277 && x > 602) || y >= 267
&& y <= 277 && x <= 182))) {
if (x == 392)
direction = ((int) (Math.random() * 4));
x++;
}
else if (direction == 1
&& (x - 2 > 60 && (((y == 62) || (y == 482)
|| (y == 181 && x >= 183 && x <= 603)
|| (y == 363 && x >= 183 && x <= 603)
|| (y >= 267 && y <= 277 && x > 602) || y >= 267
&& y <= 277 && x <= 183)))) {
if (x == 392)
direction = ((int) (Math.random() * 4));
x--;
}
else if (direction == 2
&& (Zen.getZenHeight() - y + 2 > 120 && !((y == 272 && (x > 62
&& x < 182 || x > 603 && x < 722))
|| (y == 363 && ((x > 181 && x < 392 || x > 392
&& x < 721)))
|| (y == 181 && x > 182 && x < 603)
|| (y == 482 && (x >= 182 && x < 392 || x > 392
&& x < 603)) || (y == 62 && (x > 62 && x < 392 || x > 392
&& x < 722))))) {
if (y == 272)
direction = ((int) (Math.random() * 4));
y++;
}
else if (direction == 3
&& (y - 2 > 60 && !((y == 272 && (x > 62 && x < 182 || x > 603
&& x < 722))
|| (y == 363 && x > 182 && x < 603)
|| (y == 181 && (x >= 182 && x < 392 || x > 392
&& x < 604)) || (y == 482 && ((x > 62
&& x < 392 || x > 392 && x < 722)))))) {
if (y == 272)
direction = ((int) (Math.random() * 4));
y--;
} else
direction = ((int) (Math.random() * 4)); //this statement lets the pick random direction if it gets stuck
}
public boolean isTouching(PacMan p) { //checks if packman is within 40 pixels of the ghost
if (Math.abs(p.getX() - x) < 40 && Math.abs(p.getY() - y) < 40){
return true;
}
return false;
}
public void paint() {
Zen.drawImage("ghost_inky.gif", x, y); //paints the default ghost gif
/*
Zen.drawText( "g direction " + direction + (direction == 0 && ((y ==
62) || (y == 482 && x < 722 & x > 62) || (y == 181 && x >= 182 && x
<= 602) || (y == 363 && x >= 182 && x <= 602) || (y >= 267 && y <=
277 && x > 603) || y >= 267 && y <= 277 && x <= 182)) + (direction ==
1 && (x - 2 > 60 && (((y == 62) || (y == 482) || (y == 181 && x >=
183 && x <= 602) || (y == 363 && x >= 183 && x <= 602) || (y >= 267
&& y <= 277 && x > 602) || y >= 267 && y <= 277 && x <= 183)))) +
(direction == 2 && (Zen.getZenHeight() - y + 2 > 120 && !((y == 272
&& (x > 62 && x < 182 || x > 602 && x < 722)) || (y == 363 && ((x >
181 && x < 392 || x > 392 && x < 721))) || (y == 181 && x > 182 && x
< 602) || (y == 482 && (x >= 182 && x < 392 || x > 392 && x < 603))
|| (y == 62 && (x > 62 && x < 392 || x > 392 && x < 721))))) +
(direction == 3 && (y - 2 > 60 && !((y == 272 && (x > 62 && x < 182
|| x > 602 && x < 722)) || (y == 363 && x > 182 && x < 602) || (y ==
181 && (x >= 182 && x < 392 || x > 392 && x < 603)) || (y == 482 &&
((x > 62 && x < 392 || x > 392 && x < 721)))))) + " " + x + "," + y,
10, 40);// for debugging - outputs ghost x and y and directional booleans
*/
}
}