How do you create a wall?

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){
    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

1 Like

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
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...
   
  }
}
1 Like

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.

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

1 Like

http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp

Hi people!

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

This is an example:

http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Ql2E8JBC54LJ

1 Like

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:

2 Likes