1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import greenfoot.*; /** * Write a description of class Pear here. * * @author (your name) * @version (a version number or a date) */ public class Pear extends Actor { private int speed; /** * Create the pear. */ public Pear() { speed = Greenfoot.getRandomNumber( 3 ) + 1 ; setRotation(Greenfoot.getRandomNumber( 360 )); } /** * Move around. */ public void act() { if (isAtEdge()) { turn( 180 ); } move(speed); if (Greenfoot.getRandomNumber( 100 ) < 50 ) { turn(Greenfoot.getRandomNumber( 5 ) - 2 ); } } } |
Jane Smith wrote...
"In Exercise 7.19 in the course textbook we are asked to modify the pear-moving code so that pears are only moved right if they are not already at the right edge. If they are at the right edge, they are moved the left edge instead." As you can see from my code I have everything completed up to an including 7.18 but now I am stuck. I just don't know how to code the scenario so that it recognizes "not already at the right edge." In addition, 7.20 wants the program to recognize if we are on the left half or the right half of the World. Sorry I just don't know this one.