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

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Create a Simple Game in Scratch
Create a Simple Game in Scratch
New Mexico Computer Science For All Decomposition in Computer Programming Maureen Psaila-Dombrowski.
Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
1 Procedural decomposition using static methods. 2 Algorithms Recall: An algorithm is a list of steps for solving a problem. What is the algorithm to.
1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods, Avoiding Redundancy reading: self-check:
1 An introduction to programming concepts with Scratch.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1: Introduction to Java Programming.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.
Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent.
Ms. Deveny Second Semester  Introductions  Interview your table partner  Name  Why taking CPD  Computer experience  Favorite game?
Lesson : Cookies & Cakes. One Bowl Method One Bowl Method: – All the ingredients are mixed in easy stages in the same bowl, so cleaning up is quicker.
Static methods. 2 Algorithms algorithm: a list of steps for solving a problem Example algorithm: "Bake sugar cookies" –Mix the dry ingredients. –Cream.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
 Review  Created our own motion block called “draw square”  Used script to create a square with side lengths of 100 steps.
2015 CSE/EGR Summer Camps 1 Computer Science Concepts 1. What is an algorithm? 2. Binary information coding 3. Programming concepts via Scratch Designed.
Section 1 Introduction National 4/5 Scratch Course.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Algorithms Writing instructions in the order they should execute.
Are you ready to bake???? Coconut BallsChocolate Chip Cookies.
Warm Up As you enter get out a half sheet of loose paper. Write the HelloWorld program on it. Be prepared to correct your code.
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
TRIGGERS Triggers tell a script to start executing There are four types of triggers: When green flag is clicked When I am clicked When is pressed When.
Create a Halloween Computer Game in Scratch Stephanie Smullen and Dawn Ellis Barb Ericson October 2008.
1 Project designed and created by M. Shajith Kumar.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
CSc 110, Autumn 2016 Lecture 2: Functions. Review What is the output of the following print statements? print("this class\tis' the \"best\"") Write a.
Functions / Blocks.
Static Methods and Method Calls
CSc 110, Autumn 2017 Lecture 2: Functions.
Create a Halloween Computer Game in Scratch
Exploring Mathematical Relationships Module 5: Investigation 1
Scratch for Interactivity
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Understanding the Geometric Shape Code
Scratch: iteration / repetition / loops
ECS 9/7/17.
AP Computer Science Mr. Wortzman.
Adapted from slides by Marty Stepp and Stuart Reges
Motion in Scratch 1.
Building Java Programs
CSc 110, Spring 2018 Lecture 3: Functions.
Building Static Methods
Building Java Programs
CSc 110, Spring 2018 Lecture 2: Functions.
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Unit 1: Principles of Computer Science
Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs
Scratch Lesson 2.
Building Java Programs
Building Java Programs
Creating a Simple Game in Scratch
Building Java Programs
2 Making Blocks.
Presentation transcript:

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

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 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 write this 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

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 command blocks 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. One sprite should continually move around the screen. When the space bar is pressed, the other sprite should ask for a new speed and then tell the moving sprite to change its speed.

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 1 st quadrant, sprite2 spins clockwise If sprite1 is in the 2 nd quadrant, sprite2 spins counter-clockwise If sprite1 is in the 3 rd quadrant, sprite2 moves forwards If sprite1 is in the 4 th quadrant, sprite2 moves backwards