Maze

edited November 2013 in How To...

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

  • Answer ✓

    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...

  • 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);
    }
    

    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.

    ball player;
    wall[] walls; 
    
    void setup() {
    
      size(750, 750);
    
      player = new ball(50,300);
    
      walls = new wall[2];
      walls[0] = new wall(250,0,40,500);
      walls[1] = new wall(500,250,40,500);
    
    } 
    void draw() {
    
      background(255, 255, 255); 
      noStroke(); 
    
      player.draw();
      player.move(walls);
    
      for(int i = 0; i < walls.length; i++){
        walls[i].draw();
      }
    
    }
    
    class ball {
    
      float x;
      float y;
    
      ball(float _x, float _y){
        x = _x;
        y = _y;
      }
    
      void draw(){
        fill(128);
        ellipse(x,y,25,25);
      }
    
      void move(wall[] walls){
    
        float possibleX = x;
        float possibleY = y;
    
        if (keyPressed==true) {
    
          println(key);
    
          if (key=='a') { 
            possibleX= possibleX - 5;
          } 
          if (key=='d') { 
            possibleX = possibleX + 5;
          } 
          if (key=='w') { 
            possibleY = possibleY - 5;
          } 
          if (key=='s') { 
            possibleY = possibleY + 5;
          }
        }
    
        boolean didCollide = false;
        for(int i = 0; i < walls.length; i++){
          if(possibleX > walls[i].x && possibleX < (walls[i].x + walls[i].w) && possibleY > walls[i].y && possibleY < walls[i].y + walls[i].h){
            didCollide = true;
          }
        }
    
        if(didCollide == false){
          x = possibleX;
          y = possibleY;
        }
    
      }
    
    }
    
    class wall {
    
      float x;
      float y;
      float w;
      float h;
    
      wall(float _x, float _y, float _w, float _h){
        x = _x;
        y = _y;
        w = _w;
        h = _h;
      }
    
      void draw(){
        fill(0);
        rect(x,y,w,h);
      }
    
    }
    
  • edited November 2013

    Not your original question, but if you want to stop your ball from falling off the edges then I would add:

    updown = constrain(updown,0,height);
    leftright = constrain(leftright,0,height);
    

    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:

    if(updown< 0){updown = height;}
    if(updown> height){updown = 0;}
    if(leftright< 0){leftright = width;}
    if(leftright> width){leftright = 0;}
    

    (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:

    if(leftright+radius>250 && leftright-radius<290 && updown-radius>0 &&updown-radius<500)
    {leftright = 50; updown =300;}
    
    if(leftright+radius>500 && leftright-radius<540 && updown-radius>250 &&updown-radius<750)
    {leftright = 50; updown =300;}
    

    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.

Sign In or Register to comment.