I need help on Processing 3

I am trying to create a maze but I can’t figure out how to make it so the ball doesn’t go through the walls.

I have provided my code below, please help me by telling me what code to add.

int x = 170;
int y = 315;
PImage maze1;
float updown;
float leftright;
float radius;
int possibleX;
int possibleY;

void setup()
{
  size(810, 510);
  maze1= loadImage("maze1.png");
}

void draw ()
{
  image(maze1, -465, -175, 1900, 900);

  pushMatrix();
  translate(x, y);
  fill(255, 0, 0);
  noStroke();
  ellipse(249, 187, 15, 15);
  popMatrix();

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

  textSize(26);
  text("The Labyrinth", 15, 25);
  fill(0, 102, 153);
}
{
  boolean didCollide = false;

  didCollide = true;

  if (didCollide == false) {
    x = possibleX;
    y = possibleY;
  }
}

void keyPressed() {
  {

    if (key==CODED&
    & 
    keyCode==UP)
    {
      y-=8;
    }
    if (key==CODED&
    & 
    keyCode==DOWN)
    {
      y+=8;
    }
    if (key==CODED&
    & 
    keyCode==RIGHT)
    {
      x+=8;
    }
    if (key==CODED&
    & 
    keyCode==LEFT)
    {
      x-=8;
    }
    updown = constrain(updown, 0, height);
    leftright = constrain(leftright, 0, height);
  }
}

​here is my maze1.png file

1 Like

I’m guessing you’re in the same class as the person who posted this. I’d suggest mostly the same code for you. You just may need to account for where you are drawing the ellipse.

maze1.loadPixels();

boolean isValidLocation(x, y) {  // custom function that takes in an x and y and returns true or false 
  x += 249; //accounts for where you are drawing the ellipse
  y += 187;
  if (maze1.pixels[x + y * width] == color(255)) {
    return true;
  }
  return false;
}

// then change your all your keyPressed if statements to something like this
if (keyCode == DOWN && isValidLocation(x, y - 8)) { // won't move if ellipse would hit a wall
  // all the same code
}
1 Like

When I put that code in it said isValidLocation in not a class. Then I went to the top and put class isValidLocation, but then it said that boolean was not a function. so then i did void isValidLocation. Then it wouldn’t load. I dont know what im missing.

int x = 170;
int y = 315;
PImage maze1;
float updown;
float leftright;
float radius;
int possibleX;
int possibleY;

void setup()
{
  size(900, 800);
  maze1= loadImage("Maze1.png");
}

void draw ()
{
  image(maze1, 0, 0, 900, 800);

  pushMatrix();
  translate(x, y);
  fill(255, 0, 0);
  noStroke();
  ellipse(265, 460, 15, 15);
  popMatrix();

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

  textSize(26);
  text("The Labyrinth", 15, 25);
  fill(0, 102, 153);
}
{
  boolean didCollide = false;

  didCollide = true;

  if (didCollide == false) {
    x = possibleX;
    y = possibleY;
  }
}

void keyPressed() {
  {

    if (key==CODED&& keyCode==UP)
    {
      maze1.loadPixels();
      color c = maze1.get(x, y-8);
      if (red(c) == 255) {
        y-=8;
      }
    }
    if (key==CODED&& keyCode==DOWN)
    {
      maze1.loadPixels();
      color c = maze1.get(x, y+8); 
      if (red(c) == 255) {
        y+=8;
      }
    }
    if (key==CODED&& keyCode==RIGHT)
    {
      maze1.loadPixels();
      color c = maze1.get(x+8, y); 
      if (red(c) == 255) {
        x+=8;
      }
    }
    if (key==CODED&& keyCode==LEFT)
    {
      maze1.loadPixels();
      color c = maze1.get(x-8, y); 
      if (red(c) == 255) {
        x-=8;
      }
    }
    updown = constrain(updown, 0, height);
    leftright = constrain(leftright, 0, height);
  }
}

@buddyduncan please format your code in processing using Ctrl+t and paste it here with the </> sign like so:

code here

from your code like it’s pasted right now I don’t understand the

 {
boolean didCollide = false;

didCollide = true;

if (didCollide == false) {
x = possibleX;
y = possibleY;
}
}

part. it’s not used because it’s outside the draw() loop, so what is it supposed to do?..

Also, I don’t think you understand what boolean isValidLocation(int x, int y){ }
really means as you think it’s a class or void. It has to return a boolean, so it’s a boolean function. the reason it first gave you an error is probably because you placed it in a wrong part of the code. It has to be outside any other function or loop.

It’s usually a good idea to just place those kind of functions on the very bottom of the code, like so:

void keyPressed() {
  if (key==CODED&& keyCode==UP && isValidLocation(x, y-8)) {
    y-=8;
  }
  if (key==CODED&& keyCode==DOWN && isValidLocation(x, y+8)) {
    y+=8;
  }
  if (key==CODED&& keyCode==RIGHT && isValidLocation(x+8, y)) {
    x+=8;
  }
  if (key==CODED&& keyCode==LEFT && isValidLocation(x-8, y)) {
    x-=8;
  }
}

boolean isValidLocation(int x1, int y1) {
  return (maze1.get(x1, y1 ) == -1);
}

This is also a variation of what you wanted to accomplish.
I didn’t use the pixels[] array, but instead just the data from the image with maze1.get().

It’s up to you now to change this to the complete code and fix the position of the circle (as using translate isn’t necessary) and sometimes it will go through the walls because they’re thinner than the 8 pixels you’re moving.

2 Likes

You would be correct in that statement of me not knowing what boolean was. I have figured it out though from the help of both of you.

I asked my roommate if the code i put in would work and he told me to move some stuff around. Which too helped.

Thank you for you help. :slight_smile:

1 Like