import java.awt.Color;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Test here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Test extends Element
{

    public static boolean running = true;
    
    /**
     * Act - do whatever the Test wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(running){
            //turn(Greenfoot.getRandomNumber(20)-10);
            int buf = 10;
            int width = getWorld().getWidth();
            int height = getWorld().getHeight();
            int dir = Greenfoot.getRandomNumber(3);
            boolean move = true;
           
           if(getX() >= (width-10) || getY() >= (height - 10) || getX() <= 10 || getY() <= 10){
                getWorld().removeObject(this);
            }
            if(dir == 0){
                setRotation(0);
                move = !Background.mask.getColorAt(((getX() + buf) >= width ? width-1 : (getX() + buf)), getY()).equals(Color.black);
            }else if(dir == 1){
                setRotation(90);
                move = !Background.mask.getColorAt(getX() , ((getY() + buf) >= height-1 ? height : (getY() + buf))).equals(Color.black);
            }else if(dir == 2){
                setRotation(180);
                move = !Background.mask.getColorAt(((getX() - buf) >= width+1 ? width : (getX() - buf)), getY()).equals(Color.black);
            }else if(dir == 3){
                setRotation(270);
                move = !Background.mask.getColorAt(getX(), ((getY() - buf) >= height ? height+1 : (getY() - buf))).equals(Color.black);
            }
            
            if(move){  
                try{
                    move(7);
                }catch(Error e){
                    getWorld().removeObject(this);
                }
            }
        }
    }

    public Test(){
    }
    
}
