I have this racing game for school, and I need some help. Currently, my track is white, and the walls are black. I used this code to stop it from going into the wall, it does stop, but gets stuck in the wall and cannot move, please help!!
/**
* Checks if player is close to the wall. If yes, the player will stop,
* if not, the player will continue moving.
*/
public boolean canMove(String direction, GreenfootImage background) {
try {
if(direction == "up") {
if(background.getColorAt(getX(),getY() - 2).equals(Color.BLACK) ||
background.getColorAt(getX() -2,getY() - 2).equals(Color.BLACK)
|| background.getColorAt(getX() + 2,getY() - 2).equals(Color.BLACK)) {
return false;
}
} else if(direction == "down") {
if(background.getColorAt(getX(),getY() + 2).equals(Color.BLACK)
|| background.getColorAt(getX() - 2,getY() + 2).equals(Color.BLACK)
|| background.getColorAt(getX() + 2,getY() + 2).equals(Color.BLACK)) {
return false;
}
} else if(direction == "left") {
if(background.getColorAt(getX() - 2,getY()).equals(Color.BLACK)
|| background.getColorAt(getX() - 2,getY() + 2).equals(Color.BLACK)
|| background.getColorAt(getX() - 2,getY() - 2).equals(Color.BLACK)) {
return false;
}
} else if(direction == "right") {
if(background.getColorAt(getX() + 2,getY()).equals(Color.BLACK)
|| background.getColorAt(getX() + 2,getY() - 2).equals(Color.BLACK)
|| background.getColorAt(getX() + 2,getY() + 2).equals(Color.BLACK)) {
return false;
}
}
} catch(Exception e) {
return false;
}
return true;
}