Introduction to Programming G50PRO University of Nottingham Unit 2 : Introduction To Scratch Paul Tennent http://paultennent.wordpress.com/G50PRO.html paul.tennent@nottingham.ac.uk Room C41 What is Scratch? Scratch is a free programmable toolkit that enables users to create games, animated stories, and interactive art Enables users to share programs over the Internet Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab, with funding from the National Science Foundation, Microsoft, Intel Foundation, Nokia, and the MIT Media Lab research consortia. Getting Started With Scratch Open new project Choose, create or edit your Sprite Choose your background Write your program Programming In Scratch Click on the sprite you want to program, and select the “Scripts” tab. The Scripts area is where you “build” your program by using the programming blocks. In the upper, left-hand corner of your Scratch window, you will see 8 buttons . Each of these buttons have programming blocks in those particular areas. Programming In Scratch To program a sprite, drag blocks from the Blocks Palette to the Scripts Area. To run a block, click on it. Create scripts (programs) by snapping blocks together into stacks. Click anywhere on the stack to run the whole script, from top to bottom Scratch Stage The Scratch stage is 480 pixels wide and 360 pixels high. 180 240 -240 -180 Scratch Blocks Motion Animate sprites Control and Sensing controls how many times an event happens, how long an event happens, or when an event happens. conditional statements (“if statements” or “if-else statements”) Write code so that if the user presses the Space key on the keyboard, the sprite will move 20 steps and change direction. Scratch Blocks Sound Looks Pen Operators Variables Starting From Scratch Scratch has all of the common elements used in all programming languages. Conditional statements Loops Variables Events Branching (Conditional Statements) Make Tea START Is kettle full? yes Boil Kettle no Fill kettle Conditional Statements If [Boolean] Then Do this Else Do the other Comparisons == < > <= >= != Equals Less than More than Less than or equal to Greater than or equal to Not equal to Loops Simplify the coding of repetitive tasks Giving the people in a company a 10% rise Employee 1 salary = 1.1*Employee 1 salary Employee 2 salary = 1.1*Employee 2 salary … Employee 99 salary = 1.1*Employee 99 salary Loops Simplify the coding of repetitive tasks Giving the people in a company a 10% rise For i = 1 to 99 Employee i salary = 1.1*Employee i salary Loops There are a lot of different standard types of loop Scratch only has some of them Flow control Flow control In computer science flow control refers to the order in which the statements or instructions are executed or evaluated. Branches (Conditional Statements) Loops Variables a variable is a facility for storing data One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a named location that stores a value. Values are things that can be printed, stored or operated on. (numbers, letters, Strings, etc.) Variables each variable will have Name Value Location in memory X = 100 Y = 20 Assignment statement Declare str str = “Hello” the idea is straightforward: When you declare a variable, you create a named storage location. When you make an assignment to a variable, you give it a value. BankBalance Payment 700 Check Balance 700 700 Events You can’t run all of the elements of a program at once Things happen when the relevant event triggers them The main event in Scratch is the green flag There are other custom events that you can add Summary Scratch Programming Blocks Conditional Statements allow us to make decisions about what to do next Loops allow us to perform repetitive tasks Variables record the values and state of elements of a program Events trigger different elements of the program to be run at the correct time