We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is very simple; however, I am a beginner at this.. I wanted to make a maze, but I don't know how to make it so my ball doesn't go through the maze walls. Should I use mapping or something else?
Easy peasy code so far--
float updown;
float leftright;
int a;
void setup(){
size(750,750);
updown=300;
leftright=50;
}
void draw(){
background(255,255,255);
noStroke();
fill(0,0,0);
ellipse(leftright, updown, 25, 25);
if(keyPressed==true){
if(key=='a'){
leftright=leftright-5;
}
if(key=='d'){
leftright=leftright+5;
}
if(key=='w'){
updown=updown-5;
}
if(key=='s'){
updown=updown+5;
}
}
rect(250,0,40,500);
rect(500,250,40,500);
}
Answers
Formatted your code for you... you just need to highlight it and press CTRL+K (or Command+K on a Mac?).
You're going to have to perform collision checks between each of the rectangles and the ball. At some level of complexity, you will realize that this type of project is much easier to implement when using classes...
A search will go a long way for rect to circle collision...
Highlight your pasted code and press Control+K to get it formatted correctly for markdown. Otherwise it is not readable.
To address your question: Before changing the updown and leftright variables you need to check to see if your ball has collided with a zone that you have determined to be off limits.
You need to redesign your program to allow for data structures that would make this simple. My attempt at this idea creates a class for the ball, and a class for the boundary. I create an array of boundaries, and have the ball class check to see if the possible move would intersect with the area of those boundaries.
Hero Maze: o=>
forum.processing.org/one/topic/trying-to-display-a-character-on-top-of-my-maze.html
Not your original question, but if you want to stop your ball from falling off the edges then I would add:
or you could use a series of "if" statement to make it reappear on the opposite side of the window when it runs off the edge:
(add either of these to the draw block before the ellipse command) .
Also the simple code to add to your sketch to make the ball reset to its start position when it hits a wall is below:
In other words, if the position of the edge of the ball falls within either of the rectangles the ball will reset to where you started at coord (50,300).
A simple way to detect if a wall is about to be hit is to check the pixel color in the direction of movement... Wall color vs. corridor color.