Stop player from going out of board

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

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

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


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 :slight_smile:

1 Like

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

1 Like

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.

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

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;

1 Like

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.

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

the x,y in my post

1 Like

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.

that you should make your first task then

1 Like

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

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


1 Like

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

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

1 Like