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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | public class Leaf extends Actor { /* Fields Go Here */ public static int numLeaves; int timer; int dx; boolean fallingRight; /* This is a Constructor */ public Leaf() { switch ( Greenfoot.getRandomNumber( 3 ) ) { case 0 : setImage( "Leaf1.png" ); break ; case 1 : setImage( "Leaf2.png" ); break ; case 2 : setImage( "Leaf3.png" ); break ; } setRotation(( int )Math.random()* 360 ); timer =( int )(Math.random()* 900 )+ 100 ; if (( int )(Math.random()* 5 )>= 3 ) { fallingRight= true ; } else if (( int )(Math.random()* 5 )<= 3 ) { } numLeaves++; } public void act() { timer--; if (timer== 0 & timer==- 1 ) { setLocation (getX()+dx/ 2 ,getY()+ 5 -Math.abs(dx)/ 4 ); if (fallingRight == true ) { dx++; if (dx > 20 ) { fallingRight = false ; } } else if (fallingRight == false ) { dx--; if (dx < - 20 ) { fallingRight = true ; } } if (getY() > 600 ) { numLeaves--; getWorld().removeObject( this ); } } } public static int getNumLeaves() { return numLeaves; } public static void reset() { numLeaves = 0 ; } } |
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 | public class Tree extends World { public Tree() { super ( 1000 , 750 , 1 ); Leaf.reset(); } public void addLeaves( int amount ) { for ( int i= 0 ; 0 < amount; i++) { int x = ( int )(Math.random()* 800 )+ 100 ; int y = ( int )(Math.random()* 400 ); Leaf leaves = new Leaf(); addObject (leaves,x,y); } } public void increaseLeavesTo( int amount ) { while (Leaf.numLeaves < amount) { int x = ( int )(Math.random()* 800 )+ 100 ; int y = ( int )(Math.random()* 400 ); Leaf leaves = new Leaf(); addObject (leaves,x,y); } } } |