Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java.

Similar presentations


Presentation on theme: "Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java."— Presentation transcript:

1 Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java

2 Lecture 4. Greenfoot 2 Pablo Romero, Department of Informatics Lecture plan Greenfoot in action The Wombats scenario Modifying the code * Greenfoot Designed and implemented at the University of Kent and Deakin University. Copyright held by Poul Henriksen and Michael Koelling. Slides based on those by Eueung Mulyana

3 Lecture 4. Greenfoot 3 Pablo Romero, Department of Informatics Greenfoot

4 Lecture 4. Greenfoot 4 Pablo Romero, Department of Informatics Greenfoot

5 Lecture 4. Greenfoot 5 Pablo Romero, Department of Informatics Looking at the classes

6 Lecture 4. Greenfoot 6 Pablo Romero, Department of Informatics The Wombat class import necessary packages class header; Wombat is a subclass from Actor data (constants and variables) class constructor and methods

7 Lecture 4. Greenfoot 7 Pablo Romero, Department of Informatics Constructor and leaf methods constructor  initialising direction and leavesEaten this method is inherited from the superclass Actor is there any Leaf object in my position? remove that Leaf object update the variable leavesEaten

8 Lecture 4. Greenfoot 8 Pablo Romero, Department of Informatics The act method 8 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!

9 Lecture 4. Greenfoot 9 Pablo Romero, Department of Informatics The turnLeft method 9 change direction 90 degrees to the left

10 Lecture 4. Greenfoot 10 Pablo Romero, Department of Informatics The setDirection method this method is inherited from the superclass Actor

11 Lecture 4. Greenfoot 11 Pablo Romero, Department of Informatics The move method 11 if object reaches one of the borders, do nothing! setLocation() is inherited from Actor the object moves forward; depends on its direction

12 Lecture 4. Greenfoot 12 Pablo Romero, Department of Informatics The canMove method 12

13 Lecture 4. Greenfoot 13 Pablo Romero, Department of Informatics The canMove method 13 these methods are inherited from Actor new coordinate if the object moves forward; depends on its direction checks if the object reaches the edges of the world

14 Lecture 4. Greenfoot 14 Pablo Romero, Department of Informatics The WombatWorld class Constructor populate randomLeaves

15 Lecture 4. Greenfoot 15 Pablo Romero, Department of Informatics The WombatWorld constructor calls the World constructor sets the background. the method is inherited from the superclass World cell.jpg

16 Lecture 4. Greenfoot 16 Pablo Romero, Department of Informatics The populate method

17 Lecture 4. Greenfoot 17 Pablo Romero, Department of Informatics The randomLeaves method Create a Leaf object at a random position (x,y); repeat howMany times

18 Lecture 4. Greenfoot 18 Pablo Romero, Department of Informatics So far Greenfoot environment to program simulations and games in java World and Actor are system classes The wombats program Leaf Wombat (getLeavesEaten, foundLeaf, eatLeaf, setDirection, turnLeft, canMove, move, act) WombatWorld (populate, randomLeaves)

19 Lecture 4. Greenfoot 19 Pablo Romero, Department of Informatics Exercise How would you change the behaviour of Wombat objects so that instead of always turning left, they could turn to other directions in a random way? GreenFoot. getRandomNumber(int limit) Generates a random number between 0 and limit

20 Lecture 4. Greenfoot 20 Pablo Romero, Department of Informatics Exercise Create a new method, turnRandom(), that will turn to the left a random number of times. This method will replace the method turnLeft() in the act() method of Wombats. Use the method of the greenfoot library GreenFoot. getRandomNumber(int limit) that produces a random integer between zero and limit-1.

21 Lecture 4. Greenfoot 21 Pablo Romero, Department of Informatics The method turnRandom Generate a random number between 0 and 3 call turnLeft() turns times

22 Lecture 4. Greenfoot 22 Pablo Romero, Department of Informatics The updated method act 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!

23 Lecture 4. Greenfoot 23 Pablo Romero, Department of Informatics Before and after

24 Lecture 4. Greenfoot 24 Pablo Romero, Department of Informatics to set the image of an object programmatically

25 Lecture 4. Greenfoot 25 Pablo Romero, Department of Informatics Before and after

26 Lecture 4. Greenfoot 26 Pablo Romero, Department of Informatics Adding Rocks class image for the Rock class images available in the wombats scenario folder New classes can be added easily Can import images for classes However wombats can go through rocks

27 Lecture 4. Greenfoot 27 Pablo Romero, Department of Informatics check all Rock objects in front of you; if there is no Rock object, return true (i.e. okay it‘s clear!), otherwise return false (i.e. you cannot move!)

28 Lecture 4. Greenfoot 28 Pablo Romero, Department of Informatics Summary Greenfoot environment to program simulations and games (greenfoot.org) Provides an action loop World subclasses set up the world Actor subclasses define the characters of the game / simulation Sophisticated behaviour can be implemented in Java


Download ppt "Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java."

Similar presentations


Ads by Google