

The first thing we shall do is to make the crab move.ģ Chapter 2! The first program: Little Crab Making the crab move Let us have a look at the source code of class Crab. In this chapter, we shall work on changing this. This is because there is no source code in the definition of the Crab class that specifies what the crab should do. The crab does not do anything when Greenfoot runs.

If you have just done the exercise above, then you know the answer to the What do you observe -question. We will talk a little more about the Actor and Animal classes later on. (It follows then, that a crab also is an actor.) Initially, we will work only with the Crab class. The hierarchy (denoted by the arrows) indicates an is-a relationship (also called inheritance): A crab is an animal, and an animal is an actor.

We notice that there is the usual Greenfoot Actor class, a class called Animal, and the Crab class. What do you observe? (Remember: If the class icons on the right appear striped, you have to compile the project first.) Figure 2-2: The Little Crab actor classes On the right, you see the classes in this scenario (Figure 2-2). Place a crab into the world and run the program (click the Run button). Exercise 2.1 Start Greenfoot and open the little-crab scenario. The scenario you see should look similar to Figure 2-1. You will find this scenario in the book projects on the CD of this book. Figure 2-1: The Little Crab scenarioĢ 34 Introduction to Programming with Greenfoot 2.1 The Little Crab scenario The scenario we use for this chapter is called little-crab. Now, we want to start to make our own game.
#GREENFOOT BOOK SCENARIOS HOW TO#
1 2 The first program: Little Crab topics: concepts: writing code: movement, turning, reacting to the screen edges source code, method call, parameter, sequence, if statement In the previous chapter, we discussed how to use existing Greenfoot scenarios: We have created objects, invoked methods, and played a game.
