This site requires JavaScript, please enable it in your browser!
Greenfoot back
ChessMaster123
ChessMaster123 wrote ...

2017/5/18

Random movement made easy?

ChessMaster123 ChessMaster123

2017/5/18

#
This is my code as it stands, I was wondering if anyone could suggest an easier way to achieve random movement to the same standard?
 public void act() 
    {
        move(4);
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
    } 
    if (getX() <=5 || getX() >= getWorld().getWidth() - 5)
    {
        turn(180);
    }
    if (getY() <=5 || getY() >= getWorld().getHeight() - 5)
    {
        turn(180);
    }
}
}
USER_NAME USER_NAME

2017/5/18

#
Hi, This would be very useful for me as well. Thanks, USER_NAME
USER_NAME USER_NAME

2017/5/18

#
USER_NAME wrote...
Hi, This would be very useful for me as well. Thanks, USER_NAME
Yehuda Yehuda

2017/5/19

#
ChessMaster123 wrote...
I was wondering if anyone could suggest an easier way to achieve random movement to the same standard?
Your code seems pretty simple, but I do have a simplification for it. You can remove lines 11 - 13 then include the 'if' statement from line 12 in line 8, since both 'if' statements have the same coding in them you really only need one of them.
USER_NAME USER_NAME

2017/5/23

#
Ok, thanks
You need to login to post a reply.