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

Slides:



Advertisements
Similar presentations
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package Download greenfoot and install.
Advertisements

Getting to know Greenfoot
Games and Simulations O-O Programming in Java The Walker School
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
of Kent A practical introduction to Greenfoot David Barnes Lecturer in Computer Science University of.
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Greenfoot Asteroids Mrs. C. Furman August 16, 2010.
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
Chapter 1 - Getting to know Greenfoot Bruce Chittenden.
Chapter 1 - Getting to know Greenfoot Acknowledgement: Michael Kolling & Bruce Chittenden.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
Test review. When? Monday 9/27 in class Know Greenfoot How many classes? Top-most classes? What does each button do? Lowest child class?
Games and Simulations O-O Programming in Java The Walker School
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Chapter 3 Vector Class. Agenda Design and Implementation of Vector class – add, get, set remove, copy, equals, ensureCapacity Hangman using Vector class.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Kinect in Greenfoot.
Chapter 1 - Getting to know Greenfoot
Chapter 6 – Interacting Objects: Newton’s Lab. topics: objects interacting with each other, using helper classes, using classes from the Java library.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
GridWorld Case Study Barbara Ericson March 24, 2007.
Greenfoot Game Programming Club.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Inheritance and Access Control CS 162 (Summer 2009)
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Kakihijau.googlepages.com Introduction To Greenfoot Part-1.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Kakihijau.googlepages.com Introduction To Greenfoot Part-2.
Introduction To Greenfoot
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Improving the Crab Mrs. C. Furman August 19, 2010.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 22 Game Graphics.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
PROGRAMMING INFORMATION. PROCEDURES IN PYTHON Procedures can make code shorter, simpler, and easier to write. If you wanted, for example, to use a certain.
Kakihijau.googlepages.com Introduction To Greenfoot Part-3.
Newton’s Lab Games and Simulations O-O Programming in Java.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Chapter 4 - Finishing the Crab Game
Chapter 4 - Finishing the Crab Game
Chapter 3 – Improving the Crab Game
Introduction To Greenfoot
Introduction to Object-oriented Program Design
Creating Games with Greenfoot
Version 2.
The Little Crab Scenario
Problem Solving with Data Structures using Java: A Multimedia Approach
Games and Simulations O-O Programming in Java The Walker School
Methods, Data and Data Types
Greenfoot November 8, 2009.
© A+ Computer Science - GridWorld © A+ Computer Science -
More on Creating Classes
WJEC GCSE Computer Science
PACMAN OO Style.
Presentation transcript:

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

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

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

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

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

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

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

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!

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

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

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

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

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

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

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

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

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

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)

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

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.

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

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!

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

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

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

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

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!)

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