-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayer.java
More file actions
329 lines (292 loc) · 9.47 KB
/
Player.java
File metadata and controls
329 lines (292 loc) · 9.47 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package projects.Pacman;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import javax.swing.*;
import java.awt.*;
/**
* Created by 20ZhangJ on 6/1/2018.
*/
public class Player extends Actor {
int lives;
int scoreValue;
boolean isSuper = false;
public static final int SUPERLENGTH = 40;
private int superSteps;
private final int LEFT = 1;
private final int RIGHT= -1;
private final int UP = 2;
private final int DOWN = -2;
private int checkirection;
private boolean teleported;
private boolean isCherries;
private JLabel p;
JFrame scoreBoard;
/**
* Sets everything up
*/
public Player() {
//Basic Stuff
lives = 3;
scoreValue = 0;
isSuper = false;
setColor(Color.YELLOW);
checkirection = 0;
teleported = false;
isCherries = false;
superSteps = 0;
//Scoreboard stuff
scoreBoard = new JFrame();
scoreBoard.setSize(new Dimension(200, 100));
scoreBoard.setVisible(true);
p = new JLabel();
scoreBoard.getContentPane().add(p);
p.setBackground(Color.WHITE);
p.setOpaque(true);
p.setForeground(Color.BLUE);
p.setFont(new Font("CALIBRI", Font.BOLD, 18));
p.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
p.setText("Score: " + scoreValue);
}
/**
* More specific constructor to implement when the game is more complete
* @param newLives number of lives
* @param newScore player's score
*/
public Player(int newLives, int newScore) {
lives = newLives;
scoreValue = newScore;
setColor(Color.YELLOW);
teleported = false;
}
/**
* What Pacman does in a step, decides whether it's a normal step, super step, or that weird situation where he's
* about to teleport
*/
public void act() {
Grid<Actor> gr = getGrid();
//Act like this if Pacman is super
if(isSuper)
{
doSuper();
superSteps--;
if(superSteps == 0){
isSuper = false;
isCherries = false;
gr.remove(new Location(2, 1));
gr.remove(new Location(1, 17));
gr.remove(new Location(19, 1));
gr.remove(new Location(19, 17));
System.out.println("SUPER IS " + isSuper);
}
}
else if (getLocation().equals(new Location(9,0)) && !teleported) //Teleport from left side
{
if(isSmallFood(new Location(9, 18)))
{
scoreValue++;
System.out.println("Score is: " + scoreValue);
}
else if (isBigFood(new Location(9, 18)))
{
scoreValue += 10;
isSuper = true;
superSteps = SUPERLENGTH;
}
moveTo(new Location(9, 18));
teleported = true;
}
else if (getLocation().equals(new Location(9,18)) && !teleported) //Teleport from right side
{
if(isSmallFood(new Location(9, 0)))
{
scoreValue++;
System.out.println("Score is: " + scoreValue);
}
else if (isBigFood(new Location(9, 0)))
{
scoreValue += 10;
isSuper = true;
superSteps = SUPERLENGTH;
System.out.println("SUPER IS " + isSuper);
}
moveTo(new Location(9,0));
teleported = true;
}
//Normal movement otherwise
else if (canMove())
{
move();
}
//Wait what
else {
}
p.setText("Score: " + scoreValue);
}
public int trackFood() {
return 1;
}
/**
* Separate sort of act method that changes some things up if pacman is super
*/
public void doSuper() {
Grid<Actor> gr = getGrid();
if (!isCherries) {
gr.put(new Location(2, 1), new Cherry());
gr.put(new Location(1, 17), new Cherry());
gr.put(new Location(19, 1), new Cherry());
gr.put(new Location(19, 17), new Cherry());
isCherries = true;
}
if (getLocation().equals(new Location(9,0)) && !teleported) //Teleport from left side
{
if(isSmallFood(new Location(9, 18)))
{
scoreValue+=2;
System.out.println("Score is: " + scoreValue);
}
else if (isBigFood(new Location(9, 18)))
{
scoreValue += 20;
isSuper = true;
}
moveTo(new Location(9, 18));
teleported = true;
}
else if (getLocation().equals(new Location(9,18)) && !teleported) //Teleport from right side
{
if(isSmallFood(new Location(9, 0)))
{
scoreValue+= 2;
System.out.println("Score is: " + scoreValue);
}
else if (isBigFood(new Location(9, 0)))
{
scoreValue += 20;
isSuper = true;
}
moveTo(new Location(9,0));
teleported = true;
}
//Normal movement otherwise, but it's super this happens
else if (canMove())
{
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
teleported = false;
if(isSmallFood(next))
{
scoreValue+=2;
System.out.println("Score is: " + scoreValue);
}
else if (isBigFood(next))
{
scoreValue += 20;
isSuper = true;
}
else if (isCherry(next)) {
gr.remove(next);
scoreValue += 100;
}
Actor inFront = gr.get(next);
moveTo(next);
}
else {
}
}
public boolean canMove() {
Grid gr = this.getGrid();
if(gr == null) {
return false;
} else {
Location loc = this.getLocation();
Location next = loc.getAdjacentLocation(this.getDirection());
if(!gr.isValid(next)) {
return false;
} else {
Actor neighbor = (Actor)gr.get(next);
/*if (neighbor instanceof Food) {
return true;
}*/
return neighbor == null || !(neighbor instanceof Wall);
}
}
}
public boolean isSmallFood(Location nextOne) {
Grid gr = this.getGrid();
if(gr == null) {
return false;
} else {
Location loc = this.getLocation();
//Location next = loc.getAdjacentLocation(this.getDirection());
if(!gr.isValid(nextOne)) {
return false;
} else {
Actor neighbor = (Actor)gr.get(nextOne);
/*if (neighbor instanceof Food) {
return true;
}*/
return neighbor instanceof SmallFood;
}
}
}
public boolean isBigFood(Location nextOne) {
Grid gr = this.getGrid();
if(gr == null) {
return false;
} else {
Location loc = this.getLocation();
//Location next = loc.getAdjacentLocation(this.getDirection());
if(!gr.isValid(nextOne)) {
return false;
} else {
Actor neighbor = (Actor)gr.get(nextOne);
/*if (neighbor instanceof Food) {
return true;
}*/
return neighbor instanceof BigFood;
}
}
}
public boolean isCherry(Location nextOne) {
Grid gr = this.getGrid();
if(gr == null) {
return false;
} else {
Location loc = this.getLocation();
//Location next = loc.getAdjacentLocation(this.getDirection());
if(!gr.isValid(nextOne)) {
return false;
} else {
Actor neighbor = (Actor)gr.get(nextOne);
/*if (neighbor instanceof Food) {
return true;
}*/
return neighbor instanceof Cherry;
}
}
}
public void move() {
Grid<Actor> gr = getGrid();
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
teleported = false;
if(isSmallFood(next))
{
scoreValue++;
System.out.println("Score is: " + scoreValue);
}
else if (isBigFood(next))
{
isSuper = true;
scoreValue+=10;
superSteps = SUPERLENGTH;
System.out.println("SUPER IS " + isSuper);
}
Actor inFront = gr.get(next);
moveTo(next);
}
protected void paintComponent(Graphics g) {
p.setText("Score: " + scoreValue);
}
}