51
Getting to know Greenfoot Mr. Gano

Getting to know Greenfoot Mr. Gano. Desktop Icon

Embed Size (px)

Citation preview

Page 1: Getting to know Greenfoot Mr. Gano. Desktop Icon

Getting to know Greenfoot

Mr. Gano

Page 3: Getting to know Greenfoot Mr. Gano. Desktop Icon

Greenfoot Programming Interface

• The world. The largest area covering most of the screen (a sand-colored grid in this case) is called the world. This is where the program will run and we will see things happen.

• The class diagram. The area on the right with the beige-colored boxes and arrows is the class diagram. We shall discuss this in more detail shortly.

• The execution controls. The Act, Run, and Reset buttons and the speed slider at the bottom are the execution controls. We’ll come back to them in a little while, too.

Page 4: Getting to know Greenfoot Mr. Gano. Desktop Icon

This is the Greenfoot interface

World

ClassDiagram

ExecutionControls

Page 5: Getting to know Greenfoot Mr. Gano. Desktop Icon

Objects and Classes

• We shall discuss the class diagram first. The class diagram shows us the classes involved in this scenario. In this case, they are World, WombatWorld, Actor, Leaf, and Wombat.

• We shall be using the Java programming language for our projects. Java is an object-oriented language. The concepts of classes and objects are fundamental in object orientation.

• Let us start by looking at the Wombat class. The class Wombat stands for the general concept of a wombat—it describes all wombats. Once we have a class in Greenfoot, we can create objects. from it. (Objects are also often referred to as instances in programming—the two terms are synonyms.)

Page 6: Getting to know Greenfoot Mr. Gano. Desktop Icon

Objects and Classes

• Once we have placed some objects into the world, we can interact with these objects by rightclicking them. This will pop up the object menu (Figure 1.4). The object menu shows us all the operations this specific object can perform. For example, a wombat’s object menu shows us what this wombat can do (plus two additional functions, Inspect and Remove, which we shall discuss later).

• In Java, these operations are called methods. It cannot hurt to get used to standard terminology straightaway, so we shall also call them methods from now on. We can invoke a method by selecting it from the menu.

Page 7: Getting to know Greenfoot Mr. Gano. Desktop Icon

Objects and Classes

Right Click on Wombat

Click New Wombat()Drag to World

This is how to make a wombat appear in the game world. A wombat is an object of the Actor class. The game world for the wombat is part of the world class.

Page 8: Getting to know Greenfoot Mr. Gano. Desktop Icon

Make three wombats and three leaves as shown here.

Page 9: Getting to know Greenfoot Mr. Gano. Desktop Icon

Interact with objects through the contextual menu by right clicking on the object to see what methods can be called.

Invoke the Move method

Right Click on the Wombat

Page 10: Getting to know Greenfoot Mr. Gano. Desktop Icon

Return Types

The word at the beginning is called the return type. It tells us what the method returns to us when we invoke it. The word void means “nothing” in this context: Methods with a void return type do not return any information. They just carry out their action, and then stop.

Page 11: Getting to know Greenfoot Mr. Gano. Desktop Icon

The return type of a method specifies what a method call will return. This example shows a return type of true, which is a boolean return type. In this case the methods is telling us that the wombat can move if we want it to move.

Page 12: Getting to know Greenfoot Mr. Gano. Desktop Icon

Here we see the Greenfoot interface with the method canMove() returning true. This means the wombat can move forward.

Page 13: Getting to know Greenfoot Mr. Gano. Desktop Icon

This example shows the canMove() method call returning false since the wombat cannot move since it is at the edge of the game world.

Page 14: Getting to know Greenfoot Mr. Gano. Desktop Icon

Here we have the getLeavesEaten() method returning 0 since the wombat has not eaten any leaves. When the wombat eats a leaf the int (integer) will increase to 1.

Page 15: Getting to know Greenfoot Mr. Gano. Desktop Icon

ParametersThe other bit in the method menu that we have not yet discussed are the parentheses after the

method name.• int getLeavesEaten()• void setDirection(int direction)

The parentheses after the method name hold the parameter list. This tells us whether the method requires any additional information to run, and if so, what kind of information.

If we see only a pair of parentheses without anything else within it (as we have in all methods so far), then the method has an empty parameter list. In other words, it expects no parameters—when we invoke the method it will just run. If there is anything within the parenthesis, then the method expects one or more parameters—additional information that we need to provide.

Page 16: Getting to know Greenfoot Mr. Gano. Desktop Icon

ParametersHere we are setting the direction in which the Wombat is facing.

Page 17: Getting to know Greenfoot Mr. Gano. Desktop Icon

Setting the Direction of the Wombat

Page 18: Getting to know Greenfoot Mr. Gano. Desktop Icon

Result of setting direction to 3

Page 19: Getting to know Greenfoot Mr. Gano. Desktop Icon

Numbers indicating direction of the Wombat as the computer understands directions

1

3

2

0

Page 20: Getting to know Greenfoot Mr. Gano. Desktop Icon

If we insert a value greater than 3 there is no effect since the parameter we enter must be between or including 0 – 3.

Parameter > 3 Does Nothing

Page 21: Getting to know Greenfoot Mr. Gano. Desktop Icon

Here is an example of a non integer value entered into the setDirection() method. The value 2.5 will not work.

Non-Integer Returns Error

Page 22: Getting to know Greenfoot Mr. Gano. Desktop Icon

Greenfoot Execution Activity

Place a wombat and a good number of leaves into the world, and then invoke a wombat’s act() method several times. What does this method do? How does it differ from the move method? Make sure to try different situations, for example, the wombat facing the edge of the world, or sitting on a leaf.

With a wombat and some leaves in the world, click the Act button in the execution controls near the bottom of the Greenfoot window. What does this do?.

Page 23: Getting to know Greenfoot Mr. Gano. Desktop Icon

Act() method

All objects in a Greenfoot world have this act method.

Invoking act is essentially giving the object the instruction “Do whatever you want to do now”.

If you tried it out for our wombat, you will have seen that the wombat’s act does something like

the following:• If we’re sitting on a leaf, eat the leaf.• Otherwise, if we can move forward, move forward.• Otherwise, turn left.

Page 24: Getting to know Greenfoot Mr. Gano. Desktop Icon

Wombat and leaves in the game world

Page 25: Getting to know Greenfoot Mr. Gano. Desktop Icon

When act is clicked the wombat will follow instructions included in the act() method.

Wombat Moves toward the Leaves

Page 26: Getting to know Greenfoot Mr. Gano. Desktop Icon

Wombat will move over the leaf when act() method is clicked again and again.

Wombat Moves to Leaves

Page 27: Getting to know Greenfoot Mr. Gano. Desktop Icon

Wombat will move over the leaf. Instructions in the act() method will enact the eat() method so the leaf will be ‘eaten’ or rather disappear.

Wombat Eats Leaf

Page 28: Getting to know Greenfoot Mr. Gano. Desktop Icon

Right click on a wombat and select the act() method to invoke the instructions contained within that method.

Page 29: Getting to know Greenfoot Mr. Gano. Desktop Icon

Click the Act button to inoke the act() method. How does the result differ from invoking the act() method by right clicking on a wombat?

The >Act Execution Control Affects All the Wombats

Page 30: Getting to know Greenfoot Mr. Gano. Desktop Icon

Click the Run button and see what happens to the wombat.

Wombat Runs Around the Edge of the World

Page 31: Getting to know Greenfoot Mr. Gano. Desktop Icon

Act() method for Wombat

• Act Method– If we’re sitting on a leaf, eat the leaf– Otherwise, if we can move forward, move forward– Otherwise, turn left

Page 32: Getting to know Greenfoot Mr. Gano. Desktop Icon

Asteroids Game Example

Page 33: Getting to know Greenfoot Mr. Gano. Desktop Icon

Understanding the Class Diagram

World Class is always there in Greenfoot scenarios, it is built-in. Space represents a specific world for this scenario

Arrows show relationships

Explosion and Mover are subclasses of Actor

Bullet, Rocket, and Asteroid are subclasses of Mover.

Vector is a helper class

Page 34: Getting to know Greenfoot Mr. Gano. Desktop Icon

Class DiagramLet us first have a closer look at the class diagram seen in the previous slide. At the top, you see the two classes called World and Space, connected by an arrow.

The World class is always there in all Greenfoot scenarios—it is built into Greenfoot. The class under it, Space in this case, represents the specific world for this particular scenario. Its name can be different in each scenario, but every scenario will have a specific world here.

The arrow shows an is-a relationship: Space is a World (in the sense of Greenfoot worlds: Space, here, is a specific Greenfoot world). We also sometimes say that Space is a subclass of World.

We do not usually need to create objects of world classes—Greenfoot does that for us. When we open a scenario, Greenfoot automatically creates an object of the world subclass. The object is then shown on the main part of the screen. (The big black image of space is an object of the Space class.)

Page 35: Getting to know Greenfoot Mr. Gano. Desktop Icon

Playing with AsteroidsStart Playing by Creating Some Actor Objects (Objects of the Subclass of Actor). Create Objects for Rocket, Bullet, Asteroid, and Explosion

Page 36: Getting to know Greenfoot Mr. Gano. Desktop Icon

Asteroids Reload Time Activity

If you have played this game for a while, you will have noticed thatyou cannot fire very quickly. Let us tweak our spaceship firing software a bit so that we can shoot a bit quicker. (That should make getting the asteroids a bit easier!) Place a rocket into the world, then invoke its setGunReloadTime method (through the object menu), and set the reload time to 5. Play again (with at least two asteroids) to try it out.

Page 37: Getting to know Greenfoot Mr. Gano. Desktop Icon

Right Click on the Object

Click on setGunReloadTime(int reloadTime)

Page 38: Getting to know Greenfoot Mr. Gano. Desktop Icon

Click on setGunReloadTime and Type 5

Page 39: Getting to know Greenfoot Mr. Gano. Desktop Icon

Right Click on the Object and Select Inspect

Page 40: Getting to know Greenfoot Mr. Gano. Desktop Icon

These are the parameters for the Rocket object

Page 41: Getting to know Greenfoot Mr. Gano. Desktop Icon

Right Click on the Object and Select getSpeed ( )

Page 42: Getting to know Greenfoot Mr. Gano. Desktop Icon

You can set the speed of the rocket by altering the int parameter in the rocket.getSpeed() method

Page 43: Getting to know Greenfoot Mr. Gano. Desktop Icon

Asteroids have an inherent stability. Each time they get hit by a bullet, their stability decreases. When it reaches zero, they break up. What is their initial stability value after you create them? By how much does the stability decrease from a single hit by a bullet?

Page 44: Getting to know Greenfoot Mr. Gano. Desktop Icon

Make a very big asteroid

Right Click on the Object and Select setSize(int Size) and Set the Size to 256

Page 45: Getting to know Greenfoot Mr. Gano. Desktop Icon

Source Code

Right Click on the Class and Select Open editor

Page 46: Getting to know Greenfoot Mr. Gano. Desktop Icon

Source Code for Rocket

Page 47: Getting to know Greenfoot Mr. Gano. Desktop Icon

Change gunReloadTime from 20 to 5

Page 48: Getting to know Greenfoot Mr. Gano. Desktop Icon
Page 49: Getting to know Greenfoot Mr. Gano. Desktop Icon

Make a change to the Rocket class source code that will allow the rocket to shoot quickly from the start of the game. Close the editor and compile the classes. Each time you make changes the code will need to be compiled again.

Class Changed Class Compiled

Page 50: Getting to know Greenfoot Mr. Gano. Desktop Icon

Summary

We have seen what Greenfoot scenarios can look like and how to interact with them. We have seen how to create objects and how to communicate with these objects by invoking their methods. Some methods are commands to objects, while other methods return information about the object. Parameters are used to provide additional information to methods, while return values pass information back to the caller.

Page 51: Getting to know Greenfoot Mr. Gano. Desktop Icon

• Greenfoot scenarios consist of a set of classes.• Many objects can be created from a class.• Objects have methods. Invoking these performs an action.• The return type of a method specifies what a method call will return.• A method with a void return type does not return a value.• Methods with void return types represent commands; methods with non-void return

types represent questions.• A parameter is a mechanism to pass in additional data to a method.• Parameters and return values have types. Examples of types are int for numbers,

and boolean for true/false values.• The specification of a method, which shows its return type, name, and parameters,

is called its signature.• Objects that can be placed into the world are known as actors.• A subclass is a class that represents a specialization of another. In Greenfoot, this is

shown with an arrow in the class diagram. • Every class is defined by source code. This code defines what objects of this class

can do. We can look at the source code by opening the class’s editor. • Computers do not understand source code. It needs to be translated to machine code

before it can be executed. This is called compilation.