mr. wortzman intro. to computer science. unit 1 – custom blocks
TRANSCRIPT
![Page 1: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/1.jpg)
Mr. Wortzman
INTRO. TO COMPUTER SCIENCE
![Page 2: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/2.jpg)
UNIT 1 – CUSTOM BLOCKS
![Page 3: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/3.jpg)
PROCEDURAL DECOMPOSITION• Imagine an algorithm for making cookies:
• Making sugar cookies
• Mix the dry ingredients
• Cream the butter and sugar
• Beat in the eggs
• Stir the dry ingredients into the wet
• Set the oven to 400°
• Put the cookies in the oven
• Bake for 10 minutes
• Remove the cookies from the oven and allow them to cool
• Mix the ingredients for the frosting
• Frost the cookies
![Page 4: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/4.jpg)
PROCEDURAL DECOMPOSITION• What if we were making a double batch?
• ...
• Set the oven to 400°
• Put the first batch of cookies in the oven
• Bake for 10 minutes
• Remove the cookies from the oven and allow them to cool
• Put the second batch of cookies in the oven
• Bake for 10 minutes
• Remove the cookies from the oven and allow them to cool
• ...
![Page 5: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/5.jpg)
PROCEDURAL DECOMPOSITION• This algorithm is unstructured, making it hard to follow
• It’s also redundant in the double batch case
• We can do better!
• Procedural decomposition: breaking a large task down into a number of smaller, self-contained subtasks
![Page 6: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/6.jpg)
• We can decompose our recipe into three subtasks:
• Making sugar cookies
• Mix the batter
• Bake the cookies
• Frost the cookies
• The first subtask is defined as:
• Mix the batter
• Mix the dry ingredients
• Cream the butter and sugar
• Beat in the eggs
• …
PROCEDURAL DECOMPOSITION
![Page 7: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/7.jpg)
PROCEDURAL DECOMPOSITION• This also allows us to eliminate redundancy in our double batch recipe:
• Making sugar cookies (double batch)
• Make the batter
• Bake the cookies (batch #1)
• Bake the cookies (batch #2)
• Frost the cookies (batch #1)
• Frost the cookies (batch #2)
![Page 8: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/8.jpg)
PROCEDURAL DECOMPOSITION
• Even better, it helps us avoid errors when part of the algorithm changes:
• ...
• Set the oven to 400°
• Put the first batch of cookies in the oven
• Bake for 10 15 minutes
• Remove the cookies from the oven and allow them to cool
• Put the second batch of cookies in the oven
• Bake for 10 minutes
• Remove the cookies from the oven and allow them to cool
• ...
![Page 9: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/9.jpg)
CUSTOM BLOCKS• How many times have we built this
script?
• Wouldn’t it be nice if we could writethis once, give it a name, and then just use it like a regular block?
• Turns out…..we can!!
![Page 10: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/10.jpg)
CUSTOM BLOCKS• Click on “Make a block” in the “Variables” tab
• Can also right-click on the script area
• Can put a block in any category (or none)
• Blocks without a category appear at the bottom of the “Variables” tab
• Once created, custom blocks are treated exactly the same as built-in blocks
![Page 11: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/11.jpg)
CUSTOM BLOCKS• Exercise 1: Define a custom block called drawSquare that draws a square of side
length 50. Put it in the “Motion” category.
• Exercise 2: Define a custom block called drawTwoSquares that draws two squares of side length 50 side by side.
![Page 12: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/12.jpg)
BLOCK TYPES
• There are three types of blocks in BYOB:
• Commands make the sprite do things
• Like
• Shaped like puzzle pieces
• Reporters provide a value after they are run
• Like
• Shaped like an oval (round)
• Predicates provide true/false values
• Like
• Shaped like a hexagon (pointy)
• Basically just special reporters
![Page 13: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/13.jpg)
REPORTING VALUES• Reporters and predicates must report their result
• Sometimes called returning
• Two ways to do this:
• Use the spot at the bottom
• Use one or more blocks
• Notice the shape!!
• Once you have reported, the block will stop executing
![Page 14: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/14.jpg)
REPORTING VALUES• Exercise 1: Define a custom block called distanceFromCenter that reports how
far a sprite is from the point (0, 0)
• Recall the distance formula:
• Exercise 2: Define a custom block called isInAir that reports whether or not a sprite’s y-coordinate is above 35.
![Page 15: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/15.jpg)
BLOCK ARGUMENTS• What if we want a square of a size other than 50?
• Do we need to define a separate block for every possible square size?
• When defining a new block, you’ll sometimes see orange plus signs:
• These are used to add arguments to the block.
![Page 16: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/16.jpg)
BLOCK ARGUMENTS• Arguments are like variables whose value can be changed each time you invoke a block.
• These are the white circles/rectangles in things like and
• This allows us to write more general custom blocks
![Page 17: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/17.jpg)
BLOCK ARGUMENTS• Exercise 1: Rewrite your drawSquare block to take an argument specifying the side
length.
• Exercise 2: Write a drawRectangle block that takes two arguments specifying the side lengths.
• Bonus: Rewrite drawSquare to use drawRectangle!
![Page 18: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/18.jpg)
ARGUMENT TYPES• Click the black arrow next to "Input Name" in the block argument dialog:
![Page 19: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/19.jpg)
ARGUMENT TYPES• By specifying the type of an argument, we can limit what values can be passed
• Only numbers, only Booleans, etc.
• This helps us avoid improper input
• One option is the "C-shape" (lower-left)
• What built-in blocks have this shape?
![Page 20: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/20.jpg)
SCRIPTS AS DATA• When we use a “C-shape” block, we
are really passing a script as an argument
• We can do this because BYOB treats scripts as data
• Just like numbers, text, colors, etc.
• We can utilize this to do other cool things
![Page 21: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/21.jpg)
HIGHER-ORDER BLOCKS• When we accept a script as an argument, we are making what is called a higher-order
block
• This allows us to, essentially, create a whole class of related blocks
• We can define parts of the behavior, and specify the rest when we call the block
• To execute the argument script, we use the or block
![Page 22: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/22.jpg)
HIGHER-ORDER BLOCKS• Exercise 1: Write a block called doTwice that performs a given command twice. Put a
short delay between the two executions.
• Exercise 2: Write a block called myRepeat that acts like the repeat block. You may not use the repeat block in your definition, but you may use other loops.
![Page 23: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/23.jpg)
HIGHER-ORDER BLOCKS• Exercise 3: Write a block called forever unless that is like the forever if
block, but only executes the body if the condition is false.
• Exercise 4: Write a block called alternate that takes two commands as arguments and alternates between performing each one forever. (Perform one, then the other, then the first, etc.)
![Page 24: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/24.jpg)
THE “OF” BLOCK• Under the Sensing tab is this block:
• We can use this to find information about other sprites (or the stage)
• The dropdown shows you the various things you can ask about
![Page 25: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/25.jpg)
THE “THE SCRIPT” BLOCK• Under the Operator tab is this block:
• This block is used to turn commandblocks into an object we can use as an argument
• When combined with the "of" block and the "launch" or "run" block, we can make other sprites do things
![Page 26: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/26.jpg)
COMBINING THESE THREE• We can put these three things together to tell other sprites to do stuff
• This will cause Sprite2 to take these actions regardless of which sprite executes this block!!!
![Page 27: Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS](https://reader035.vdocuments.mx/reader035/viewer/2022062409/56649cdc5503460f949a7f83/html5/thumbnails/27.jpg)
RUNNING SCRIPTS FOR OTHERS• Exercise 1: Write a program that includes two sprites. The position of one sprite should
determine the other's behavior as follows:
• If sprite1 is in the 1st quadrant, sprite2 spins clockwise
• If sprite1 is in the 2nd quadrant, sprite2 spins counter-clockwise
• If sprite1 is in the 3rd quadrant, sprite2 moves forwards
• If sprite1 is in the 4th quadrant, sprite2 moves backwards