We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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++;
}
}
}
//
Answers
Please stop the multiple postings.
at least this one isn't 'urgent'
Be consistent:
x first, then y
columns first, then rows
Otherwise you will never be able to conquer this.