Greenfoot Greenfoot AsteroidsAsteroidsMrs. C. FurmanMrs. C. Furman
August 16, 2010August 16, 2010
Open AsteroidsOpen Asteroids
Open Greenfoot.Open Greenfoot. Open Scenario, “asteroids1” in the Open Scenario, “asteroids1” in the
chapter01 folder in book scenarios.chapter01 folder in book scenarios.
Class DiagramClass Diagram In Greenfoot, you In Greenfoot, you
always have a World always have a World class. class.
Space is a specific Space is a specific class made for this class made for this scenarioscenario
World Classes
InheritanceInheritance
In greenfoot you always a more specific In greenfoot you always a more specific class under World. class under World.
The arrow between the class represents The arrow between the class represents an an is-ais-a relationship. Space is-a World. relationship. Space is-a World.
Space is a Space is a subclasssubclass of World of World..
More on WorldMore on World
When we open a scenario, greenfoot When we open a scenario, greenfoot automatically creates an object of the automatically creates an object of the World subclass, in this case Space. That World subclass, in this case Space. That is why the black screen automatically is why the black screen automatically pops up.pops up.
Actor ClassesActor Classes
Under the World classes is Under the World classes is the set of Actor classes. the set of Actor classes.
How many subclasses of How many subclasses of Mover are there? Mover are there?
What are the subclasses of What are the subclasses of Actor?Actor?
SubclassSubclass Bullet, Rocket, and Asteroid are Bullet, Rocket, and Asteroid are
subclasses of Moversubclasses of Mover Explosion and Mover are Explosion and Mover are
subclasses of Actorsubclasses of Actor While Bullet, Rocket and While Bullet, Rocket and
Asteroid are all subclasses of Asteroid are all subclasses of Mover as well as Actor, but are Mover as well as Actor, but are NOT subclasses of ExplosionNOT subclasses of Explosion
Subclass and SuperclassSubclass and Superclass Superclasses – these classes Superclasses – these classes
are less specific are less specific Subclasses – these classes Subclasses – these classes
inheritinherit the traits of the the traits of the superclass, and then expands superclass, and then expands on them. on them.
Subclasses are BIGGER than Subclasses are BIGGER than Superclasses.Superclasses.
Other ClassesOther Classes
Vector – helper class Vector – helper class We can not place objects of We can not place objects of
it into the World.it into the World.
Playing with AsteroidsPlaying with Asteroids We can only create objects of classes We can only create objects of classes
that do not have subclasses. that do not have subclasses. Which classes can we create objects for?Which classes can we create objects for? Place objects into the World, then click Place objects into the World, then click
run. Use the arrow keys to move your run. Use the arrow keys to move your spaceship and space bar to fire shots.spaceship and space bar to fire shots.
Exercise 1Exercise 1 After you’ve played the game for a little After you’ve played the game for a little
bit. Lets tweak it!bit. Lets tweak it! Invoke the setGunReloadTime method Invoke the setGunReloadTime method
and set the reload time to 5. Then play and set the reload time to 5. Then play again.again.
Exercise 2Exercise 2
Stop the execution by pressing pause.Stop the execution by pressing pause. Find out how many shots you have fired. Find out how many shots you have fired. Invoke a Rocket method.Invoke a Rocket method.
Exercise 3Exercise 3
You will notice that the rocket moves a bit You will notice that the rocket moves a bit as soon as you place it into the World. as soon as you place it into the World.
What is its initial speed?What is its initial speed?
Exercise 4Exercise 4 Asteroids have an inherent stability. Each Asteroids have an inherent stability. Each
time they get hit by a bullet, their stability time they get hit by a bullet, their stability decreases. When it reaches zero, they decreases. When it reaches zero, they break up. break up.
What is their initial stability value after you What is their initial stability value after you create them? create them?
Exercise 4BExercise 4B
By how much does the stability decrease By how much does the stability decrease from a single hit by a bullet?from a single hit by a bullet?
Just shoot an asteroid once, and then Just shoot an asteroid once, and then check the stability again.check the stability again.
ConstructorsConstructors Asteroids have 2 constructor methodsAsteroids have 2 constructor methods Constructors are used to set up the object. Constructors are used to set up the object. new Asteroid() – called a default new Asteroid() – called a default
constructor. Sets it to a default sizeconstructor. Sets it to a default size new Asteroid(int size) – allows us to set the new Asteroid(int size) – allows us to set the
size of the Asteroid by passing an integer.size of the Asteroid by passing an integer.
Exercise 5Exercise 5
Make a very big asteroid. Make a very big asteroid. Make a very small asteroid. Make a very small asteroid.
VariablesVariables
In Java, we often want to store values. In Java, we often want to store values. We see this in our gun speed. We used We see this in our gun speed. We used
setGunReloadTime and passed a value setGunReloadTime and passed a value in order to be saved by the variable in order to be saved by the variable speed.speed.
These values are stored in variables.These values are stored in variables.
Variable names… Variable names… IdentifiersIdentifiers
When we name a variable we need to use a When we name a variable we need to use a “proper identifier”. Here are the rules for “proper identifier”. Here are the rules for proper identifiers:proper identifiers: Must start with a letter. As a convention, it starts Must start with a letter. As a convention, it starts
with a lowercase letterwith a lowercase letter Must contain only numbers, letter or underscoresMust contain only numbers, letter or underscores Must not be a keyword. Like new.Must not be a keyword. Like new.
Java is Case SensitiveJava is Case Sensitive
Java is what we call Java is what we call case sensitivecase sensitive. . Which means that it distinguishes Which means that it distinguishes between upper and lower case letters between upper and lower case letters differently.differently.
Assignment StatementsAssignment Statements We can assign values to our variables We can assign values to our variables
with assignment statements.with assignment statements. Variable has to be on the left, value you Variable has to be on the left, value you
are giving to the variable is on the right.are giving to the variable is on the right.
variable = 8;variable = 8;
Variable Name Value that is beinggiven to the variable
Assignment Operator
Source CodeSource Code Behavior of each Object is defined by its Behavior of each Object is defined by its
class. class. Source code – the code that specifies all the Source code – the code that specifies all the
details about a class and its objects. details about a class and its objects. Select Select Open editorOpen editor for the Rocket class, or for the Rocket class, or
double click on the Rocket class to open the double click on the Rocket class to open the source code.source code.
Rocket Source CodeRocket Source Code We can We can
change the change the reload speed reload speed on all on all Rockets in Rockets in the source the source code.code. Change getReloadTime
to 5. Recompile.