# Stop player from going out of board

**URL:** https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858
**Category:** Coding Questions
**Created:** [October 15, 2021, 11:56pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858 "2021-10-15T23:56:46Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![kiddish](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@kiddish](https://discourse.processing.org/u/kiddish)
#### Post date: [October 15, 2021, 11:56pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/1 "2021-10-15T23:56:46Z")

</div>

After a few tries, it still won’t get to work.

Somewhat created a game in which the object collides with multiple stuff on the map.  
I have created a player, which is the image I can move with the cursor. What I actually want is that the player isn’t able to touch the border in the board, those are the 1 in the array. Also, a possibility when the player picks up the “chests” it disappears

**FighterGame**

```auto
int xblocks;
int yblocks;
int blockSize = 40;
int score = 0;
PImage friendly;
Friendly f1;

void setup() {
  
  size(600, 600);

  setupGame();
  friendly = loadImage("spaceman.png");
  f1 = new Friendly(30,50,128,50);
  
 
}

void draw() {

drawGame();

  f1.display();
  f1.keyPressed();
  collision();
  textAlign(RIGHT);
  textSize(50);
  text(score, width - 70, 100);
  
}

```

**Friendly**

```auto
class Friendly{
  
 
  int friendlyX;
  int friendlyY;
  int friendlyWidth = 20;
  int friendlyHeight = 50;
  float direction;
  boolean dead = false;
  int maxHealth = 100;
  int health = 100;
  int healthDecrease = 1;
  int healthBarWidth = 60;

  Friendly(int tempX, int tempY, int tempWidth, int tempHeight){
    friendlyX = tempX;
    friendlyY = tempY;
    friendlyWidth = tempWidth;
    friendlyHeight = tempHeight;
  }

  void display(){
  
    noStroke();
    fill(189, 195, 199);
    rectMode(CORNER);
    rect(friendlyX-(healthBarWidth/2), friendlyY, healthBarWidth, 5);
    if (health > 60) {
      fill(46, 204, 113);
    } else if (health > 30) {
      fill(230, 126, 34);
    } else {
      fill(231, 76, 60);
    }
    rectMode(CORNER);
    rect(friendlyX-(healthBarWidth/2), friendlyY, healthBarWidth*(health/maxHealth), 5);

        image(friendly, friendlyX, friendlyY, friendlyWidth, friendlyHeight);
  
    }

  void decreaseHealth(){
  health -= healthDecrease;
  if (health <= 0) {
    gameOver();
    }
  }

void gameOver() {
  println("game over");
  noLoop();
}
  void keyPressed(){

  if (keyPressed) {

      if (keyCode == RIGHT){
          friendlyX = friendlyX + 4;
          direction = 0;
      }else if (keyCode == LEFT){
          friendlyX = friendlyX - 4;
          direction = PI;
      }else if (keyCode == UP){
              friendlyY = friendlyY - 4;
              direction = PI * 3/2;     
      }else if (keyCode == DOWN){
              friendlyY = friendlyY + 4;
              direction = PI/2;
        }
  
      
      
  
  }
  
  

  }
 
}

```

**Board**

```auto
int[][] board = {  
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

```

**Game Board**

```auto

final int boardwidth = board[0].length;
final int boardheight = board.length;
int blockDiameter;

void setupGame() {

  blockDiameter = width/(boardwidth+2);
  int chestAmount = 10;
  placeChest(chestAmount);
  
}
void collision(){
}
  

void drawGame() {
  background(#FFFFFF);
  tekenBord();
}

void drawBoard() {
  int marge = blockDiameter;
  int x = marge, y = marge;

  for (int r = 0; r < boardheight; r++) {
    for (int k = 0; k < boardwidth; k++) { 

      noStroke();
      if (board[r][k] == 0) {  
        fill(#FFFFFF); 
      } 
      if (board[r][k] == 1) {  
        fill(0);
      }    
      if (board[r][k] == 2) {  
        
        fill(#FFFF00); 
      }     
      rect(x, y, blockDiameter, blockDiameter);
      x = x+ blockDiameter;
    }
    y = y+ blockDiameter;
    x = marge;
  }
}

void placeChest(int amount) {

  int r = round(random(boardheight-1));
  int k = round(random(boardwidth-1));
  for (int i = 0; i< amount; i++) {
    while (board[r][k] > 0) {
      r = round(random(boardheight-1));
      k = round(random(boardwidth-1));
    }
    board[r][k] = 2; 
  
}
  
}

```

Also would accept tips 🙂

---

<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: [October 16, 2021, 12:21pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/2 "2021-10-16T12:21:00Z")

</div>

> [@kiddish](#):
>
> ```auto
> if (keyCode == RIGHT){
> friendlyX = friendlyX + 4;
> direction = 0;
> 
> ```

Is this where you move your player?

Before changing the position check with if  
whether the next field on the map is empty

So when the current field is x,y and new direction is 0 check field x-1,y

---

<div class="post-metadata">

### Author: ![kiddish](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@kiddish](https://discourse.processing.org/u/kiddish)
#### Post date: [October 16, 2021, 5:05pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/3 "2021-10-16T17:05:53Z")

</div>

Yes, this makes it possible to move. The player goes out of the window when I try to check if it is within the map.

```auto
  void keyPressed(){

  if (keyPressed) {

      if (keyCode == RIGHT){
          friendlyX = friendlyX + 4;
          direction = 0;
      }else if (keyCode == LEFT){
          friendlyX = friendlyX - 4;
          direction = PI;
      }else if (keyCode == UP){
              friendlyY = friendlyY - 4;
              direction = PI * 3/2;     
      }else if (keyCode == DOWN){
              friendlyY = friendlyY + 4;
              direction = PI/2;
        }
  }

```

---

<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: [October 16, 2021, 5:43pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/4 "2021-10-16T17:43:10Z")

</div>

> [@kiddish](#):
>
> What I actually want is that the player isn’t able to touch the border in the board, those are the 1 in the array

Before changing the position check with if  
whether the next field on the map (your `board`) is empty

So when the current field (on your board) is x,y and new direction is 0 check field x+1,y on board; when it’s 1, don’t add: ` friendlyX = friendlyX + 4;`

---

<div class="post-metadata">

### Author: ![kiddish](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@kiddish](https://discourse.processing.org/u/kiddish)
#### Post date: [October 16, 2021, 6:53pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/5 "2021-10-16T18:53:42Z")

</div>

I have the wrong idea of implementing if checking the next field is empty. any example of how to check the next field in my map? I seem to get stuck at that point.

---

<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: [October 16, 2021, 6:55pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/6 "2021-10-16T18:55:01Z")

</div>

Do you have any information in which field of the map your player currently is?

the x,y in my post

---

<div class="post-metadata">

### Author: ![kiddish](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@kiddish](https://discourse.processing.org/u/kiddish)
#### Post date: [October 16, 2021, 7:02pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/7 "2021-10-16T19:02:40Z")

</div>

The player gets printed out on the screen and not actually got put on the map. So the player doesn’t have a place on the map as of yet.

---

<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: [October 16, 2021, 7:29pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/8 "2021-10-16T19:29:06Z")

</div>

> [@kiddish](#):
>
> map

that you should make your first task then

---

<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: [October 16, 2021, 7:53pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/9 "2021-10-16T19:53:23Z")

</div>

not enough time

this shows red point for current board pos xglobal,yglobal

you can’t move right when INSIDE a wall (see keyPressed) (should be: when BEFORE a wall)

check for errors

```auto
int xblocks;
int yblocks;
int blockSize = 40;
int score = 0;

PImage friendly;
Friendly f1;

int xglobal, yglobal;

void setup() {

  size(600, 600);

  setupGame();
  friendly = loadImage("spaceman.png");
  f1 = new Friendly(30, 50, 128, 50);
}

void draw() {

  drawGame();

  f1.display();
  f1.keyPressed();
  collision();
  textAlign(RIGHT);
  textSize(50);
  text(score, width - 70, 100);
}

// ----------------------------------------------------

class Friendly {

  int friendlyX;
  int friendlyY;
  int friendlyWidth = 20;
  int friendlyHeight = 50;
  float direction;
  boolean dead = false;
  int maxHealth = 100;
  int health = 100;
  int healthDecrease = 1;
  int healthBarWidth = 60;

  Friendly(int tempX, int tempY, int tempWidth, int tempHeight) {
    friendlyX =144; //tempX;
    friendlyY = tempY;
    friendlyWidth = tempWidth;
    friendlyHeight = tempHeight;
  }

  void display() {

    noStroke();
    fill(189, 195, 199);
    rectMode(CORNER);
    rect(friendlyX-(healthBarWidth/2), friendlyY, healthBarWidth, 5);
    if (health > 60) {
      fill(46, 204, 113);
    } else if (health > 30) {
      fill(230, 126, 34);
    } else {
      fill(231, 76, 60);
    }
    rectMode(CORNER);
    rect(friendlyX-(healthBarWidth/2), friendlyY, 
      healthBarWidth*(health/maxHealth), 5);

    fill(0); 
    text("friendly", 
      friendlyX, friendlyY, friendlyWidth, friendlyHeight);
  }

  void decreaseHealth() {
    health -= healthDecrease;
    if (health <= 0) {
      gameOver();
    }
  }

  void gameOver() {
    println("game over");
    noLoop();
  }

  void keyPressed() {

    if (keyPressed) {

      if (keyCode == RIGHT) {
        direction = 0;
        if (board [xglobal+4][yglobal] != 1&&
          board [xglobal+1][yglobal] != 1 &&
          board [xglobal+2][yglobal] != 1 &&
          board [xglobal+3][yglobal] != 1) {
          friendlyX = friendlyX + 4;
        }
      } else if (keyCode == LEFT) {
        friendlyX = friendlyX - 4;
        direction = PI;
      } else if (keyCode == UP) {
        friendlyY = friendlyY - 4;
        direction = PI * 3/2;
      } else if (keyCode == DOWN) {
        friendlyY = friendlyY + 4;
        direction = PI/2;
      }
    }
  }
}

// --------------------------------------------------

int[][] board = {  
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

//-----------------------------------------------------

final int boardwidth = board[0].length;
final int boardheight = board.length;
int blockDiameter;

void setupGame() {

  blockDiameter = width/(boardwidth+2);
  int chestAmount = 10;
  placeChest(chestAmount);
}
void collision() {
}

void drawGame() {
  background(#FFFFFF);
  drawBoard();// tekenBord();
}

void drawBoard() {
  int marge = blockDiameter;
  int x = marge, y = marge;

  for (int r = 0; r < boardheight; r++) {
    for (int k = 0; k < boardwidth; k++) { 

      // -------
      noStroke();
      if (board[r][k] == 0) {  
        fill(#FFFFFF);
      } 
      if (board[r][k] == 1) {  
        fill(0);
      }    
      if (board[r][k] == 2) {  
        fill(#FFFF00);
      }     
      rect(x, y, 
        blockDiameter, blockDiameter);

      if (f1.friendlyX > x && f1.friendlyX < x+blockDiameter &&
        f1.friendlyY > y && f1.friendlyY < y+blockDiameter )
      { 
        xglobal=r; 
        yglobal=k;
        fill(255, 0, 0); 
        ellipse(x+blockDiameter/2, y+blockDiameter/2, 
          6, 6);        
        fill(0, 255, 0); 

        ellipse(x+blockDiameter+(blockDiameter/2), y+blockDiameter/2, 
          6, 6);
      }

      x = x + blockDiameter;
    }
    y = y+ blockDiameter;
    x = marge;
  }
}

void placeChest(int amount) {

  int r = round(random(boardheight-1));
  int k = round(random(boardwidth-1));
  for (int i = 0; i< amount; i++) {
    while (board[r][k] > 0) {
      r = round(random(boardheight-1));
      k = round(random(boardwidth-1));
    }
    board[r][k] = 2;
  }
}
//

```

---

<div class="post-metadata">

### Author: ![kiddish](https://avatars.discourse-cdn.com/v4/letter/k/f14d63/32.png) [@kiddish](https://discourse.processing.org/u/kiddish)
#### Post date: [October 18, 2021, 3:07pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/10 "2021-10-18T15:07:05Z")

</div>

Thank you, There are no errors with it, only problem is the image which disappears after increasing the size.

---

<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: [October 18, 2021, 5:36pm UTC](https://discourse.processing.org/t/stop-player-from-going-out-of-board/32858/11 "2021-10-18T17:36:32Z")

</div>

I can’t help you with that

Also the image should be inside the class because it belongs to it

Also, you should load it in the constructor of the class and resize () it there

To use image () with 5 parameters instead of 3 is very costly to the processor

When you use resize, 3 parameters are enough
