greenfoot november 8, 2009. reference open the greenfoot tutorial located on the lab computers at...

21
Greenfoot November 8, 2009

Upload: pierce-barber

Post on 01-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Greenfoot

November 8, 2009

Page 2: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Reference

• Open the Greenfoot Tutorial located on the lab computers at

Applications/Greenfoot 1.5.1/tutorial/tutorial.html

• You can download Greenfoot to your personal computer from the Greenfoot website. Java™ is a prerequisite for Greenfoot.

Page 3: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

What is Greenfoot? Who developed it?

• “Greenfoot is a software tool designed to let beginners get experience with object-oriented programming. It supports development of graphical applications in the Java™ Programming Language.”

• “Greenfoot was designed and implemented at the University of Kent, England, and Deakin University, Melbourne, Australia.”

Page 4: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

First Look

• Open Greenfoot (found in the Applications folder)

• If it doesn’t load with the Wombat scenario, open it by selecting Scenario/Open/Wombats.

• You should see something similar to what is displayed as a background to this slide.

Page 5: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Screen Layout

• Large grid = “ the world

• Class display– “World” and “Actor” classes part of

Greenfoot – Other classes belong to the wombat

scenario.

• Execution Controls ( ‘Act’, ‘Run’, slider).

Page 6: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

The [wombat] World Turns

• Place objects into the world– Control-click the Wombat class, select ‘New

Wombat()’. Then click anywhere in the world. You have just created a wombat object and placed it into the world.

– Use the same process to add several leaves– Shortcut: Make sure the Leaf class is selected,

then hold down the Shift key and click into the world several times.

Page 7: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

The [wombat] World Turns

• Make objects act– Click the ‘Act’ button in the execution controls.

Each object now acts – that is: each object does whatever it wants to do.

• Run a scenario– Click the ‘Run’ button. This is equivalent to clicking

the Act button over and over again, very quickly. You will notice that the Run button changes to a ‘Pause’ button. Clicking it stops the whole show.

Page 8: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Invoke Single Methods

• Make sure you have a wombat in the world, and the scenario is not running. Then right-click on the wombat, and you see that objects in the world also have a pop-up menu (Figure 2). – Try turnLeft(). – Try getLeavesEaten().– Try act().

Page 9: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Invoking wombatWorld Methods

• right-click the WombatWorld class and select ‘new WombatWorld()’. What happens? What type of function is WombatWorld()? How did you know that?

• Try populate().

• Try randomLeaves(int howMany).

Page 10: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Changing the Wombat Class

• Add the following method to the Wombat class:

public void turnRandom() { // get a random number between 0 and 3... int turns = Greenfoot.getRandomNumber(4); // ...and turn left that many times. for(int i=0; i<turns; i++) { turnLeft(); } }

Page 11: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Change act()

public void act() { if(foundLeaf()) { eatLeaf(); } else if(canMove()) { move(); } else { turnLeft();

// change turnLeft() to turnRandom(); } }

Page 12: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Compile

• Before you can execute your class changes you must compile your project. – Compile within the editor, or– Compile from Greenfoot’s main window.

• Once you have successfully compiled, you can create objects again.– Compilation (if successful) automatically

instantiates a world object.

Page 13: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Changing Images

• 2 ways to change the image of objects– You can change the image of a class, which will change the

default image for all objects of that class. • Select ‘Set Image...’ from the class’s pop-up menu.• Try this - change the leaf’s image to something else

– An object may change its image programmatically, which will change only the individual object. Each object can change its image as often as it likes.

• calling the ‘setImage’ method inherited from Actor. – one version of setImage() expects a parameter of type GreenfootImage

– one version of setImage() takes the name of a file (and then reads that image file into a GreenfootImage and sets it).

Page 14: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Change to Left-facing Wombat When the Wombat Moves Left• Conveniently, the ‘wombats’ project contains

a file named ‘wombat-left.gif’ in its ‘images’ sub-folder.

• To change the wombat image use the following method call.

setImage("wombat-left.gif");• Edit the Wombat’s setDirection(int

Direction) method to display the correct image when he is moving left.

Page 15: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Code

/* if you want to use the left facing image for upwards movement add

case NORTH :

setImage("wombat-left.gif");

setRotation(90);

break;

*/

case WEST :

setImage("wombat-left.gif");

setRotation(0);

break;

Page 16: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Documentation

• To make changes to object behavior, you often need to make use of some standard Greenfoot classes. Greenfoot provides four important classes that you should know about: 1. World2. Actor3. GreenfootImage 4. Greenfoot.

• Greenfoot’s online documentation: http://www.greenfoot.org/doc/

Page 17: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Inspecting an Object

• Invoke the ‘Inspect’ function from the menu of any wombat in the world.

• Populate the world and then inspect one of the wombats. What do you see?– some fields defined in the Wombat class (such as

‘leavesEaten’)– some fields that are not defined in Wombat. The

additional fields (such as x, y and rotation) are inherited from Actor and are present in all Greenfoot objects.

• If a value is shown as an arrow symbol, then the field contains a reference to another object, which can be inspected in turn (by selecting and clicking ‘Inspect’).

Page 18: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Create a New Class

• Create a new actor class. Choose ‘New subclass’ from the pop-up menu of Actor. This will create a new class as a subclass of Actor. When prompted for a class name, type ‘Rock’. You are also prompted to select an image for the class.

• Select the image, click Ok, and a new class named Rock is created.

• Open the editor for that class. Why don’t we have to write code for the rock class?

Page 19: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Create a New Class

• Answer: Because rocks don’t act.• Close the editor, compile and test (create an object) –

there are your rocks!• Now populate the scenario a bit and test the

wombats. (You can use the world’s populate() and randomLeaves() methods, and then add some rocks by hand.)

• You will notice that you have rocks, but the wombats still run through them. Can you modify the Wombat class so that they don’t run through the rocks?

Page 20: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

In Class Activity

• Add a Kangaroo class to the scenario. (In case you weren’t taught kangaroo behavior, a kangaroo will jump over at least one grid square with each move but, like a Wombat, will eat a leaf on the square on which she lands.)

• Modify wombatWorld’s populate() to initialize the world with 2 rocks, 1 kangaroo, 1 wombat and 7 leaves to the wombatWorld.

Page 21: Greenfoot November 8, 2009. Reference Open the Greenfoot Tutorial located on the lab computers at Applications/Greenfoot 1.5.1/tutorial/tutorial.html

Final Request

• So that the wombat scenario reverts to its original state, if you modified the original, please reboot the computer.

• Thank you, and remember that Project 1 is due 11/10!