Greenfoot Game Walkthrough

This walkthrough will show you how to create a simple game in Greenfoot. It is split into the following sections:
 

 

Starting from Scratch

In an exam, you should be given a scenario with a background and some actors already. However, this introduction will show you how to create actors and change the background image.

New Java Scenario

Open Greenfoot. If you have never used it before then it will already be empty and ready to use.

 

Otherwise, it will automatically load the last project you worked on. Therefore create a new Java scenario.

Background (World) Image

Right-click on the MyWorld class and select Set image...

Choose any picture from the 'backgrounds' image category.

Creating the Main Character

Right-click on the Actor class and select 'New subclass...'

Give the new object an appropriate name and choose a relevant image.

Repeat this stage to create 3 new subclasses:

  • The main character you will control (e.g. my sheep)

  • Objects for them to collect (e.g. my orange)

  • Enemy objects (e.g. my elephant)

Populating and Saving the World

In an exam, you should be given a scenario with a background and some actors already. The easiest step (and you get marks for it!) is to populate the world with some of the objects and save them in place.

New Object Placements

Right-click on your main character object and select the top option, e.g. new Sheep().

Drag your mouse to the world and click to drop it.

Complete the following actions:

  • Place 1 main character object.

  • Place 5 collectable objects.

  • Place 2 enemy objects.

Saving the World

Once you have populated your world with objects, then right-click on the world and select 'Save the World'.

This saves the positions of each object so that it won't reset every time you start a new game.

Move with the Arrow Keys

Automatic Movement

Firstly, we will practice how to use the editor and get the main character to move.

Right-click on your main character object and select 'Open editor'.

The editor allows you to write different methods - actions that the class can perform.

The act() method will repeat whenever the Run button is pressed.

Overwrite the comment in the act method with move(10);

Each line of code needs to end with a semi-colon.

Change the number on the move line to make you character move faster or slower.

Using the Arrow Keys

Delete the 'move(10);' code that we wrote above - that was just a practice example.

Copy the code to the left. This will check to see if the up arrow has been pressed on the keyboard.

Important - Greenfoot uses a capitalisation system where the first word is lowercase, but each subsequent word is uppercase - like isKeyDown. 

Important - You must have the same number of curvy brackets at all times (e.g. 2 green, 2 yellow, 2 grey in my example). An if statement needs an open curvy bracket { and a close curvy bracket }.

Inside the if statement's curvy brackets copy the code to change the character's rotation to look up and move.

Once you have written the code above for the up arrow, copy it and paste three more if statements, keeping them all in the yellow box. Change the contents so you have one if statement for each arrow:

  • Up

  • Down

  • Right

  • Left

Remember to change the arrow name (e.g. "Left") and rotation (see the graph right) for each different if statement!

With all four if statements completed, your main character will move in the respective direction when you press the arrow keys. Game on!

 
 

ERROR?  Fix it here

 

Move Randomly & Bounce on Edge

Random Movement

Right-click on your enemy class and select 'Open editor' to start writing code that will make them randomly move around the world.

Delete the // comment and start with move(1);

This will make the enemy always move.

Underneath, copy the if statement. This generates a random number under 100 and checks if the random number is less than 10. This creates a 1 in 10 probability - if we didn't have this part then the enemy would turn all the time and it would not look natural.

Inside the if statement copy the code.

This turns the enemy object a random angle between 45 and  -45 degrees (slight right to slight left). If you just wrote to turn a random number between 1 and 90 it would always turn left - that is why we take away 45! See below:

Slight Left (-45)

Slight Right (45)

Straight (0)

Straight (0)

Slight Right (45)

Right (90)

turn(Greenfoot.getRandomNumber(90);

turn(Greenfoot.getRandomNumber(90)-45);

Click Run to see your enemy objects randomly move around the world.

Bounce at World Edge

You may have noticed that the enemy objects can get 'stuck' in the corners or when they hit the edge of the world.

In the act() method, below the random movement statement, add this code that checks to see if the object is at the edge, and turns it around (180 degrees) if it is.

 

Remove Objects from the World

Removing Objects

Open the editor for your main character.

Underneath the if statements for using the arrow keys you need to copy the code to the right.

Instead of writing Orange, you need to type the name of the collectable class that your main character will pick up (e.g. Apples or Ants).

This code creates a new variable and looks at the same co-ordinates (0,0) as wherever your main character currently is - to see if it is on top of the collectable object.

Underneath the code you just wrote above, add the if statement that checks if your collectable is in place. If it is it removes that object from the world (makes it disappear).

When your code is written, press Run and try to collect all of your collectables!

Once you have written the code above for making your collectables disappear, you know need to add some danger to your game and have your main character disappear if they touch the enemy objects!

Copy the code that you just wrote for removing the collectables and paste it into your enemy's code, directly after the edge bouncing code (but still inside the act() method).

Change the code so that it says the name of your main character instead of your collectables (e.g. Sheep instead of Orange in my game).

Removing Objects - Danger!

ERROR?  Fix it here

ERROR?  Fix it here

Play Sounds & Stop the Game

Play Audio Files

Any sounds that you want to use can be found (and have to be placed) in the sounds folder of your Greenfoot project.

 

In an exam, sounds will be given for you if you require them. I have placed a sound I made for when my main character 'eats' an orange - I have placed it inside the same if statement that will remove the orange from the world.

Stop the Game

When an enemy object 'eats' the main character then the game should be stopped - it is a simple single line of code that you can add to the remove object if statement in the enemy class's code.

Add & Update a Counter

Step 1: Import Counter

The counter is a pre-made class that can be imported from Greenfoot's library of code so that you don't need to code it all yourself.

Select Edit then 'Import class...' and choose Counter. Then right-click, choose the second option and drag it into the world.

Step 2: Class Code for using the Counter

Each Class that wants access to the counter (to add or take away values) needs to be set up properly.

My sheep should add 1 to the counter when it eats an orange so I have opened the editor for my sheep class.

First, you need to create a new counter variable before the act() method is defined.

Second, a public method needs to be created after the act() method that changes the counter to pointcounter so values can be increased / decreased.

Where I have typed 'Sheep' refers to the name of the class that is accessing the counter (in this instance my main character 'Sheep').

Third, you can add the code to change the value of the counter, inside the if statement where your main character picks up the collectable.

To decrease the value, use negative numbers.

 

This is the most challenging part of using Greenfoot for making a basic game. You will encounter errors, which should be cleared up once you finish all 3 main steps. Make sure you follow the instructions carefully.

Step 3: World Code

Right-click on MyWorld and select 'Open editor'.

You must right-click on the background and select 'Save the World' once you have dragged the counter into the world!

If everything is error-free and you've followed the steps carefully, you should be able to pick up collectables and increase the counter score.

For each class you have added counter code to, you must type 'counter' into the empty brackets - they should be underlined with the red error squiggle.

Coming

Soon

Once you have dragged the counter over and saved the world,

open the MyWorld editor (right-click and select 'Open editor').

The code to place the counter in the world will now appear on the last two lines, but this needs to be at the top (so the other classes can use the counter later).

Highlight the two counter lines, cut them and paste them at the top.

ERROR?  Fix it here

This concludes the walkthrough for a simple Greenfoot game! Try a combination of the suggestions below to add complexity to your game:

  • Add multiplayer functionality (create a second main character and have them move using different keys (e.g. WASD)

  • Make the collectables randomly move to make it harder.

  • Add ‘bad’ collectables that decrease the score if they are picked up.

  • Make the enemies pick up collectables too.

  • Have one type of collectable for player 1 and another for player 2, if the wrong collectable is picked up by a player, have them lose a point.

Extensions

 

© CSNewbs 2020