move objects correctly arround the squares (orange / blue)

edited November 2017 in Questions about Code

Hello !!

I have been working to progress in programming, but I can not manage to move the Blue Obstacles. The orange obstacle moves one or two places to the blue object, but then the blue object disappears

//build a grid
int rows = 20;
int columns = 20;

//if a cell is true it's an obstacle
final boolean gridObstacle=true; 
final boolean gridFree=false; 

//if a cell is true it's an obstacle
boolean[][] grid = new boolean[rows][columns];

//where the player is in the grid
int posX = 10; //position X 
int posY = 10;

// START NEW GLOBAL LEVEL BLOCK
int thingX;
int thingY;
int time;

boolean freespace( int x, int y ) {

  if ( x < 0 || x >= rows ) return false;
  if ( y < 0 || y >= columns ) return false;

  //if a cell is true it's an obstacle:
  if ( grid[y][x] ) 
    return false; // not free (wall)

  // free 
  return true;
}
// END NEW GLOBAL LEVEL BLOCK

void setup() {
  size(600, 600);
  //randomly place obstacles in the grid
  for (int row = 0; row < grid.length; row++) {
    for (int column = 0; column < grid[row].length; column++) {
      //each cell has a 20% chance of being an obstacle
      if (random(1) < .2) {
        grid[row][column] = true;
      }
    }
  }
}

void draw() {
  background(#4C91E3);
  float cellWidth = width/columns;
  float cellHeight = height/rows;
  for (int row = 0; row < rows; row++) {
    for (int column = 0; column < columns; column++) {
      float cellX = cellWidth*column;
      float cellY = cellHeight*row;
      //fill the obstacles in with red
      if (grid[row][column]) {
        fill(#3237B7);
      } else {
        noFill();
      }
      rect(cellX, cellY, cellWidth, cellHeight);
    }
  }
  float playerPixelX = posX * cellWidth;
  float playerPixelY = posY * cellHeight;
  fill(#FF8C0F);
  rect(playerPixelX, playerPixelY, cellWidth, cellHeight);
  // START NEW DRAW BLOCK
  if ( millis() - time > 100 ) {
    time = millis();
    int rd = int(random(4));
    if ( rd == 0 && freespace( thingX, thingY-1 ) ) {
      thingY--;
    }
    if ( rd == 1 && freespace( thingX, thingY+1 ) ) { 
      thingY++;
    }
    if ( rd == 2 && freespace( thingX-1, thingY ) ) { 
      thingX--;
    }
    if ( rd == 3 && freespace( thingX+1, thingY ) ) { 
      thingX++;
    }
  }
  fill(255, 0, 255); // Fill the thing's cell with purple.
  rect(thingX*cellWidth, thingY*cellHeight, cellWidth, cellHeight);
  // END NEW DRAW BLOCK
}

void keyPressed() { // hear start the programation to move the obstacles and the orange object
  if (keyCode == UP) {
    //up
    if (posY > 0 && !grid[posY-1][posX]) {
      posY--;
    } 
    // a obstacle is present: 
    else if (posY > 0 && grid[posY-1][posX]) {
      grid[posY-1][posX] = gridFree;
      if (freespace(posY-2, posX))
        grid[posY-2][posX] = gridObstacle;
      posY--;
    }
   //////////////////

  } else if (keyCode == DOWN) {
    if (posY < rows-1 && !grid[posY+1][posX]) {
      posY++;
    }
    ////////// a obstacle is present: 
    else if (posY > 0 && grid[posY+1][posX]) {
      grid[posY+1][posX] = gridFree;
      if (freespace(posY+2, posX))
        grid[posY+2][posX] = gridObstacle;
      posY++;
    } 

  /////////////////

  } else if (keyCode == LEFT) {
    if (posX > 0 && !grid[posY][posX-1]) {
      posX--;
    }
        ///////// a obstacle is present: 
    else if (posX > 0 && grid[posX-1][posY]) {
      grid[posX-1][posY] = gridFree;
      if (freespace(posX-2, posY))
        grid[posX-2][posY] = gridObstacle;
      posX--;
    }

    ////////////////

  } else if (keyCode == RIGHT) {
    if (posX < columns-1 && !grid[posY][posX+1]) {
      posX++;
    }
    // a obstacle is present: 
    else if (posX > 0 && grid[posX+1][posY]) {
      grid[posX+1][posY] = gridFree;
      if (freespace(posX+2, posY))
        grid[posX+2][posY] = gridObstacle;
      posX++;
    }
  }
}
//
Tagged:

Answers

  • Please stop the multiple postings.

    at least this one isn't 'urgent'

  • Answer ✓

    Be consistent:

    x first, then y

    columns first, then rows

    Otherwise you will never be able to conquer this.

  • edited November 2017
    int rows = 20;
    int columns = 20;
    boolean[][] grid = new boolean[rows][columns];
    
    int playerX = 10;
    int playerY = 10;
    
    int monsterX;
    int monsterY;
    int monsterT;
    
    int[] dx = { 0, 0, -1, 1 };
    int[] dy = { -1, 1, 0, 0 }; 
    
    boolean canPlayerMove( int x, int y, int d ) {
      if ( x == monsterX && y == monsterY ) { 
        return false;
      }
      if ( freespace(x, y) ) { 
        return true;
      }
      // Must be a crate or the edge then. Can it move?
      if( freespace( x + dx[d], y + dy[d] ) ){ // Edges would fail this check!
        // Yes. So move the crate now and return the fact that the player can move.
        grid[y][x]=false;
        grid[y+dy[d]][x+dx[d]]=true;
        return(true);
      }
      return(false);
    }
    
    boolean freespace( int x, int y ) {
      if ( x < 0 || x >= rows ) return false;
      if ( y < 0 || y >= columns ) return false; 
      return( !grid[y][x] );
    }
    
    void setup() {
      size(611, 611);
      for (int row = 0; row < grid.length; row++) {
        for (int column = 0; column < grid[row].length; column++) {
          if (random(1) < .2) {
            grid[row][column] = true;
          }
        }
      }
    }
    
    void draw() {
      background(0,100,0);
      translate(5, 5);
      // Draw grid.
      float cellWidth = width/columns;
      float cellHeight = height/rows;
      for (int row = 0; row < rows; row++) {
        for (int column = 0; column < columns; column++) {
          fill(#4C91E3);
          if ( grid[row][column] ) {
            fill(#3237B7);
          }
          rect(cellWidth*column, cellHeight*row, cellWidth, cellHeight);
        }
      }
      // Draw player.
      fill(#FF8C0F);
      rect(playerX * cellWidth, playerY * cellHeight, cellWidth, cellHeight);
      // Move monster if it's time to do so.
      if ( millis() - monsterT > 100 ) {
        monsterT = millis();
        int rd = int(random(4));
        if ( freespace( monsterX+dx[rd], monsterY + dy[rd] ) ) {
          monsterX+=dx[rd];
          monsterY+=dy[rd];
        }
      }
      // Draw monster.
      fill(255, 0, 255);
      rect(monsterX*cellWidth, monsterY*cellHeight, cellWidth, cellHeight);
    }
    
    void keyPressed() {
      int d = -1;
      if( keyCode == UP ) d = 0;
      if( keyCode == DOWN ) d = 1;
      if( keyCode == LEFT ) d = 2;
      if( keyCode == RIGHT ) d = 3; 
      if( d>=0 && canPlayerMove( playerX + dx[d], playerY + dy[d], d ) ){
        playerX+=dx[d];
        playerY+=dy[d];
      }
    }
    
Sign In or Register to comment.