# How do you create a wall?

**URL:** <https://discourse.processing.org/t/how-do-you-create-a-wall/873>\
**Category:** Beginners\
**Created:** [June 11, 2018, 2:38pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873 "2018-06-11T14:38:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ssskyyy](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Ssskyyy](https://discourse.processing.org/u/Ssskyyy)\
**Post date:** [June 11, 2018, 2:38pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/1 "2018-06-11T14:38:04Z")

</div>

Hi there!

I’ve just started with Processing and am still learning. But I’ve ran into a problem already.

This is my code:

```auto
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){
    if(key == 'a'){
      xPos += -2;
    }
  }
  if(keyPressed == true){
    if(keyCode == RIGHT){
      xPos += 2;
    }
  }
  if(keyPressed == true){
    if(key == 'd'){
      xPos += 2;
    }
  }
  if(keyPressed == true){
    if(keyCode == UP){
      yPos += -2;
    }
  }
  if(keyPressed == true){
    if(key == 'w'){
      yPos += -2;
    }
  }
  if(keyPressed == true){
    if(keyCode == DOWN){
      yPos += 2;
    }
  }
  if(keyPressed == true){
    if(key == 's'){
      yPos += 2;
    }
  }
}

```

How can I create a proper wall?  
If someone could provide me examples, that would be much appreciated.

Thank you!

~ Sky

---

<div class="post-metadata">

**Author:** ![sableraph](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sableraph/32/251_2.png) [@sableraph](https://discourse.processing.org/u/sableraph)\
**Post date:** [June 11, 2018, 2:56pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/2 "2018-06-11T14:56:41Z")

</div>

Hi Sky,

I assume you are trying to prevent the cube from moving out of the window.

After you change the value of xPos and yPos, you can use an if statement to check whether they are still inside the window, and if not, force them to be equal to the last suitable value.

Here’s a modified version of your code that checks for one of the sides. You should be able to infer the other sides from there.

I made two other changes:

- The size of the cube is a variable now, for reasons that should become clear once you try to implement the bottom and right walls
- Redundant if statements in your keyPressed code have been removed or combined

```auto
int xPos = 25;
int yPos = 25;

int cubeSize = 15;

void setup() {
  size(800,600,P2D);
  //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, cubeSize, cubeSize);
  
  //These are the controls for the player’s cube.
  if (keyPressed == true) {
    
    if (keyCode == LEFT || key == 'a') {
      xPos += -2;
    }
    
    else if (keyCode == RIGHT || key == 'd') {
      xPos += 2;
    }
    
    else if (keyCode == UP || key == 'w') {
      yPos += -2;
    }
    
    if (keyCode == DOWN || key == 's') {
      yPos += 2;
    }
    
    // This checks for out of bounds position
    if (xPos < 0) {
      xPos = 0;
    } // Put other boundary checks below...
   
  }
}

```

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colin/32/230_2.png) [@colin](https://discourse.processing.org/u/colin)\
**Post date:** [June 11, 2018, 3:03pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/3 "2018-06-11T15:03:45Z")

</div>

What do you mean by wall ? Would you like to draw like another rectangle in your scene with an hitbox ? If so, here is a suggestion.

```auto
int xPos = 25;
int yPos = 25;
int xPosN;
int yPosN;
int playerSize=15;

void setup() {
  fullScreen();
}

void draw() {
  background(75, 75, 75);
  xPosN=xPos;
  yPosN=yPos;
  if(keyPressed)control();
  fill(250, 0, 0);
  rect(xPos, yPos, playerSize, playerSize);
  if(!wall(50,50,50,50)) move();
}

void control() {
  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 move() {
  xPos=constrain(xPosN,0,width-playerSize);
  yPos=constrain(yPosN,0,height-playerSize);
}

boolean wall(int x, int y, int w, int h) {
  fill(0, 250, 0);
  rect(x, y, w, h);
  return (xPosN+playerSize>x && xPosN<x+w && yPosN+playerSize>y && yPosN<y+h);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 11, 2018, 3:03pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/4 "2018-06-11T15:03:48Z")

</div>

[http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp](http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp)

---

<div class="post-metadata">

**Author:** ![Ssskyyy](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Ssskyyy](https://discourse.processing.org/u/Ssskyyy)\
**Post date:** [June 11, 2018, 3:39pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/5 "2018-06-11T15:39:00Z")

</div>

Hi people!

I’m making a maze game, so I need walls that the cube can’t pass through.

This is an example:

 ![simpleexample02](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/6d46f0956e5f42940a086a4297c7bacb19ac5c1e.png)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 11, 2018, 3:48pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/6 "2018-06-11T15:48:52Z")

</div>

[http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Ql2E8JBC54LJ](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Ql2E8JBC54LJ)

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [June 11, 2018, 5:04pm UTC](https://discourse.processing.org/t/how-do-you-create-a-wall/873/7 "2018-06-11T17:04:31Z")

</div>

It sounds like you’re looking for **collision detection** which allows you to detect when two shapes are overlapping. The idea is you’d check whether your player was overlapping with a wall, and not let them move in that direction.

Shameless self-promotion:

> **[Processing Collision Detection](https://happycoding.io/tutorials/processing/collision-detection)**
>
> Learn how to trigger an action when objects overlap in Processing!
