This site requires JavaScript, please enable it in your browser!
Greenfoot_back

Tutorial 2: Movement and Key Control

This tutorial will explain how to do movement in Greenfoot, and how to control actors with the keyboard.

The Crabs Scenario

Download the crab zip file the starting crab scenario and unzip the contents to somewhere on your hard disk. Then open the scenario in that location in Greenfoot; you should see the standard Greenfoot interface, with an empty sandy world:

Scenario-main

Right-click (on Mac, Control-click whenever this tutorial says right-click) on the crab-class and select new Crab(), then left click on the world to place the crab:

Class-popup-crab-new-crab

After that, click Run. You might be hoping you could watch the Crab do an amazing dance around the screen. Unfortunately, it seems we have a lazy Crab! Let's open up the code and have a look; you can either double-click on the Crab class in the class browser, or you can right-click and select 'Open Editor':

Class-popup-crab-open-editor

What you'll see is the Java code for the Crab. You don't need to understand all of it just yet, but the important bit is the code between the curly brackets below the public void act() line -- there's nothing there: Edit-crab-act

A quick note about bracket spotting (as usual, wikipedia has a frighteningly comprehensive article). There are three main bracket types used in Java; let's see some blown-up images of them:

Bracket-round

These are round brackets, known in the USA as parentheses, but in the UK simply as brackets. In Java, they are used in mathematical expressions, and to surround the parameter lists for method calls (we'll get to those shortly).

Bracket-sq

These are square brackets, known in the USA simply as brackets (see where confusion can arise?). In Java, they are used for arrays.

Bracket-curly

These are curly brackets, also known as braces or squiggly brackets. In Java, these are used to surround blocks of code, such as methods or the contents of classes.

Anyway, back in the code, you can see that there's nothing between the curly brackets. If we want our crab to do anything, we're going to have to fill that in. Let's get it moving, by adding a move instruction to the code:

Edit-crab-act-narrow

You need to precisely match what is written there. It's the word move, followed by round brackets containing the number 4, followed by a semi-colon. Common mistakes include captialising the m (capitalisation matters in Java!), missing the semi-colon, using the wrong brackets (or thinking that empty round brackets are a zero), or accidentally deleting the curly brackets. If you get an error during this tutorial, look for one of these errors that you might have made when copying the code.

Once you've written this, hit the Compile button in the main Greenfoot interface (or at the top of the editor window) then place a crab in the world again and click Run. Now the crab should glide sideways across the screen. Then it should hit the edge of the world and abruptly stop. If you like, you can pause the scenario, drag the crab over to the left, hit Run and watch it again. Why not place a few more crabs in the world and watch them all do that? The crabs aren't actually stopping as such; they are still trying to move, but Greenfoot doesn't let them move out of the world (if they did, how would you drag them back in again?). You can vary the speed of the Crab by changing the number 4 in the code to a different number. Higher will be faster, lower will be slower -- see if you can guess what happens with a negative number.

Let's make the crab do a bit more than moving in a straight line. Go back to the code, and after the move line, add another line (but still inside the curly brackets for the act method) that says turn(3), like this:

Edit-crab-act-narrow

You'll see that the crab runs in a circle. Experiment with the turning amount to get the circle tighter or larger -- we'll leave it up to you to figure out which way you need, and why that is.

The good thing about the crab turning all the time is that even if it does hit the edge of the world, eventually it will turn enough that it moves back off the edge of the world and into the middle again. What would be even better is if we can control the crab's turning -- let's get some interaction in our scenario! We can make the crab turn when we press the left or right cursor (arrow) keys. Here's the code:

Edit-crab-act-narrow

We use the Greenfoot built-in methods for checking if a key is down. Between the quotes is the name of the key, "left" is the left cursor key, "right" is right. If you want something like "a" and "d", just use those instead! Our code is saying: if those keys are down, turn a certain number of degrees. Enter that code, compile it, and try it out for yourself. You can alter how fast the crab turns by increasing those numbers.

If you put multiple crabs in the world, you'll find that pressing the keys controls all of them at once in glorious synchronisation, making you into some sort of crab overlord: all of the crabs are executing the same code, so if you press the left key, all of them will see that the left key is down, and they will all turn accordingly.

What happens if you hold down left and right at the same time? Try it, and find out -- then look at the code and see if you can work out why that is. Then continue on to part 3 of the tutorial.