Hi,
I've been playing around with randomisation and I have been trying to have a scenario where a grass square appears in a random position and you press space and there is a 1 in 2 chance it will duplicate itself on the screen. I can't figure out how to get it to work. I've looked at the example of the crab and worms but I want to add an object of itself. It is hard to do this because I cannot declare it. Here is my code:
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 | import greenfoot.*; /** * Write a description of class Grass here. * * @author (your name) * @version (a version number or a date) */ public class Grass extends Actor { int lucky = Greenfoot.getRandomNumber( 2 ); /** * Act - do whatever the Grass wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown( "right" )) { if (lucky == 1 ) { if (grass != null ) { World world; world.getWorld(); world.removeObject(grass); } } } } } |