scratch the cat. object oriented programing writing computer programs based on objects instead of...

56
Introduction to Object-Oriented Programming Scratch the Cat

Upload: jeremy-morton

Post on 25-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • Scratch the Cat
  • Slide 2
  • Object Oriented Programing Writing computer programs Based on Objects Instead of Actions Based on Data Instead of Logic
  • Slide 3
  • Traditional Programming Input Data Process the Data using Logic Output the Data Example: Add 2 Numbers Input Data Get Number 1 Get Number 2 Process the Data Add Number 1 + Number 2 Output the Data The Answer is: _______
  • Slide 4
  • Object Oriented Programming Focus is on the Object we want to Manipulate/Change Person Animal (Scratch the Cat) Building Buttons / Icons Identify What Objects You Need How do the Objects Relate to Each Other
  • Slide 5
  • Objects All Objects have 3 Parts Properties What the Object looks like Methods Things the Objects can do Events What activities (by the user or by the computer) that the object responds to
  • Slide 6
  • Our Programming Example Dog vs. Cat The cat and dog will walk up to one another and say hi (bark and meow) Object Oriented Programming What objects will we need? (Objects and Properties) What do the objects need to do? (Methods) When do they need to do these things? (Events)
  • Slide 7
  • Lets Get Programming Start Scratch the Cat Double Click on the Scratch Icon
  • Slide 8
  • Scratch Interface Methods and Events Code Objects Display Properties
  • Slide 9
  • Objects What Objects do You Need? Cat Dog Cat Already Exists (Scratch) Set Properties for Scratch
  • Slide 10
  • 1) Click on Scratch 2) Click Costumes 3) Edit Costume1 Properties Edit Cat Properties
  • Slide 11
  • Bigger RotateRotate Smaller Smaller FlipFlip Tools Colors
  • Slide 12
  • Edit Cat Properties Change the Animal Erase the Current Cat 1) Click Eraser 2) Use Mouse to Erase Scratch
  • Slide 13
  • Edit Cat Properties 1) Import 2) Click Animal Folder 3) OK
  • Slide 14
  • Pick New Animal 1) Pick a Cat 2) OK Scroll
  • Slide 15
  • Edit Properties Change the Size Make Smaller ?????
  • Slide 16
  • Add Sound Properties Sounds
  • Slide 17
  • Add Sound Scratch already has One Sound MEOW Add Another Sound Import Animal Cat OK 1) Import 2) Animal 3) Cat
  • Slide 18
  • Add A 2 nd Object - DOG Add New Sprite (Object) Add New Sprite
  • Slide 19
  • Add a 2 nd Object - Dog Look Familiar What Should We Do??
  • Slide 20
  • Edit the Object Properties Resize Make Smaller Rotate if Needed So Dog will Face the Cat
  • Slide 21
  • Add Sound New Objects will not have any existing sounds Add a Sound Import Animal Dog1 OK 2) Animal 3) Dog1 1) Import
  • Slide 22
  • Add Events Events are the Actions Each Object will Respond to Can be Computer Generated or User Generated Cannot Respond to EVERY action 1 st Event in this Program Starting the Program When the User Clicks the Green Flag
  • Slide 23
  • Events Responses to Actions Can be Computer Generated or User Generated We need to tell the Objects (Cat and Dog) what Actions to Respond to They cannot respond to everything 1 st Event Starting the Program When the Green Flag is Clicked 1) Make sure CAT is Clicked 2) Make sure Scripts is Click Move Objects around using Mouse to look like this.
  • Slide 24
  • Event When Flag Clicked Flag Event is in the Control Section Using Mouse Drag to the Script area 1) Control 2) Drag Flag to Scripts Area
  • Slide 25
  • Methods When Flag Clicked What do we want the Cat to do when the Flag is Clicked Say Meow Make a Meowing Sound These are Methods Add Method to Code Window (Attach to Event)
  • Slide 26
  • Method Say Meow Find Method LOOKS section Drag Say HELLO for 2 Secs to Scripts area We dont want Cat to Say Hello Boxes in Methods are things we can change 1) LOOKS 2) Drag Say Hello to Code
  • Slide 27
  • Method Say Meow Click on Hello box Type MEOW Click on 2 box Type 1 Test Click the Green Flag (Code a Little Test a LOT)
  • Slide 28
  • Add Next Method Play Sound Add Sound SOUND Section Drag Play Sound Cat to Scripts Area Change Cat to Meow Test 1) Sound 2) Drag to Event
  • Slide 29
  • Variables A variable is a placeholder for a number that we dont know what it is yet, or a number that can change It has a name We assign it a value when we are ready We can change the value when we need to Lets include a variable to count how many times the cat meows
  • Slide 30
  • Make a Variable - Counter Click the VARIABLE section Click on the Make a Variable option Give the Variable a Name: COUNTER Make sure For all Sprites is clicked OK 1) Variables 2) Make a Variable 3) Give a Name
  • Slide 31
  • Variable Choices Variable Shows Up
  • Slide 32
  • Give Variable a Value We need to set a Value for our Counter Variable When all variables are created in Scratch they are given a value of Zero We need to set it to zero so that it resets everytime we start over and hit the green flag
  • Slide 33
  • Give Variable a Value Drag Set Counter to 0 method over to the Scripts Area Put just under Drag to Code List
  • Slide 34
  • Update Value We want to change the value of the counter every time the cat meows What options do we have to do that??
  • Slide 35
  • Update Value Drag Change Counter by 1 method over to the Scripts Area Underneath other instructions
  • Slide 36
  • Repeating Actions How many times to we want the cat to meow? 5 times? 10 times? As long as we run the program? We do not want to have to add more instructions for each time Programmers use something called a LOOP to repeat Lets have our cat meow 15 times
  • Slide 37
  • Adding a Loop Click on CONTROL section Find the Loop instructions Drag the instructions to Scripts Area Need to put above the cat meowing to repeat this action 1) Repeat 2) Put Here
  • Slide 38
  • Adding a Loop Change the 10 value to 15 to loop 15 times Test It Watch Counter We could use the Forever loop to loop as long as the program is running Change to 15
  • Slide 39
  • Events - Let the Dog Respond The dog has to know that the cat has meowed Cat has to cause a computer generated event Dog can respond by barking Broadcast an Event And wait for a response
  • Slide 40
  • Create an Event Drag Broadcast to the Scripts Area Click on the Down Arrow NEW Name the Event CatMeow 1) Drag Broadcast and Wait 2) Arrow - NEW
  • Slide 41
  • Have Dog Respond Click on the Dog Sprite No Scripts yet for the Dog Object
  • Slide 42
  • Have Dog Respond Dog must listen for the CatMeow event Drag When I receive CatMeow to the Scripts Area When the Cat Meow and the event fires the Dog will hear it What should the dog do??? Bark
  • Slide 43
  • Dog Bark LOOKS Section Drag Say Hello for 2 Secs to Scripts Area Change Hello to Woof Change 2 secs to 1 sec Test It 1) Drag to Scripts Area 2) Change to Woof and 1 Sec
  • Slide 44
  • Dog Bark SOUND Section Drag Play Sound to Scripts Area Click on Down Arrow Select Dog1 sound Test It 1) Play Sound 2) Dog1 Sound
  • Slide 45
  • Dog Chases Cat Dog moves toward the Cat Move 20 steps Cat moves away from the Cat Move 10 steps Add Code for Each Object to Move
  • Slide 46
  • Dog Chases Cat Make sure the Dog Object is Selected MOTION Section Move 10 Steps Change to 20 Steps 1) Dog 2) Move 10 Steps 3) Change to 20
  • Slide 47
  • Cat Runs from Dog Click Cat Object Motion Move 10 Steps Put Under Broadcast Test It 1) Pick Cat 2) Move 10 Steps 3) Put into Scripts
  • Slide 48
  • Clear Method Return the Objects to their Starting Place to Run Again Clear Method Responds to an Event Move Dog to Location (-125, -30) Move Cat to Starting Location (135, -15) Uses Pixel numbers (0,0) is the middle of the screen
  • Slide 49
  • Clear Method 1) Cat 3) When I Receive Event 2) CONTROL
  • Slide 50
  • Create the Event Drag to the Scripts Area NOT part of other instructions Click on the CatMeow Box Click NEW to create a new Event Name Event CLEAR
  • Slide 51
  • Move the Cat Locations use an X and Y values for Height and Width MOTION Section Set X to 0 Set Y to 0 Change X to 135 Change Y to -15 1) Set X to 0 2) Set Y to 0 3) Change Values
  • Slide 52
  • Broadcast the Clear Event Make the Clear Event Happen Beginning of Program 1) CONTROL 2) Broadcast CatMeow
  • Slide 53
  • Broadcast the Clear Event CONTROL Section Drag to Top Right below Click on CatMeow Change to Clear Event 1) Below Flag Click
  • Slide 54
  • Move the Dog Uses the Same Event Still Need to listen for it (Already being Broadcast) 1) Click Dog Object 3) When I Receive CatMeow 2) CONTOL
  • Slide 55
  • Move the Dog CONTOL Section Drag to the Scripts Area Not part of other instructions Click on the CatMeow Box Click Clear event
  • Slide 56
  • Move the Dog MOTION Section Set X to 0 Set Y to 0 Change X to -125 Change Y to -30 1) Set X to 0 2) Set Y to 0 3) Change Values