Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables.
Create a Simple Game in Scratch
ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Harry Potter Scratch Game
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations.
Write Your First Computer Game!. Coding: Programming Languages Just like you can speak Chinese to someone who understands Chinese to tell them what to.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Scratch Programming Session 9 of 10 Review elements to use in stories, games, etc.
Session 5 of 10 Review + Multiple Sprites
1 An introduction to programming concepts with Scratch.
An intro to programming concepts with Scratch Session 2 of 10 sessions I/O, variables, simple computing.
1 An introduction to programming concepts with Scratch (in 5 hours + homework)
Madlib-Input, Strings, and Lists in Scratch Barb Ericson Georgia Tech June 2011.
Scratch Understanding some programming techniques using Scratch Resetting, Parallelism and Events.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Scratch the Cat. Object Oriented Programing Writing computer programs Based on Objects Instead of Actions Based on Data Instead of Logic.
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.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Spiral Rider PAGE 1. Set Up Scene 1.Add Stage-underwater scene 2.Add crab sprite 3.Add two fish sprites PAGE 2.
Summer Computing Workshop. Session 2 Input in Scratch  Multi-Character input - This is used when the user is prompted to enter a number or word.  Problems.
Introducing Scratch the Cat
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
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.
Introduction to Programming G50PRO University of Nottingham Unit 2 : Introduction To Scratch Paul Tennent
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.
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
Scratch Another computer programming language Developed by MIT in 2003
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.
24 Background Building 25 Computing Terminology, and Speed/Velocity Module 4 Notes: Sensing, Or Operator, Conditional Logic 28 Module 4 Algorithms,
Chapter 7 Problem Solving with Loops
Scratch Creative Computing. INTRODUCTION TO SCRATCH Section 1.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
How to create a basic game in Scratch. The Scratch Stage The Scratch stage is 480 pixels wide and 360 pixels high x increasesx decreases.
Madlib-Input, Strings, and Lists in Scratch Barb Ericson Georgia Tech.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Introducing Scratch Learning resources for the implementation of the scenario
School of Computer Science Space School 2015 Programming a Lunar Lander Game.
Review for Final June 13, 2016.
Scratch Programming Cards
Create a Halloween Computer Game in Scratch
Scratch for Interactivity
Madlib-Input, Strings, and Lists in Scratch
Scratch Unit Overview We are going to look at computer programming and how to create your very own computer game The piece of software we will be using.
Spanish Mad libs with Scratch
IF statements.
Intro to CS Monday, August 29
Intro to CS Monday, August 24
Scratch for Interactivity
Introduction to Object-Oriented Programming
Learn… Create… Program
Number and String Operations
Go to =>
Learn… Create… Program
An intro to programming concepts with Scratch
Game Over Module 4 Lesson 2.
Learn… Create… Program
Learn… Create… Program
Creating a Simple Game in Scratch
Presentation transcript:

Summer Computing Workshop

Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs later in the program. Lets create a variable for the new sprite. Call this variable score. On the left you should see and blocks. These are the two most used blocks when it comes to manipulating variables in Scratch. Programmers use similar methods of setting the value to a specified number, or incrementing/decrementing the value. Now we will look at a brief overview of this session.

Session 5

Basic Overview Incrementing/Decrementing Variables – This function is useful when decreasing or increasing variables by a specified number (e.g. the a player losing lives in a game or increasing a players score). Setting Variables – This function is used to set the variables value to a specified number (e.g. setting the score to zero after the game is restarted or setting the players lives to max after the game is restarted) Using Variables – Programmers use variables to store data for later use. Problems/Type differences – Similar to the issues related with input, different types of data may have unexpected outcomes.

Incrementing/Decrementing Variables For this examples we need a forever loop, an if conditional block, a change score by block, a change x by block, another sprite (total of 2), and another block we havent used before called. This block is found in the sensing category and will be used in the predicate part of an if/else block to detect the collision of some other object (another sprite, an edge, etc). We also need to give the user control over the cat similar to the cat in the input session. Use whatever type of single-character input you want, allow the user to move the cat towards the other sprite eventually allowing them to collide. Now we will create a structure that increments the score only if the two sprites are touching. Lets construct this example (see example 5.1 on the Constructed Examples page at the end of this section). Notice what happens when we stop and restart the program, the score doesnt change as though the game started over. This is where the ability to set the variable comes in handy.

Setting Variables To demonstrate setting a variable, we will use the same script we just pieced together. Drag a to the scripts area and put it above the forever loop but still a part of the Scratch script. When do you think the variable score will be set; before the next game starts, or as the next game starts? Run the script and see for yourself (see example 5.2 on the Constructed Examples page at the end of this section). If you notice, the setting of the variable doesnt occur when the stop button is pressed, it occurs after the green flag is clicked again. Now we will enter single stepping mode and watch the script as it runs in order to further establish this concept.

Using Number Variables There are many different ways programmers use variables throughout their programs. Variables that contain numbers are often used in repeat blocks, move blocks, and change by blocks. Numbers can also be stored for later use in the program. For this example, we need to construct a script that asks the user how far they want the sprite to move, then sets that value to a variable called move. The sprite then moves right or left depending on the entered value. Construct example (see example 5.3 on the Constructed Examples page at the end of this section).

Using Word Variables Words can also be used and stored into variables. Examples could be asking someone's name, or what their favorite color is, or if they would like to continue with the program. The example we will construct will modify the previous script to also ask the user if they would like to continue, and if not, to enter the word no. This will require us to create a new variable and set it to the answer. Everything needs to be in a repeat until loop and we only repeat if the answer variable is not equal to no. Ok, sounds difficult but it really isnt too bad. Lets now construct this example and see how it works (see example 5.4 on the Constructed Examples page at the end of this section). If you stop the script, notice the variable still contains the word no. This means we must set the value of this variable to anything other than no immediately after the script is started.

Problems that could Arise In the program we have created, when the sprite asks How far would you like me to move? try entering a word or letter instead of a number. Again the sprite doesnt move. Just as discussed in the input session, Scratch doesnt allow the programmer to specify different types of data. If you notice, the value of the variable move is whatever you entered. In most other programming languages, programmers specify what type of content the variable will contain. If it were possible in Scratch, it would be good practice to specify what type of data the variable was going to contain later in the program. This is so we dont have incompatible variable types.

Amazing Example Now we will construct a fairly difficult script using only one sprite. The script demonstrates how variables can be used throughout a script. The goal is to have the sprite draw a grid using nested loops and the pen feature that was discussed in session 3. First, we want to clear what has been drawn and point the sprite in the 90 degree direction. Ask for the size of each box inside the grid, assign that value to a variable called box size. Ask for the width of the grid or how many blocks wide and assign it to a variable called width. Ask for the height of the grid which is how many boxes high the grid will be and assign the value to a variable called height. We will need 3 repeat loops. Now lets construct our example (see example 5.5 on the Constructed Examples page at the end of this section).

Constructed Examples Example 5.1Example 5.2Example 5.3 Example 5.4

Constructed Examples Example 5.5

Projects 1. Create a Scratch script that averages multiple numbers. You can either ask the user to enter how many numbers they would like to average and use a repeat loop, or use a repeat until loop and wait for the user to enter done. 2. Create a Scratch script that asks the user to enter 2 numbers, stores them as variables A and B, then switches theses numbers.

Session 5 Questions 1. What is useful about loops? 2. Once variables contain a value, can that value be changed? 3. Can a variable contain a number, then later in the program, contain words? 4. If so, could this be a problem? 5. There are two loops, the outer executes 5 times, the inner executes 3 times. Inside the inner loop, there is a block of code that makes a sprite say hello world. How many times will the sprite say hello world? 6. Can a variable be used any place a number can be used?