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

2017/3/8

How do I make worms turn as follows?

Mjosue Mjosue

2017/3/8

#
i. Give the worms a chance to survive! Have the worms respond to the “up” and “down” arrow keys on the keyboard by moving the worms left or right, respectively, a random number of steps between 0 and 3. ii. Turn the worms randomly left or right up to 18 degrees when they reach the end of the World.
Super_Hippo Super_Hippo

2017/3/8

#
What have you tried?
Mjosue Mjosue

2017/3/8

#
public class Worm extends Actor { public void act() { if ( isAtEdge() ) { turn(3); } if ( Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(60) - 18); } move(3); checkKeypress(); } public void checkKeypress() { if (Greenfoot.isKeyDown("down")) { turn(-3); } if (Greenfoot.isKeyDown("up")) { turn(3); } } }
Mjosue Mjosue

2017/3/8

#
they dont turn randomly when they reach the edge.
Mjosue Mjosue

2017/3/8

#
im supposed to edit as little as possible within this code. it is mostly adding to it
Super_Hippo Super_Hippo

2017/3/8

#
You have 'turn(3)'. This is not random and it is not 18 degrees. However, 18 degrees is not really doable because turning 18 degrees does not guarantee to leave the edge. However, according to your question:
if (isAtEdge())
{
    turn((Greenfoot.getRandomNumber(2)*2-1)*18);
}
This turns either +18 or -18.
Mjosue Mjosue

2017/3/8

#
Ooh
Mjosue Mjosue

2017/3/8

#
Ill try it as soon as i get home. Thanks
You need to login to post a reply.