# Borders for a maze that resets the object to the start?

So I am making a little game as a project to improve my coding.
It is a maze-ish game where you can shrink and grow to go through it and when you touch a wall it will reset you to the start, I am using if statements to do this but it is very ineffective and I was wondering if there was a better way. Please help me any way you can, thank you

2 Likes

Hello,

and welcome to the forum!

Great to have you here!

It would be cool to see your code, so we can make suggestion based on what you got.

Basically I would code a labyrinth using a 2D array (a grid like a chessboard).

Then in each cell store infos like 0 (for way) 1 for wall, 2 for goal and 3 for treasure, 4 for reset, maybe even 5 for player

So you make the grid in setup (using random, but nor only) and evaluate it in draw().

See tutorial on 2D arrays: https://www.processing.org/tutorials/2darray/

Warmest regards,

Chrisir

1 Like

Thank you, I will look into that, my code is very messy as Iâ€™m kind of trying what comes to mind as I code since Iâ€™m learning as I go.
Heres my code:

``````//the first two are the objects postion
float x = 500;
float y = 400;
//the objects height(y) and width(x)
float ySize = 80;
float xSize = 80;
/*this is so when the arrow input is moving the object in a direction it will change
its direction*/
float start = PI/4;
float end = 7*PI/4;

void setup(){
size(1000,800);
}

void keyPressed(){
if(key == CODED){
if(keyCode == LEFT){
x = x - 10;
//makes the object face left
start = PI + PI/4;
end = 11*PI/4;
}

else if(keyCode == RIGHT){
x = x + 10;
//makes object face right
start = PI/4;
end = 7*PI/4;
}
if(keyCode == UP){
y = y - 10;
//makes object look up
start = 7*PI/4-PI-PI;
end = 5*PI/4;
}
else if(keyCode == DOWN){
y = y + 10;
//makes object look up
start = 3*PI/4;
end = 9*PI/4;
}
}
if(keyPressed == true){
if(key == 'f' || key == 'F'){
//makes object grow, 'f' for "fatten"
xSize = xSize+5;
ySize = ySize+5;
}
if(keyPressed == true){
if(key == 's' ||key == 'S'){
//shrinks the object
xSize= xSize -5;
ySize = ySize -5;
}
}
}
}
void draw(){
/*the following if statements with a "xSize/2" or "ySize/2" are borders
which when the object touches the wall it is reset to the start
the xSize/2 makes it so that when the edge of the object it will reset
rather than the center*/
if(x < 25+ (xSize/2)){
x =500;
y=400;
}
else if(x > width-(xSize/2)){
x = width-(xSize/2);

}
if(x<105 +(xSize/2) && y >  428-(ySize/2)){
x = 500;
y=400;
}
if(x < 360+(xSize/2) &&y < 125+(ySize/2)){
x = 500;
y=400;
}
if(y <0+(ySize/2)){
y =ySize/2;
}
else if(y > height-(ySize/2)){
y = height-(ySize/2);
}

background(255);
fill(0);
rect(0,0,1000,800);
//these rectangles are the layout of the maze
fill(255);
noStroke();
rect(475,125, 50,221,25);
rect(475,125,225,50,25);
rect(425,325,200,200,25);
rect(655,125,50,575,25);
rect(125,650,800,50,25);
rect(125,350,100,338,25);
rect(25,350,138,75,25);
rect(25,150,120,238,25);
rect(25,125,400,70,25);
rect(375,25,50,138,25);
rect(375,25,490,50,25);
rect(900,450,25,240,25);
rect(750,450,170,25,25);
rect(750,350,25,120,25);
rect(750,350,225,25,25);
rect(950,200,25,170,25);
rect(725,200,245,25,25);
rect(725,100, 25,120,25);
rect(725,100,190,18,25);
//this is the goal
fill(0,128,0);
rect(825,25,100,100,25);
fill(255,255,0);
stroke(0);
//this is the object which I made into the Pac-Man character
arc(x,y,xSize,ySize, start, end, PIE);
fill(0);
noStroke();
//this is the win condition which gives you a green check mark for winning
if(x > 825 && y < 125){
fill(0,128,0);
noStroke();
}
``````
1 Like

It looks great!

Instead of code the entire labyrinth as a grid you can also keep your cool labyrinth and store the additional data in an 1D array of PVector and use dist() to find out if pacman is on it (or near it).

Chrisir

For a simple â€śdonâ€™t-touch-the-wallsâ€ť maze, here is one approach:

1. Draw a bunch of things all in â€śwall colorâ€ť. This is your maze.
2. When the player moves, update their position before drawing them.
3. Check the pixel they are about to appear on. Is that pixel wall color? If it is, reset.

and optionally:

1. after drawing walls, draw some goal pixels in â€śgoal colorâ€ť.
2. If the player is on a goal pixel, win.
1 Like