Features
Teach your kids to code Part 2
20080512 [PC Pro]
Design your own game
|
1 Install the Greenfoot software from our cover disc - you'll need the latest version of the software. Download a Java system from www.pcpro.co.uk/links/164java. Copy the Pengu scenario from the cover disc to your desktop. Launch the Greenfoot app, select Scenario | Open and pick the Pengu folder you've placed on your desktop. |
|
|
2 In the Greenfoot software, click Scenario | Open and choose the Pengu scenario you just copied to the desktop. If you click the Run button, you'll see that nothing happens. We need to write some code to help the penguin move. |
|
|
3 We will now help Pengu cross from one side to the other. Open Pengu's editor by right-clicking on the Pengu class in the right-hand panel and select Open Editor. There is currently no code for the penguin, which is why he doesn't do anything. |
|
|
4 First, we're going to make Pengu move when the cursor keys are pressed. Type the code above into your editor and close the editor. Then press the Compile All button and click Run again. Press the cursor keys and Pengu should start moving. |
|
|
5 The "moveLeft" and "moveRight" methods come from the Mover class - right-click on the Mover icon in the right-hand panel and open its editor to see other methods. You can switch between documentation and source code with the dropdown menu in the top right of the window. Other methods are available, such as "onGround" and "fall". |
|
|
6 Now Pengu can move, we're going to make him fall when he walks off the edge of the cliff. To do this, we write and call a "checkFall", which makes Pengu fall when not on solid ground. We use the "onGound" and "fall" methods from Mover to achieve this. Type the code above into Pengu's editor, click Compile All and Run to test. |
|
|
7 At the moment, Pengu is always looking to the right, even when moving left. Included with this project are two different image files: pengu-right.png and pengu-left.png. By inserting the two lines starting with "setImage" as shown above, we can make Pengu face the direction in which he's moving. |
|
|
8 Next, we'll make the cloud move back and forth so that Pengu can use it to get over the canyon. Open the editor for the Cloud and enter the code shown above. We use "setLocation" to change the cloud's x co-ordinate. We've declared the actual speed of movement (4) in a variable at the top of the class so it can be found and modified later. |
|
|
9 We made the cloud move in the previous step, but it moved too far to the right and didn't turn. Now we add the code highlighted above to make it turn at the right points. We define two variables with the x co-ordinates to show where we want it to turn, then add code to reverse the direction whenever the cloud reaches one of those points. |
|
|
10 The next task is to make Pengu jump so he can hop on to the cloud. Back in the Pengu editor, we add the code highlighted above to our "checkKeys" method to make Pengu jump when the space bar is pressed and he's on firm ground. For the jump itself, we set the vertical speed to -16 (movement upwards) and then start a fall from there. |
|
|
11 If you compiled and ran the code in step 10, you probably noticed that the cloud moves out from under the penguin's feet. We now modify the cloud so that Pengu moves with it when he's aboard. The code highlighted here (in the Cloud class) finds the penguin object and synchronises its location with the cloud's own location. |
|
|
12 That's the end of our tutorial, but this is only the beginning of a possible game. There are many directions you could go from here. Continue to play with this scenario, or download one of the many other code examples from www.greenfoot.org/scenarios. |
|