greenfoot introduction (3)

17
kakihijau.googlepages.com Introduction To Greenfoot Part-3

Upload: e-mulyana

Post on 21-Jan-2015

2.270 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Greenfoot Introduction (3)

kakihijau.googlepages.com

Introduction To

GreenfootPart-3

Page 2: Greenfoot Introduction (3)

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 (3)

3Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Purpose

This is the second part of the document „Introduction to Greenfoot“ and focuses on two topics: modification and programming of 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 (3)

4Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Outline

Changing the Behaviour Changing Representations Creating New Classes

Page 5: Greenfoot Introduction (3)

5Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing the Behaviour (1)

We want to change the behaviour of Wombat objects : instead of always turning left, they shall be able to turn to other directions in a random way

We will add an additional method, called turnRandom()

Open the source code of the Wombat class, add the following codes

call turnLeft()turns times

Generate a randomnumber between 0 and 3

Page 6: Greenfoot Introduction (3)

6Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing the Behaviour (2)

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 a random direction!

Page 7: Greenfoot Introduction (3)

7Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing the Behaviour (3)

Page 8: Greenfoot Introduction (3)

8Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing Representations (1)

Visual representation (image) of an object can be changed in two different ways– Changing the „class image“; in this way, all objects of

the same class shall look the same (to the assigned class image)

– Changing the image programmatically; in this way, objects of the same class might look different, depending on the implementation

These two ways might be combined!

Page 9: Greenfoot Introduction (3)

9Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing Representations (2)

Page 10: Greenfoot Introduction (3)

10Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing Representations (3)

Page 11: Greenfoot Introduction (3)

11Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing Representations (4)

the result of populate()after changin the class image

Additionally change the class imagethen add 2 objects after modification

Page 12: Greenfoot Introduction (3)

12Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing Representations (5)

to set the image of an objectprogrammatically

Page 13: Greenfoot Introduction (3)

13Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Changing Representations (6)

Page 14: Greenfoot Introduction (3)

14Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Creating New Classes (1)

We could not only modify the behaviour of objects, but also add a completely new type of objects i.e. create new classes

Right-click the class Actor and choose item „New subclass ...“, enter the class name and select a class image in the dialog; Greenfoot will automatically generate your class !

In this case we add a new class, called Rock. The image for this class is already available in the wombats scenario folder

class image forthe Rock class

images available in the wombats

scenario folder

Page 15: Greenfoot Introduction (3)

15Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Creating New Classes (2)

Page 16: Greenfoot Introduction (3)

16Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Creating New Classes (3)

check all Rock objectsin front of you;if there is no Rock object,return true (i.e. okay it‘sclear!) , otherwise return false (i.e. you cannot move!)

To stop wombats from walking through rocks ...

Page 17: Greenfoot Introduction (3)

17Introduction to Greenfoot

Eueung Mulyana | kakihijau.googlepages.com

Creating New Classes (4)