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

2018/6/4

I'm trying to make this object turn randomly without going through walls.

Lev Lev

2018/6/4

#
I have an object named leviathan, how do i get it to move randomly without letting it go through the walls of the maze?
/**
 * Using the code below the Rogue will turn at random.
 * Right or left.
 */
public void randomTurn()
{
    if ( Greenfoot.getRandomNumber (100) < 10)
    {
        turn( Greenfoot.getRandomNumber (90)-45);
    }
if (canSee(Wall.class))
            {
                move(1);
            }
        }
I posted yesterday asking how to get my rocket to not go through walls when controlling it, so I'm basing my code off of that.
public void checkKeyPressed()
{
    setRotation(90);
    if (Greenfoot.isKeyDown("up"))
    {move(-1); }
    if (canSee(Wall.class))
            {
                move(1);
            }
    if (Greenfoot.isKeyDown("down"))
    {move(1); } 
    if (canSee(Wall.class))
            {
                move(-1);
            }
    setRotation(0);
    if (Greenfoot.isKeyDown("left"))
    {move(-1); }
    if (canSee(Wall.class))
            {
                move(1);
            }
    if (Greenfoot.isKeyDown("right"))
    {move(1); }
    if (canSee(Wall.class))
            {
                move(-1);
            }
           
  }
  
Help would be greatly appreciated!
danpost danpost

2018/6/4

#
Lev wrote...
I have an object named leviathan, how do i get it to move randomly without letting it go through the walls of the maze? << Code Omitted >> I posted yesterday asking how to get my rocket to not go through walls when controlling it, so I'm basing my code off of that. << Code Omitted >>
In each case (direction) in your original code, you did two things -- (1) moved; and (2) if saw wall, moved back. This is not consistent with your new code.
You need to login to post a reply.