We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm not sure if this is the kind of question I can ask on here so if it isn't allowed just say. I have been given this piece of code by a lecturer in college but he hasn't really explained what it does. It is a ghost class in a pacman game so it is just creating the ghosts and making them chase the pacman and then killing it and turning colour when the pacman eats the super food. I dont exactly know how it's doing this though.
class Ghost {
int type;
int x, y, tx, ty, ptx, pty;
int pgx, pgy;
int d;
boolean is_scared;
boolean is_dead;
int trapped_timer;
Ghost(int itype) {
type = itype % ghost_colors.length;
x = 10 + 20 * 15;
y = 10 + 20 * 14;
tx = x;
ty = y;
ptx=tx;
pty=ty;
trapped_timer = 200 * type;
}
void dont_be_afraid() {
if (is_scared) {
is_scared = false;
d=(d+2)%4;
tx = ptx;
ty = pty;
}
}
void be_afraid() {
if (!is_dead) {
is_scared = true;
d=(d+2)%4;
tx = ptx;
ty = pty;
}
}
void draw() {
simulate();
render();
if (!is_dead) kill_check();
}
void simulate() {
trapped_timer--;
if (trapped_timer > 0) return;
int px = x;
int py = y;
if (tx < x) x--;
if (tx > x) x++;
if (ty < y) y--;
if (ty > y) y++;
if (px==x&&py==y) {
ptx = tx;
pty = ty;
int wall_count = 0;
for (int w = 0; w < 4; w++) {
if ( grid.is_wall(x+decodeX[w], y+decodeY[w]) ) {
wall_count++;
}
}
if (wall_count == 4) { // >:(
d = 4;
tx = x+decodeX[d];
ty = y+decodeY[d];
return;
}
if (wall_count == 3) {
for (int w = 0; w < 4; w++) {
if ( !grid.is_wall(x+decodeX[w], y+decodeY[w]) ) {
d = w;
}
}
tx = x+decodeX[d];
ty = y+decodeY[d];
return;
}
if (wall_count == 2) {
int result = 5;
for (int w = 0; w < 4; w++) {
if ( !grid.is_wall(x+decodeX[w], y+decodeY[w]) ) {
if ( w != (d+2)%4 ) {
result = w;
}
}
}
d = result;
tx = x+decodeX[d];
ty = y+decodeY[d];
return;
}
int gx = int(player.x); // RED - Go for pacman.
int gy = int(player.y);
if (!to_corners) {
if (type == 1 ) { // PINK = Get in front of pacman.
gx = int(player.x)+4*decodeX[player.d];
gy = int(player.y)+4*decodeY[player.d];
if ( player.d == 3 ) { // Classic pink ghost behaviour.
gx = int(player.x-80);
}
}
if ( type == 2 ) { // CYAN - Negative RED's positions, offset a couple of squares.
float mpx = player.x+2*decodeX[player.d];
float mpy = player.y+2*decodeY[player.d];
if (player.d == 3) { // Classic cyan ghost behaviour.
mpx = player.x - 40;
}
gx = int(2*mpx - ghosts[0].x);
gy = int(2*mpy - ghosts[0].y);
}
if ( type == 3 ) { // ORANGE - Get close, then back off.
if (dist(player.x, player.y, x, y) < 160) {
gx = 0;
gy = height;
}
}
} else {
gx = width;
gy = height;
if ( type == 1 || type == 3 ) gx = 0;
if ( type == 1 || type == 0 ) gy = 0;
}
if (is_dead||is_scared) {
gx = 10 + 20 * 15;
gy = 10 + 20 * 14;
if (is_dead && x==gx &&y==gy) is_dead = false;
}
pgx = gx;
pgy = gy;
float best = -1 ;
int result = 5;
for ( int w = 0; w < 4; w++) {
if ( w!=(d+2)%4 && !grid.is_wall(x+decodeX[w], y+decodeY[w])) {
float new_dist = dist(x+decodeX[w], y+decodeY[w], gx, gy);
if ( best == -1 || new_dist < best) {
best = new_dist;
result = w;
}
}
}
d = result;
tx = x+decodeX[d];
ty = y+decodeY[d];
return;
}
}
void render() {
stroke(0);
fill(is_scared?color(0, 0, 255):ghost_colors[type]);
pushMatrix();
translate(x, y);
if (!is_dead) {
rect(-11, -1, 22, 11);
arc(0, 0, 22, 22, PI, TWO_PI);
for (int i=0; i<4; i++) {
arc(-7+5*i, 10, 5, 5, 0, PI);
}
}
fill(255);
noStroke();
ellipse(-4, 0, 4, 8);
ellipse(4, 0, 4, 8);
if (is_dead||!is_scared) {
fill(0, 0, 255);
int eyex = (d==2?-1:0)+(d==0?1:0);
int eyey = (d==3?-3:0)+(d==1?3:0);
ellipse(-4+eyex, eyey, 3, 3);
ellipse(4+eyex, eyey, 3, 3);
}
popMatrix();
}
void kill_check() {
if ( grid.kill_at(x, y) ) is_dead = true;
}
}
int[] decodeX = { 20, 0, -20, 0, 0 };
int[] decodeY = { 0, 20, 0, -20, 0 };
Answers
class and objects
ok, this is class of a typical ghost.
A class is a blue print (pattern) for all ghosts (objects derived from the class), which then can have different positions etc.
Read the tutorial on objects:
https://www.processing.org/tutorials/objects/
Code Formatting
Then your code is formatted badly. Often a new method starts in the middle of a line with other text. Bad.
Try ctrl-t in processing to get better indents etc. before posting here
Leave an empty line between two methods:
The code
Now the code is pretty straight forward.
I personally would give the variables longer names which are better readable.
I mean x,y is position, p stands for previous and t stands for target. That should make the code better readable.
Now the class is doing the steering and displaying of the ghosts in the grid.
(i've fixed the worst of the formatting issues and improved the clickbaity title)
Did your lecturer give me any credit? This is obviously my code.
https://forum.processing.org/two/discussion/11156/pac-man-game-not-completed-asking-for-assistance
You might try following along with my original thread, instead of trying to get the ghost class working all at once.