# Need help with creating barriers/walls with boolean

**URL:** <https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444>\
**Category:** Coding Questions\
**Created:** [April 17, 2021, 1:39pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444 "2021-04-17T13:39:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cherie](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Cherie](https://discourse.processing.org/u/Cherie)\
**Post date:** [April 17, 2021, 1:39pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/1 "2021-04-17T13:39:21Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

float xpos;  
float ypos;  
float xposN;  
float yposN;  
float playersize;

PVector position;  
float direction;  
PVector velocity;  
float jumpSpeed;  
float walkSpeed;

void setup(){  
size(640, 360);  
//fullScreen();  
xpos = 50;  
ypos = 280;  
playersize=20;  
}

void draw(){  
background(0); // Clear the screen with a black background

playerDraw(); //function for the player: a cube

xposN=xpos;//!!!make sure the player can move aside the wall!!!  
yposN=ypos;

playerControl(); // up, down, left, right (keypressed)

playerRange();//move but stop at the floor or outside the canvas

floorDraw();//function for the ground and floors

//[How do you create a wall? - #3 by colin](https://discourse.processing.org/t/how-do-you-create-a-wall/873/3)

}

void playerDraw() {  
stroke(255, 153, 0);//orange color stroke of the cube  
fill(255, 153, 0); //orange color of the cube  
rect(xpos, ypos, 20, 20);//position and size of the cube  
}

void floorDraw() {  
stroke(0, 153, 255); // blue color of the ground line  
line(0, 310, 640, 310); // Creat a line as ground  
//line(200, 250, 250, 250); // Creat a line as a floor  
}

void playerControl()  
{ if(keyPressed){  
if (keyCode == LEFT || key == ‘a’) {xposN = xpos-2;}  
if (keyCode == RIGHT || key == ‘d’) {xposN = xpos+2;}  
if (keyCode == UP || key == ‘w’) {yposN = ypos-2;}  
if (keyCode == DOWN || key == ‘s’) {yposN = ypos+2;}  
}}

void playerRange() {  
if(!floor(50,50,10,50) && !floor(100,50,10,50)){  
xpos=constrain(xposN,0,width-playersize); //player size  
ypos=constrain(yposN,0,height-playersize-50); //50 is the ground height  
}}

boolean floor(float x1, float y1, float w1, float h1){  
stroke(0, 153, 255);  
fill(0, 153, 255);  
rect(x1, y1, w1, h1);  
return (xposN+playersize\>x1 && xposN\<x1+w1 && yposN+playersize\>y1 && yposN\<y1+h1);  
}

//the reason to use boolean: The operator ! is undefined for the argument type(s) void  
//the reason to use return: the method must return a result of type boolean  
/_boolean floor2(float x2, float y2, float w2, float h2){  
stroke(0, 153, 255);  
fill(0, 153, 255);  
rect(x2, y2, w2, h2);  
return (xposN+playersize\>x2 && xposN\<x2+w2 && yposN+playersize\>y2 && yposN\<y2+h2);  
}_/

 ![Screen Shot 2021-04-17 at 11.31.10 pm](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/72588ed125f929ab061dfb5d8acae5f0c90222e1.png)

The rect on the right will just disappear if the cube hit the left one. I hope I could stop it from disappearing even the cube hit the left one, i.e, I have 2 barriers/walls.

I am a new learner and I got the current code from another forum so I am not sure the reason behind it also even I work on it for many hours.

Appreciate your kind help!

Regard,

Cherie

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 17, 2021, 3:39pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/2 "2021-04-17T15:39:22Z")

</div>

Hi! Welcome to the forum!

Before we begin, I have 2 comments - first please use `</>` button to format your code. Second, if you copied something from another forum, please give us a link - it’s totally ok to reuse and remix, but you must always give a credit (it helps us understand the context, too) 🙂

---

<div class="post-metadata">

**Author:** ![Cherie](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Cherie](https://discourse.processing.org/u/Cherie)\
**Post date:** [April 18, 2021, 12:53pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/3 "2021-04-18T12:53:47Z")

</div>

> [@How do you create a wall?](https://discourse.processing.org/t/how-do-you-create-a-wall/873):
>
> Hi there! I’ve just started with Processing and am still learning. But I’ve ran into a problem already. This is my code: int xPos = 25; int yPos = 25; void setup(){ fullScreen(); } void draw(){ //This is the background color. background(75,75,75); //This is the player's controllable cube. fill(250,0,0); rect(xPos,yPos,15,15); //These are the controls for the player's cube. if(keyPressed == true){ if(keyCode == LEFT){ xPos += -2; } } if(keyPressed == true){ …

This is where I got the original code. Thanks for the reminder.

---

<div class="post-metadata">

**Author:** ![Cherie](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Cherie](https://discourse.processing.org/u/Cherie)\
**Post date:** [April 18, 2021, 12:56pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/4 "2021-04-18T12:56:12Z")

</div>

Thanks for your reminder. The link is attached.  
For the \</\> button, May I know where should I add it to format it? Thanks!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 18, 2021, 1:01pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/5 "2021-04-18T13:01:17Z")

</div>

Hello,

I usually share this link for new users:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

Let me know if it is helpful.

`:)`

---

<div class="post-metadata">

**Author:** ![Cherie](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Cherie](https://discourse.processing.org/u/Cherie)\
**Post date:** [April 18, 2021, 1:05pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/6 "2021-04-18T13:05:06Z")

</div>

Hi thanks but I still can’t get the idea that " press the `</>` code button ".  
What the `</>` code button mean? 😭

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 18, 2021, 2:07pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/7 "2021-04-18T14:07:19Z")

</div>

No worries 🙂 You can find it here when you edit a message:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d5a1a612d4d936d131bf0d19690672a2fd170350.png)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 18, 2021, 3:30pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/8 "2021-04-18T15:30:26Z")

</div>

you can go back and edit your initial post by pressing the “pencil button”

select the code in your post with the mouse and press the "`</>` button" in the small command bar

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 18, 2021, 3:38pm UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/9 "2021-04-18T15:38:14Z")

</div>

It’s a little hard to spot where the error is

reason is this:

` if(!floor(50,50,10,50) && !floor(100,50,10,50)){`

The if has TWO conditions.

BUT: when the first condition is met (or NOT met), the 2nd isn’t executed (to save time when running the Sketch) (which means, the 2nd `floor()` isn’t called so the rect gets not drawn )

Here I avoid this problem:

```auto

float xpos;
float ypos;
float xposN;
float yposN;
float playersize;

PVector position;
float direction;
PVector velocity;
float jumpSpeed;
float walkSpeed;

void setup() {
  size(640, 360);
  //fullScreen();
  xpos = 50;
  ypos = 280;
  playersize=20;
}

void draw() {
  background(0); // Clear the screen with a black background

  playerDraw(); //function for the player: a cube

  xposN=xpos;//!!!make sure the player can move aside the wall!!!
  yposN=ypos;

  playerControl(); // up, down, left, right (keypressed)

  playerRange();//move but stop at the floor or outside the canvas

  floorDraw();//function for the ground and floors

  //https://discourse.processing.org/t/how-do-you-create-a-wall/873/3
}

void playerDraw() {
  stroke(255, 153, 0);//orange color stroke of the cube
  fill(255, 153, 0); //orange color of the cube
  rect(xpos, ypos, 20, 20);//position and size of the cube
}

void floorDraw() {
  stroke(0, 153, 255); // blue color of the ground line
  line(0, 310, 640, 310); // Creat a line as ground
  //line(200, 250, 250, 250); // Creat a line as a floor
}

void playerControl()
{ 
  if (keyPressed) {
    if (keyCode == LEFT || key == 'a') {
      xposN = xpos-2;
    }
    if (keyCode == RIGHT || key == 'd') {
      xposN = xpos+2;
    }
    if (keyCode == UP || key == 'w') {
      yposN = ypos-2;
    }
    if (keyCode == DOWN || key == 's') {
      yposN = ypos+2;
    }
  }
}

void playerRange() {
  // two lines get executed no matter what ! 
  boolean a=floor(50, 50, 10, 50);
  boolean b=floor(100, 50, 10, 50);
  if (!a && !b) {
    xpos=constrain(xposN, 0, width-playersize); //player size
    ypos=constrain(yposN, 0, height-playersize-50); //50 is the ground height
  }
}

boolean floor(float x1, float y1, float w1, float h1) {
  stroke(0, 153, 255);
  fill(0, 153, 255);
  rect(x1, y1, w1, h1);
  return (xposN+playersize>x1 && xposN<x1+w1 && yposN+playersize>y1 && yposN<y1+h1);
}

//the reason to use boolean: The operator ! is undefined for the argument type(s) void
//the reason to use return: the method must return a result of type boolean
/*boolean floor2(float x2, float y2, float w2, float h2) {
 stroke(0, 153, 255);
 fill(0, 153, 255);
 rect(x2, y2, w2, h2);
 return (xposN+playersize>x2 && xposN<x2+w2 && yposN+playersize>y2 && yposN<y2+h2);
 }*/

```

---

<div class="post-metadata">

**Author:** ![Cherie](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Cherie](https://discourse.processing.org/u/Cherie)\
**Post date:** [April 28, 2021, 8:24am UTC](https://discourse.processing.org/t/need-help-with-creating-barriers-walls-with-boolean/29444/10 "2021-04-28T08:24:55Z")

</div>

Thanks for your help!
