greenfoot introduction (2)

22
kakihijau.googlepages.com Introduction To Greenfoot Part-2

Upload: e-mulyana

Post on 21-Jan-2015

2.518 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Greenfoot Introduction (2)

kakihijau.googlepages.com

Introduction To

GreenfootPart-2

Page 2: Greenfoot Introduction (2)

2Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Disclaimer

This document is intended to give a beginner an introductory material for the Greenfoot system. Greenfoot is a software framework made by Poul Henriksen and Michael Koelling at University of Kent / Deakin University. More information can be found at http://www.greenfoot.org

This document is available „AS IS“ free of charge for personal use and non-commercial redistribution. It may not be sold for profit or included in other packages which are sold for profit without written authorisation. This document is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; your use of the document is at your sole risk. Reusing and modifying (part of) this document is allowed, as long as you state explicitly that your work is based on the original document produced by the copyright holder: Eueung Mulyana. The author can be contacted via eueung-[at]-yahoo.com (http://kakihijau.googlepages.com).

Page 3: Greenfoot Introduction (2)

3Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Purpose

This is the second part of the document „Introduction to Greenfoot“ and focuses on understanding the wombats scenario

Part of the content of these slides is based on the Greenfoot Tutorial made by Michael Koelling, publicly accesible at the Greenfoot website, http://www.greenfoot.org

Page 4: Greenfoot Introduction (2)

4Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Outline

Class Display Revisited Class Editor The Leaf Class The Wombat Class The WombatWorld Class

Page 5: Greenfoot Introduction (2)

5Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Class Display Revisited

Class display contains all classes which are used in a scenario Class World and Actor are abstract superclasses, part of the

Greenfoot system Scenario wombats has 2 actor classes (Wombat, Leaf)

Page 6: Greenfoot Introduction (2)

6Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Class Editor (1)

We can modify objects behaviour by editing the source code of the corresponding class

To display the source code editor:– Double-click the class– Right-click the class, choose the

item „Open editor“

Page 7: Greenfoot Introduction (2)

7Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Class Editor (2)

Page 8: Greenfoot Introduction (2)

8Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Leaf Class

The simplest class compared to the Wombat and WombatWorld class

Leaf objects do nothing! As you can see in the previous slide, the Leaf

class has no statement and consists of an empty constructor

Page 9: Greenfoot Introduction (2)

9Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (1)import necessary packages

class header;Wombat is a subclassfrom Actor

data (constants andvariables)

class constructor and methods

Page 10: Greenfoot Introduction (2)

10Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (2)

4 constants : EAST, WEST, NORTH, SOUTH

2 variables : direction, leavesEaten

1 constructor : Wombat() 8 methods :

– getLeavesEaten(), foundLeaf(), eatLeaf()

– setDirection(), turnLeft()– canMove(), move()– act()

Page 11: Greenfoot Introduction (2)

11Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (3)

constructor initialisingdirection and leavesEaten

this method is inherited fromthe superclass Actor

is there any Leaf objectin my position?

remove that Leaf objectupdate the variableleavesEaten

Page 12: Greenfoot Introduction (2)

12Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (4)

Page 13: Greenfoot Introduction (2)

13Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (5)

this method is inherited fromthe superclass Actor

Page 14: Greenfoot Introduction (2)

14Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (6)

change direction 90 degreesto the left

Page 15: Greenfoot Introduction (2)

15Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (7)

these methods areinherited from Actor

new coordinateif the object movesforward; depends on its direction

checks if the objectreaches the edges of the world

Page 16: Greenfoot Introduction (2)

16Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (8)

Page 17: Greenfoot Introduction (2)

17Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (9)

if object reaches one ofthe borders, do nothing!

setLocation() isinherited from Actor

the object movesforward; depends on its direction

Page 18: Greenfoot Introduction (2)

18Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The Wombat Class (10)

if you find leaves, eat !

if you don‘t find leaves, but you can move forward, then move!

if you don‘t find leaves and you also cannot move, then turn to the left!

Page 19: Greenfoot Introduction (2)

19Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The WombatWorld Class (1)

1 constructor : WombatWorld()

2 methods :– populate()– randomLeaves()

Page 20: Greenfoot Introduction (2)

20Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The WombatWorld Class (2)

calls the World constructor

sets the background.the method is inheritedfrom the superclass World

cell.jpg

Page 21: Greenfoot Introduction (2)

21Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The WombatWorld Class (3)

Page 22: Greenfoot Introduction (2)

22Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

The WombatWorld Class (4)

Create a Leaf object at a randomposition (x,y); repeat howmanytimes