Mr. Wortzman INTRO. TO COMPUTER SCIENCE. UNIT 1 – CUSTOM BLOCKS

Preview:

Citation preview

Mr. Wortzman

INTRO. TO COMPUTER SCIENCE

UNIT 1 – CUSTOM BLOCKS

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

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

• ...

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

• 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

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)

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

• ...

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!!

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

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.

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

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

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.

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.

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

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!

ARGUMENT TYPES• Click the black arrow next to "Input Name" in the block argument dialog:

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?

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

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

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.

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.)

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

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

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!!!

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

Recommended