Features
Teach your kids to code
20080117 [PC Pro]
Design your own game
|
1 Go to www.greenfoot.org to download and install Greenfoot. Note that you must have the Java system installed on your PC to run the software. There's a link on Greenfoot's download page to get a Java system. When you open Greenfoot for the first time, select Open Tutorial and Tutorial Scenario for a brief introduction. |
|
|
2 The tutorial scenario is called "Wombats". On the right-hand side, you'll see some Animal "classes". Right-click on the Wombat class and select New Wombat(). Drag the wombat into the world (the brown area of the main window). Now click on Act to make it move. The onscreen tutorial explains further interaction with objects. |
|
|
3 Download the Crabs scenario from www.greenfoot.org/scenarios/files/crab.zip and unzip it with WinZip. Click on Scenario | Open and select the Crabs folder you've unzipped. Create a crab in the same way we created a wombat, place it in the world, and click run. Nothing will happen; we now need to program the crab to move. |
|
|
4 Open the crab's editor by right-clicking on the crab actor class and selecting Open editor. In the box, Enter the command "move();" between the two { } symbols (as shown), close the editor and click Compile All. Place a crab in the world and click Act. Then click Run and play with the Speed slider to control the crab's movement. |
|
|
5 Now we need to make the crab turn. Open the crab's editor once more and include the turn command shown above. Note the figure in the brackets after the turn command tells the crab how many degrees to turn (five, in our example). Close the editor, press Compile All and try placing multiple crabs in the world to try it out. |
|
|
6 The Move and Turn methods come from the Animal class. You can find out what other methods are available to use by opening the editor of the Animal class (right-click on Animal and select Open Editor), and then switching to Documentation view from the drop-down menu. |
|
|
7 In this list of methods, there's one called "atWorldEdge()" - this makes the crab check if it's reached the edge of the screen and turn. Using this method, we can modify our crab's code, as shown above. Remember once again to click Compile All, then place multiple crabs into the world and click Run to see what happens. |
|
|
8 We now want to add a new animal. Right-click on the Animal class and select New Subclass. Type "Worm" as the class name - make sure you use an upper-case W. You'll see there's a pre-prepared image of a sand worm. Select that image and then click OK. You can then compile again and place the worm into the world. |
|
|
9 Next, we want to control the crab, so that we can catch worms. Every key on the keyboard has a name, for example "left" for the left cursor key. Delete the previous commands from the crab's editor and enter the text shown above. Now when you Compile All and Run you should be able to control the crab with the cursor keys. |
|
|
10 Now, we want the crab to eat the worms when it comes across them. For this, we use the "canSee" and "eat" methods from the Animal class, as shown in the code above. Remember once again to use the upper-case W on Worm.class. Compile, place a crab and some worms into the world, and try it out. |
|
|
11 To spice things up, we can add another creature. Let's add a lobster that runs around randomly and tries to eat the crab. Add the lobster as before, and write the code shown above into its editor. Recompile, then add a crab, three lobsters and a few worms, and see whether you can eat the worms before the lobsters catch you! |
|
|
12 The final step is to add sound effects. In the crab class, at the place in the code where the crab eats the worm, place the command shown above to play a sound. Two sound files, named slurp.wav and au.wav, are provided. Similarly, add the au.wav sound in the Lobster's editor, where the lobster eats the crab. |
|
|
|