August 31 Intro to CS
Today’s Objectives Hand in Homework Assignment Review last class Today If/Then/Else Parallelism Debugging How to deal with SNAP! edges
Review: Make Block or Right click on empty space in Script Area this dialog box will appear. or Press “Make Block” button in Variables and this dialog box will appear.
Two “Looks” commands – goto front , go back N layers Front, -2 layers Front, -1 layer Front layer With these 2 commands, you can control where the scenery resides.
Stage scripting Click on Stage icon Scripts just like for Sprites Broadcast messages This runs Mario Sprite only which is useful for debug
Today’s Objectives Hand in Homework Assignment Review last class Today Loops: If; If/Else; Repeat; Forever Parallelism Debugging How to deal with SNAP! edges
“Loops”: If/Else/Repeat Notice how the bottom is flat? This script ends here and does not move on. The contents inside will repeat until the condition is met. Repeat the # of times in the box. Then moves to the next block Executes if evaluation of Boolean expression is True. If not, it moves to the next block. Executes if evaluation of Boolean expression is True. If not, it executes the contents of “else”.
Lab: Model the ‘aRoomLightOn’ Boolean expression in SNAP Lab: Model the ‘aRoomLightOn’ Boolean expression in SNAP! (aRoomLightOn = pgePower AND (floorLamp OR tableLamp ) Create three variables: pgePower, floorLamp, tableLamp Initialize all three variables to be ‘false’ Say “The variables are initialized.” Broadcast a “Initialized” message Start when “initialized” message is received Create a Boolean expression that tests each variable for “true” Use an If/Else statement If expression = “true”, say, “The light is on” If expression is not = “true”, think, “Hmmm. I wonder when the light will turn on.”
Lab: ANSWER
Parallelism ( also called threads) These are 5 different streams of instructions/code also called threads. Program Counter can’t be in 5 different places at the same time!! SNAP executes a few instructions in one stream and then moves to the next. 1 GHz = 1,000,000,000 instructions in 1 second Makes it appear that each stream/thread is executed simultaneously.
Debugging A methodical process of finding and reducing the number of defects (bugs) in a computer program, making it behave as expected.
You need to know where the program stops working. Debugging Who do you find them? You need to know where the program stops working. How do you do that?
Debugging Here are a couple methods: Insert a block before and after the section of code that you think has the problems. (Be sure and remove them after you have solved the problem.)
Debugging You can also isolate the problem by running just he block or script itself. Click on the block or script. Duplicate the block to get it out of the script.
Debugging Finally, you can display global, local and system variables on the stage.
Debugging - Help Right click on desired instruction. Pops up a drop-down menu. Click on “help”.
Debugging - Help The Help dialog box pops up. Has and explanation and ... A code example!!!
Lab: Debug ‘August31Labs - aRoomLightOn’ (There are 3 bugs planted.) Type this into your browser, all on one line with no spaces or breaks: http://snap.berkeley.edu/snapsource/snap.html# Username=johngoble&ProjectName=Aug31Labs-v3
Lab: Debug ‘PowerOnLabs’-ANSWER
Dealing with EDGES in Mario – using Blocks, of course Let’s write some code which deals with a Sprite at the edge. Two choices – The Sprite can wrap or bounce.
Dealing with EDGES in Mario continued Question: Why use a CustomBlock rather than a stack of SNAP! blocks from the palette?
Dealing with EDGES in Mario continued Question: Why use a CustomBlock rather than a stack of SNAP! blocks from the palette? Answer: A stack of SNAP! blocks from the palette requires me to cut and paste the stack 4 times for each of the 4 Sprit I must fix a bug in 4 places instead of 1 place: Custom Block Also, 4 Stacks of the same code consumes too much screen area
Edges - Make Block Dialog box appears Give it a name Press OK Right click in Script area/Press MakeBlock button Dialog box appears Give it a name Press OK
Edges continued Click on “+” sign Name the input - bWrap LowerCase “b” reminds me it’s Boolean i.e. true or false. – not a number Click OK
Edge Logic bWrap = true If past Right Edge Wrap If not do nothing bWrap = false Let SNAP make it bounce. Click OK
Reminder of Stage Geometry
Test it!! I used “MakeVariable” to create 3 variables – LeftEdge RightEdge bWrapOrBounce Why is the “move 2 steps” not inside the CustomBlock? Let’s see it work!
Demo
Edge exercise - get ready Question: Why should CustomBlock be global but variable bWrapOrBounce be local?
Edge exercise - get ready Question: Why should CustomBlock be global but variable bWrapOrBounce be local? Answer : CustomBlock should be global so Mario, Tree , Gremlin and Mountain can use it. bWrapOrBounce should be local( this Sprite only) so that each Sprite can have individual behavior at the edge – Wrap or Bounce.
Lab: Edge exercise – 30 minutes Extend my example to both left and right edges. Your code should be able to Wrap or Bounce based on an input variable. When your code works “Save it” so you can use it on your Mario projects.
Open Lab Work on your project
Review of Today’s Topics Loops: If; If/Else; Repeat; Forever Custom Block w/input for Edge behavior Debugging
Reminders Read Chapter 2* before the start of class on Monday, September 12 *NOT just the excerpt http://www.bitsbook.com/ Test will be Friday, Sept 9 on computer parts and SNAP!