how to turn an object 90 degrees to the direction of the key you pressed (up, down, left, right)
int dx = 0; // for horizontal direction
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
int dy = 0; // for vertical direction
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (dx*dy == 0 && dx+dy != 0) // if one is zero and the other is not
{
int rotation = 0;
if (dx != 0) rotation = 90*(1-dx); // horizontal
else rotation = 90*(2-dy); // vertical
setRotation(rotation); // turn
}