A Greenfoot Guide to Creating a Game

This guide will show you how to create a simple game in Greenfoot.

The game will include a controllable main character that must pick up objects and avoid another character.

This page is split into the following sections:

According to the 2016 specification, in the Eduqas exam, you will use Greenfoot version 2.4.2, despite the fact that Greenfoot is now on version 3.6.1.

This means that some newer code won't work! This guide here will work on version 2.4.2.

Just make sure you are also using version 2.4.2 - see the download instruction page for help.

Starting from Scratch

In the Eduqas exam, you will probably be given a scenario with a background and some actors already.

 

Just in case, this introduction section explains how to start from scratch.

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.

 

For a new project, click 'Scenario' and then 'New Java Scenario'.

New Java Scenario

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.

I have named my class 'Sheep' and selected the sheep.png image.

Create a new Actor subclass each for:

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

  • Collectable objects to pick up (e.g. my orange)

  • An  enemy  character to avoid (e.g. my elephant)

 

Now to create a new class for the main character.

Populating and Saving the World

In the Eduqas programming exam you will probably be given a scenario with a background and some actors already.

 

The easiest task (and you actually 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 collectible 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

Is Key Down

Time to open the editor and code 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.

 

You need to use an if statement to check if a certain key (like the right arrow key) is being pressed down.

An if statement must be contained in standard brackets. After each if statement, the proceeding code must be typed within curly brackets - see the image on the left.

Tip - If the brackets pairs are on the same line then use standard brackets ( and )

      If the brackets pairs are on different lines then use curly brackets { and }

0

90

180

270

Inside the if statement brackets, type Greenfoot and a full stop then press Ctrl and Space bar; this brings up a list of possible commands.

Select the isKeyDown command and in the brackets type the first key that you are programming (such as "Right", "Left", "Up" or "Down" for the arrow keys.

The key name must be in speech marks because it is a string.

Within the curly brackets we want to program our main character to set their rotation to a certain direction and to move.

Use Ctrl and Space bar again to select the correct commands. This stops you from incorrectly entering commands.

If a line of code has a white background, that line must end in a semi-colon ;

Use the diagram below for help with how Greenfoot sets out directions.

Test your code works when you have got the right arrow key working.

Press the Run button on the main Greenfoot window.

When you have done the first key, you can copy that code, paste it and edit it for your other keys.

The image to the right shows the code for the right and down arrows.

Click on me if you've got an error that you're stuck with.

  • Use this code to help you also add the left and up keys.
     

  • The diagram below shows you how Greenfoot works out directions.

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

 

Move Randomly & Bounce on Edge

Random Movement

Right-click on your enemy class and select 'Open editor'.

The enemy character should always be moving, so start with move(1);

Next we will use an if statement to start the random movement.

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.

Inside the if statement type Greenfoot and a full stop then press Ctrl and Space Bar to see a list of possible commands.

Select getRandomNumber. Type 10 in brackets and less than 1 after (<1).

This code generates 10 random numbers and if it is less than 1 then it will run the proceeding code. If we don't do this, the character will turn too much or too little.

Use the Ctrl and Space Bar trick for the turn command and the getRandomNumber command.

Type 90 in brackets and subtract 45 ( - 45) after.

This code generates a random angle between 0 and 90. The - 45 part is necessary otherwise the character will always move to the right. See the diagram below for an explanation.

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.

Click on me if you've got an error that you're stuck with.

 

Remove Objects from the World

Removing Objects

Open the editor for your main character.

Underneath the if statements for using the arrow keys is where we type the removal code.

If you have chosen a different collectible object, type the exact name of that class instead of Orange (e.g. Apples or Ants).

Press Run and try to grab all of your collectibles!

Time to code your main character to be removed if they are touched by the enemy objects!

The removal code is placed beneath the code you have already written.

Use the same code as you did above but change the class (instead of Orange.class I have chosen Sheep.class).

Removing Objects - Adding danger to your game

Click on me if you've got an error that you're stuck with.

Press Run and try to avoid the enemies!

Add code to remove your main character if it is touched by the enemy class.

Play Sounds & Stop the Game

Play Audio Files

Any sound files that you may need can be found in the 'sounds' folder of your Greenfoot project.

 

In an exam, sounds will be given for you. 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 istouching method in the enemy class's code.

Add & Use a Counter

Importing a new Counter 

Select the Edit tab then 'Import class...' and choose Counter.

 

Then right-click, choose the New Counter() option and drag it into the world.

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

 

Click on me if you've got an error that you're stuck with.

There are two options for using a counter.

 

In most Eduqas Greenfoot exams you will be given a counter class.

 

You can decide whether to use the counter or import a new one

Using the Counter you are given

Right-click on the counter, choose the New Counter() option and drag it into the world.

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

Increasing the counter requires just one line of code.

Add it within the removal code for your collectible.

Increasing the counter requires just one line of code.

Add it within the removal code for your collectible.

Press Run and check the counter works.

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 collectibles randomly move to make it harder.

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

  • Make the enemies pick up collectibles too.

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

Extension Ideas

 

Right-click on the World class and select 'new subclass...'. 

Chose an appropriate world name, such as MyWorld.

Choose an image from the 'backgrounds' image category, I chose cell.jpg but you can choose any.

Click the Compile button in the bottom right to save the program.

New World Subclass

© CSNewbs 2020