When a character is going to move, first check if where it’s moving to is a valid place for it to be. If it’s not, don’t move there! It’s as simple as adding an if statement.
class Player {
int x, y;
/// ...
void move(int dx, int dy){
// Prevent movement into solid walls.
if( is_solid( level_data[x+dx][y+dy] ) ) return;
// Do movement.
x += dx;
y += dy;
}
boolean is_solid( int wall_type ){
return( wall_type == 1 || wall_type == 2 );
}
}
You might also need to add some logic to check that the position you are moving to is inside your grid too. For example, if your player is in the top left, (0,0), and trieds to move left, you don’t want to try to access level_data[-1][0]!