We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I'm very very new at Processing and trying to create an abbreviated version of Pac-man for class.
I have some parts moving, namely, a yellow dot that moves through a maze when the directional arrows are pressed.
This worked for the most part when coded basically in setup/draw, however as soon as I write a class for the exact same code (reorganized), the entire game shrinks!
I don't know what is triggering it, I think it has to do with the placement of the values that moved from the original version of setup(). I would appreciate any help. I noticed some behaviours in Processing change when written as a class (but I'm still new to this, so I can't tell if it's a quirk or my unfamiliarity).
Code written as a class
Maze gameset = new Maze();
void setup() {
size(1000, 500);
}
void draw() {
gameset.displayMap();
gameset.keyPressed();
}
class Maze {
//Starting by using a 2D Array to map the maze.
int [][]maze = new int[][]{
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //TOP WALL // ROW 1
{-1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1}, // ROW 2
{-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1}, //ROW 3
{-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1}, //ROW 4
{-1, 1, -1, 1, -1, -1, 1, -1, -1, 0, 0, -1, -1, 1, -1, -1, 1, -1, 1, -1}, //ROW 5
{-1, 2, 1, 1, 1, 1, 1, -1, 0, 0, 0, 0, -1, 1, 1, 1, 1, 1, 1, -1}, //ROW 6
{-1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1}, //ROW 7
{-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1}, //ROW 8
{-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1}, //ROW 9
{-1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1}, // ROW 10
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //BOTTOM WALL // ROW 11
};
int rows = 11;
int cols = 20;
int minX, maxX, minY, maxY;
int cellWidth, cellHeight;
int pacX = 1, pacY = 6;
Maze() {
minX = 10;
maxX = width;
minY = 10;
maxY = height;
cellWidth = maxX/cols;
cellHeight = maxY/rows;
}
void displayMap() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
pushStyle();
noStroke();
int value = maze[j][i];
if (value == -1) {
fill(0, 0, 255);
} else if (value == 0) {
fill(0);
} else if (value == 1) {
fill(0, 255, 0);
} else {
fill(230, 190, 30);
}
rect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
popStyle();
}
}
println(pacX, pacY);
}
void keyPressed() {
if (key == CODED) {
int newX = pacX, newY = pacY;
if (keyCode == RIGHT) {
newX = pacX + 1;
newY = pacY;
} else if (keyCode == LEFT) {
newX = pacX - 1;
newY = pacY;
} else if (keyCode == UP) {
newX = pacX;
newY = pacY - 1;
} else if (keyCode == DOWN) {
newX = pacX;
newY = pacY + 1;
}
updatePacPosition(newX, newY);
}
}
void updatePacPosition(int newX, int newY) {
if (newX < cols && newX >= 1 && newY >= 1 && newY < rows && maze[newY][newX] != -1) {
maze[pacY][pacX] = 0;
pacX = newX;
pacY = newY;
maze[pacY][pacX] = 2;
}
}
}
Code before class
//Starting by using a 2D Array to map the maze.
int [][]maze = new int[][]{
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //TOP WALL // ROW 1
{-1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1}, // ROW 2
{-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1}, //ROW 3
{-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1}, //ROW 4
{-1, 1, -1, 1, -1, -1, 1, -1, -1, 0, 0, -1, -1, 1, -1, -1, 1, -1, 1, -1}, //ROW 5
{-1, 2, 1, 1, 1, 1, 1, -1, 0, 0, 0, 0, -1, 1, 1, 1, 1, 1, 1, -1}, //ROW 6
{-1, 1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1}, //ROW 7
{-1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1}, //ROW 8
{-1, 1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1}, //ROW 9
{-1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1}, // ROW 10
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, //BOTTOM WALL // ROW 11
};
int rows = 11;
int cols = 20;
int minX, maxX, minY, maxY;
int cellWidth, cellHeight;
int pacX = 1, pacY = 6;
void setup() {
size(1000, 500);
minX = 10;
maxX = width;
minY = 10;
maxY = height;
cellWidth = maxX/cols;
cellHeight = maxY/rows;
}
void draw() {
//fill(0);
//rect(0, 0, width, height);
//fill(0);
//rect(minX, minY, maxX-minX, maxY-minY);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
pushStyle();
noStroke();
int value = maze[j][i];
if (value == -1) {
fill(0, 0, 255);
} else if (value == 0) {
fill(0);
} else if (value == 1) {
fill(0, 255, 0);
} else {
fill(230, 190, 30);
}
rect(i * cellWidth, j * cellHeight, cellWidth, cellHeight);
popStyle();
}
}
println(pacX, pacY);
}
void keyPressed() {
if (key == CODED) {
int newX = pacX, newY = pacY;
if (keyCode == RIGHT) {
newX = pacX + 1;
newY = pacY;
} else if (keyCode == LEFT) {
newX = pacX - 1;
newY = pacY;
} else if (keyCode == UP) {
newX = pacX;
newY = pacY - 1;
} else if (keyCode == DOWN) {
newX = pacX;
newY = pacY + 1;
}
updatePacPosition(newX, newY);
}
}
void updatePacPosition(int newX, int newY) {
if (newX < cols && newX >= 1 && newY >= 1 && newY < rows && maze[newY][newX] != -1) {
maze[pacY][pacX] = 0;
pacX = newX;
pacY = newY;
maze[pacY][pacX] = 2;
}
}
Answers
You need to format your code. Edit your post, select your code and hit ctrl+o. Make sure there is an empty line above and below the code.
Kf
Thanks @kfrajer!
It worked. I've updated the code.
Would appreciate any feedback.
@wonkywoah -- without running your class code, I'm guessing that this will fix your problem:
Why? Because you class constructor uses
width
andheight
. When were you calling your class constructor? Before you calledsize(1000, 500)
... which meant that the width and height were still set to the default 200x200 ! :-?Thanks, @jeremydouglass.
Changing my maxX and maxY values within the constructor, as opposed to referencing the width and height set within setup() worked.
I guess there is something conceptual, here, that I am missing? I don't understand why a class cannot recognize the width and height of the sketch? Don't all classes and function adhere to setup()?
Would like to understand this further. :)
Classes can recognize the
width
andheight
, but they can't do it before you call thesize()
function.@KevinWorkman
Ah-ha. Got it. In trying to make call my object efficiently in the first line I completely missed that step. Thanks for your patience all. I'll keep at it.