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