Student Pacman help
in
Programming Questions
•
2 years ago
Hi
I am new to processing, I am working on a Pacman assignment and finding it quite difficult, if anyone can help me I would be very grateful.
So far I have managed to create a Pacman class and object which moves with KeyPressed via the arrow keys, I have managed to create a class for the dots and created the object, covering the canvas with ellipses similar to a gird. I do not know how to make Pacman eat the dots, once in contact, I have attempted a boolean and if statement, but I am not sure what I have done wrong, the program runs. I also have not been able to make Pacman face the direction he is moving in.
Here is all of the code to better explain
// main program
float cx = 250,
cy = 250,
dx = 0,
dy = 0;
PImage Pac;
Pacman P1;
Food F;
int Fx,
Fy,
S,
G;
void setup() {
size(500, 500);
Pac = loadImage ("Pacman 48x48px.png");
P1 = new Pacman (cx, cy, dx, dy, Pac);
F = new Food (Fx, Fy, S, G);
}
void draw () {
background (0);
P1.Move ();
F.Draw ();
P1.Draw ();
Eating() ;
}
void Eating() {
float distance;
distance = sqrt(pow(P1.cx - F.Fx,2) + pow(P1.cy - F.Fy, 2));
if(distance < 10);
}
void keyPressed () {
if (key == CODED) {
switch (keyCode) {
case LEFT:
P1.dy = 0;
P1.dx = -1;
break;
}
}
if (width < dy) {
dy = 500;
}
if (key == CODED) {
switch (keyCode) {
case RIGHT:
P1.dy = 0;
P1.dx = +1;
break;
}
}
if (key == CODED) {
switch (keyCode) {
case UP:
P1.dy = -1;
P1.dx = 0;
break;
}
}
if (key == CODED) {
switch (keyCode) {
case DOWN:
P1.dy = +1;
P1.dx = 0;
break;
}
}
}
HERE is the Pacman Class:
lass Pacman {
float cx,
cy,
dx,
dy;
PImage Pac;
Pacman (float cx, float cy, float dx, float dy, PImage Pac){
this. cx = cx;
this. cy = cy;
this. dx = dx;
this. dy = dy;
this. Pac = Pac;
}
void Draw () {
imageMode (CENTER);
image (Pac, cx, cy);
}
void Move () {
cx += dx;
cy += dy;
}
}
Here is food class:
class Food {
int Fx,
Fy;
int S,
G;
Food (int Fx, int Fy, int S, int G){
this.Fx= Fx;
this.Fy = Fy;
this.S = S;
this.G = G;
}
void Draw () {
G = 30;
S = 10;
for(int Fx = 0; Fx <= width; Fx += G){
for(int Fy = 0; Fy <= height; Fy += G){
ellipse (Fx, Fy, S, S);
fill (White);
stroke (Red);
}
}
}
}
color Yellow = color(255, 255, 0);
color Red = color(255, 0, 0);
color Green = color(0, 255, 0);
color Black = color(0, 0, 0);
color White = color(255, 255, 255);
color Blue = color(0, 0, 255);
color Orange = color(255, 127, 0);
This is all the code I have managed to do, if anyone can guide me or show me how to make Pacman at least make the dots disappear when they both come in contact I would be very grateful. Apologies if I am unclear or ambiguous on my problem.
Thanks
Ishy
I am new to processing, I am working on a Pacman assignment and finding it quite difficult, if anyone can help me I would be very grateful.
So far I have managed to create a Pacman class and object which moves with KeyPressed via the arrow keys, I have managed to create a class for the dots and created the object, covering the canvas with ellipses similar to a gird. I do not know how to make Pacman eat the dots, once in contact, I have attempted a boolean and if statement, but I am not sure what I have done wrong, the program runs. I also have not been able to make Pacman face the direction he is moving in.
Here is all of the code to better explain
// main program
float cx = 250,
cy = 250,
dx = 0,
dy = 0;
PImage Pac;
Pacman P1;
Food F;
int Fx,
Fy,
S,
G;
void setup() {
size(500, 500);
Pac = loadImage ("Pacman 48x48px.png");
P1 = new Pacman (cx, cy, dx, dy, Pac);
F = new Food (Fx, Fy, S, G);
}
void draw () {
background (0);
P1.Move ();
F.Draw ();
P1.Draw ();
Eating() ;
}
void Eating() {
float distance;
distance = sqrt(pow(P1.cx - F.Fx,2) + pow(P1.cy - F.Fy, 2));
if(distance < 10);
}
void keyPressed () {
if (key == CODED) {
switch (keyCode) {
case LEFT:
P1.dy = 0;
P1.dx = -1;
break;
}
}
if (width < dy) {
dy = 500;
}
if (key == CODED) {
switch (keyCode) {
case RIGHT:
P1.dy = 0;
P1.dx = +1;
break;
}
}
if (key == CODED) {
switch (keyCode) {
case UP:
P1.dy = -1;
P1.dx = 0;
break;
}
}
if (key == CODED) {
switch (keyCode) {
case DOWN:
P1.dy = +1;
P1.dx = 0;
break;
}
}
}
HERE is the Pacman Class:
lass Pacman {
float cx,
cy,
dx,
dy;
PImage Pac;
Pacman (float cx, float cy, float dx, float dy, PImage Pac){
this. cx = cx;
this. cy = cy;
this. dx = dx;
this. dy = dy;
this. Pac = Pac;
}
void Draw () {
imageMode (CENTER);
image (Pac, cx, cy);
}
void Move () {
cx += dx;
cy += dy;
}
}
Here is food class:
class Food {
int Fx,
Fy;
int S,
G;
Food (int Fx, int Fy, int S, int G){
this.Fx= Fx;
this.Fy = Fy;
this.S = S;
this.G = G;
}
void Draw () {
G = 30;
S = 10;
for(int Fx = 0; Fx <= width; Fx += G){
for(int Fy = 0; Fy <= height; Fy += G){
ellipse (Fx, Fy, S, S);
fill (White);
stroke (Red);
}
}
}
}
color Yellow = color(255, 255, 0);
color Red = color(255, 0, 0);
color Green = color(0, 255, 0);
color Black = color(0, 0, 0);
color White = color(255, 255, 255);
color Blue = color(0, 0, 255);
color Orange = color(255, 127, 0);
This is all the code I have managed to do, if anyone can guide me or show me how to make Pacman at least make the dots disappear when they both come in contact I would be very grateful. Apologies if I am unclear or ambiguous on my problem.
Thanks
Ishy
3