lesson&2:&reading&aprogram&scaiano.com/edu/lesson...
TRANSCRIPT
![Page 1: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/1.jpg)
Lesson 2: Reading a program
![Page 2: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/2.jpg)
Remember: from yesterday We learned about…
• Precise language is needed to program • Actors and Classes • Methods – step by step instruc@ons • If statements • Adding sounds • Variables
![Page 3: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/3.jpg)
Let’s learn to read the program
• Open WombatWorld
• Open the code for WombatWorld
![Page 4: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/4.jpg)
Understanding WombatWorld
• Import ??? – means I am going to use these classes
Import command
Class to import * Means important all classes found here!
![Page 5: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/5.jpg)
Understanding WombatWorld
• Import ??? – means I am going to use these classes
No@ce the semi-‐colon! Almost all commands end in a semi-‐colon A few do not. This can be one of the most difficult things for new programmers.
![Page 6: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/6.jpg)
Understanding WombatWorld
• Comments – do not affect the program or running of a program – They are for the programmer, not the computer
• Comments are either start with // and got un@l the end of the line
• Comments are everything between /* comment goes here **** cow goes moo */
![Page 7: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/7.jpg)
Understanding WombatWorld
• Comments – do not affect the program or running of a program – They are for the programmer, not the computer
• Comments are either start with
• Comments are everything between
![Page 8: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/8.jpg)
Understanding WombatWorld
• Defining a class public class WombatWorld extends World
• This says I am defining a public class called “WombatWord”. It is a sub-‐type of World, just like a human is a sub-‐type of animal.
• public – means anyone/class can see and use this private – means no one can see or use this
• World – is a class defined elsewhere in greenfoot. We imported it with import greenfoot.*;
![Page 9: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/9.jpg)
Understanding WombatWorld
• PreYy colors in greenfoot mean something
• Words in red and colored are special words in Java. – Eg. import, public, class, extends, void – They cannot be used as names
![Page 10: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/10.jpg)
Understanding WombatWorld
• PreYy colors in greenfoot mean something • { and } (braces) are used to group code together in blocks.
• The colors match with braces
![Page 11: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/11.jpg)
Understanding WombatWorld
Everything in green is inside the class
![Page 12: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/12.jpg)
Understanding WombatWorld
Everything in yellow is inside the method or command
Random leaves command
![Page 13: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/13.jpg)
Review • import – a class you want to use
• public class WombatWorld extends World – Defining a new public class that is a type of world
• { and } (braces)-‐ blocks of code (colored)
• // and got un@l the end of the line /* more comments */ – Comments for the programmer not the computer
![Page 14: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/14.jpg)
Objects
• Objects are par@cular instances of a class – Your pet dog, my pet cat, me, you
• Objects represent “real” concrete things.
• Because an object is “real” thing, you can actually change it and command it to things.
• New Objects must be created using new
![Page 15: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/15.jpg)
Objects
• The “actors” or things in a Greenfoot game are objects, but each must be of a previously defined class.
• Let’s take a look.
![Page 16: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/16.jpg)
Understanding WombatWorld.populate()
• Open WombatWorld
• Find the populate method public void populate( )
Who can see this method
What this type Method returns
Name of method
arguments
![Page 17: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/17.jpg)
Understanding WombatWorld.populate()
• Different example: (not from Wombat World)
• Find the defini@on of a word public Definition lookupDefinition( String Word )
Who can see this method
What this type Method returns
Name of method
Arguments: What word to look up
![Page 18: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/18.jpg)
Crea@ng new objects
• To create a new object first create a variable to store the object in
• Then we request a new object or actor be created
• Set things about the object • Then do something with the object
![Page 19: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/19.jpg)
In populate()
• We create the Wombat
• We create the leaves
• We add them to the game
![Page 20: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/20.jpg)
Let’s move forward
• How about we create a really big world?
• The world will be bigger than what we can see on screen – We will only see a small part of the map
• We will use code from ScrollWorld: – hYp://www.greenfoot.org/scenarios/5806
![Page 21: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/21.jpg)
Let’s move forward
• With this we can make… – Overhead bomber games – Debugging – finding and fixing mistakes – Star@ng from one else’s project – Prac@cing what we know – Reading documenta@on
![Page 22: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/22.jpg)
Let’s start by creating a world
� Open ScrollWorld Project
� We will create a big map
� We will then populate the world with different objects and tasks
![Page 23: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/23.jpg)
ScrollWorld � Open ScrollWorld
� Notice DemoWorld is a subtype of ScrollWorld
� Open the code for DemoWorld
� Let’s look and see
![Page 24: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/24.jpg)
DemoWorld
![Page 25: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/25.jpg)
DemoWorld Name of our World Parent (or super type)
![Page 26: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/26.jpg)
DemoWorld Screen Size in cells Width , Height
Map Size in cells Width , Height
![Page 27: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/27.jpg)
DemoWorld How many pixels in a cell
![Page 28: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/28.jpg)
Big moving world Let’s make the world really tall, but just as wide as the screen. Change full width and full height values to 600 and 10000
![Page 29: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/29.jpg)
Big moving world Let’s move the Actors around
We don’t need to move the bug because he follows the camera try setting the Apple to 700, 9700 move the other Actors
Let’s set the camera position and direction
Let’s put the camera at the bottom (600, 9800) type “this” then hit CTRL+SPACE for a list of methods use setCameraLocation
use setCameraDirection and set the direction to -90 (UP) NOTE: 0 is right, 180 is left, 90 is down
![Page 30: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/30.jpg)
Big moving world Now let’s make the world move. Create a new method call act()
It should look like this public void act() { }
Inside it call moveCamera( 5 );
![Page 31: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/31.jpg)
Big moving world Your program should look like this. Compile and run it.
![Page 32: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/32.jpg)
Let’s make hitting rocks bad
� When a bug hits a rock, let’s end the game.
� Open Bug
� We will first add steering � then crashing
� Remove the body of the act method public void act() { }
� Let’s add some simple movement
![Page 33: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/33.jpg)
Steering
� Make and if statement and test � Greenfoot.isKeyDown(“left”)
� We don’t want “move” the bug, we want to slide it left or right � For right setLocation( getX()+ 3, getY() );
� For left setLocation( getX()- 3, getY() );
� Try it out you should be able to move left and right
![Page 34: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/34.jpg)
If, Condi@ons, and Comparisons
• Remember how if statements need true or false (boolean) values
• We can compare values – The result of a comparison is true or false
• Are two values equal == • Are two values different != • We also have greater than > and less than <
![Page 35: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/35.jpg)
If, Condi@ons, and Comparisons
• How do we say is X greater than 5 X > 5 OR 5 < X
• How about is X equal to 7 X == 7
• How is variable professor equal to nothing? professor == null OR null == professor
• Professor is equal to something (not null) professor != null OR null != professor
![Page 36: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/36.jpg)
We can even combine values
• Is X greater than 5 but less than 10 X > 5 && X < 10 Both parts must be true
• Is it true for X = 3 • Is it true for X = 7 • Is it true for X = 5
![Page 37: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/37.jpg)
We can even combine values
• Is X greater than 5 OR less than 10 X > 5 || X < 10 at least one part must be trye
• Is it true for X = 3 • Is it true for X = 7 • Is it true for X = 5 • Is it true for X = 13
![Page 38: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/38.jpg)
We can even combine values
1 2 3 4 5 6 7 8 9 10
X > 5
X < 10
X > 5 && X < 10
![Page 39: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/39.jpg)
We can even combine values
1 2 3 4 5 6 7 8 9 10
X > 5
X < 10
X > 5 || X < 10
![Page 40: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/40.jpg)
Comparing Objects/Reference
• Objects and references are hard to compare and understand
• Comparison between Object checks if they are the same reference, and NOT that they are the same
![Page 41: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/41.jpg)
Comparing Objects/Reference
Person mar@n = new Person( “Mar@n” ); Person other = new Person( “Mar@n” ); What will martin == other return true or false
FALSE – because they are different referenced (made by different new opera@ons)
![Page 42: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/42.jpg)
Making hitting rocks bad � After moving left or right, we will add a test to see if
we hit a rock
� Make an if statement � the test will use the method
getOneIntersectingObject( Rock.class ) � It returns a rock if the bug is intersecting one
� Otherwise it returns Nothing (null)
� Compare the returns object with null
![Page 43: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/43.jpg)
Making hitting rocks bad � It should look like this
OR
OR
![Page 44: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/44.jpg)
Making hitting rocks bad � End the game by calling
Greenfoot.stop();
� Add a sound if you would like
� Let’s add some text!
![Page 45: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/45.jpg)
Saying Game Over � Go back to the project
� Right click on Actor and select New subclass…
� Call it Text
![Page 46: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/46.jpg)
Saying Game Over � Let me explain this, then type it out
![Page 47: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/47.jpg)
Saying Game Over � Go back to Bug, just before calling
Greenfoot.stop();
� Create a new Text object � Text text = new Text( “Game Over”, 200, 50, 36 );
� Add the object to the center of the screen � addObject( text, 300, 200 );
� Try it in the game; drive into a rock.
![Page 48: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/48.jpg)
Let’s make hitting rocks bad
� When a bug hits a rock, let’s end the game.
� Open Bug
� We will first add steering � then crashing
� Remove the body of the act method public void act() { }
� Let’s add some simple movement
![Page 49: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/49.jpg)
Making other thing move
� Open the Apple or the Mushroom
� Add an act method public void act() { }
� Let’s try simple back and forth � We will use move again � How will we detect when we reach an edge?
� getX() � Where are the edges?
� 0 and the width – getWorld().getFullWidth() � Then what?
� Flip the direction – setRotation( … );
![Page 50: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/50.jpg)
Making other thing move
� Your code should look something like this
![Page 51: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/51.jpg)
Firing bullets � When the player presses space bar, we will fire
bullets
� The bullet will move forward
� If the bullet hits something it can destroy, then both should be destroyed.
� First create a bullet class a sub type of scroll actor!
![Page 52: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/52.jpg)
Firing bullets � When the player presses space bar, we will fire
bullets
� The bullet will move forward
� If the bullet hits something it can destroy, then both should be destroyed.
� First create a bullet class a sub type of scroll actor! � Right click on ScrollActor select “new subclass…”
![Page 53: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/53.jpg)
Firing a bullet � Now open actor
� When the player presses space � Create a new bullet � Add it to the world
� At the same location in the world as the Bug
![Page 54: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/54.jpg)
Firing a bullet � Now run it
� Wait why are the bullet behind me? � They aren’t going anywhere
![Page 55: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/55.jpg)
Firing a bullet � Open Bullet
� Create the act method public void act() { }
� Call move(10);
![Page 56: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/56.jpg)
Firing a bullet � Try it again!
� Wait now the bullets go left � Oh, we never set a direction!
![Page 57: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/57.jpg)
Firing a bullet � Open Bug again
� Set the direction to up setRotation( -90 );
� Try it!
![Page 58: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/58.jpg)
Firing a bullet � Bullets are too close together.
� Let’s slow them down
� We could limit the number of bullets
� Or we could limit the rate of fire � We will only allow bullet to be fired if a certain
amount of time has passed
![Page 59: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/59.jpg)
Firing a bullet � Bullets are too close together.
� Let’s slow how fast we shoot
� We will only allow bullet to be fired if a certain amount of time has passed
� In Bug, we need to create two variables in the class � One for how often we are allowed to fire � Another for how long has passed since the last bullet
was shot
![Page 60: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/60.jpg)
Firing a bullet � Both will be integer values (whole numbers)
� Create the values like so
� Go to act()
� Change the if statement � If space is down and myLastShot equal 0 � Inside the if set myLastShot = myRateOfFire; � At the end of act() subtract 1 from myLastShot
� We are counting down until we can fire again
![Page 61: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/61.jpg)
Firing a bullet � Your code should look something like this
� Notice I am using a if statement to stop subtracting when myLastShot gets to zero
![Page 62: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/62.jpg)
Making bullets destroy things
� We need a way to test if an Actor can be destroyed � Remember how we detected if the bug hit a rock?
� We need to remove the objects � Remember how World and ScrollWorld have an
AddObject method? � Maybe they have a removeObject method
� Let’s double clicking one of them
� Last, what type should we test for? � Rocks? Actors? � Let’s create a special type for detecting what bullets
can destroy
![Page 63: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/63.jpg)
Making bullets destroy things
� Create a new class � Edit > “New subclass…” � Call it destroyable
� Make sure the class has not methods
� Replace the word class with interface � An interface is like a class (or type) � It only describe what the type can do – it can’t actually do
anything or even exist. You need to create a subtype of the interface
![Page 64: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/64.jpg)
Making bullets destroy things
� Let’s make rocks destroyable – they need to be subtypes � We just need to say the a rock implements
Destroyable
� Now a rock is Destroyable and a type of ScrollActor � It is two things
![Page 65: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/65.jpg)
Making bullets destroy things
� Open bullet
� Add the detect hitting things code from Bug � Change Bug to Destroyable
� Inside the if statement
� Remove the Actor and bullet
![Page 66: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/66.jpg)
Final touches
� Try it out
� Destroy some rocks
� How far should bullets go? Should they go for ever?
� Let’s limit how far bullets go � Give them a time limit then remove them
� Like counting with shots
� Or once they are off screen remove them � Since they only go up right now,
� If y < 0
![Page 67: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/67.jpg)
Final touches
� Try it out
� Destroy some rocks
� How far should bullets go? Should they go for ever?
� Let’s limit how far bullets go � Give them a time limit then remove them
� Like counting with shots
� Or once they are off screen remove them � Since they only go up right now,
� If y < 0
![Page 68: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/68.jpg)
Final touches
OR
![Page 69: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/69.jpg)
Extend the game with your ideas
� Add more rocks or objects
� Add more destroyable
� Add enemies that shoot bullets at you � Need enemies, new bullets – fancy stuff
� If you get a mushroom you can shoot faster � For a limited amount of time?
� For more help see the documentation or ask me!
![Page 70: Lesson&2:&Reading&aprogram&scaiano.com/edu/Lesson 2.pdfGreenfoot.isKeyDown(“left”) ! We don’t want “move” the bug, we want to slide it left or right ! For right setLocation(](https://reader033.vdocuments.site/reader033/viewer/2022042213/5eb7c14beb6a9131b95c7e0d/html5/thumbnails/70.jpg)
Documenta@on
• hYp://www.greenfoot.org/files/Greenfoot-‐API.pdf
• hYp://www.greenfoot.org/files/javadoc/