200 likes | 221 Views
Variables. What will we learn today?. What Are Variables Variables in Real-Life Variables in Programs Types of Variables Creating a Timer Example. Variables. A Variable is a storage location (place in computer’s memory) which contains information
E N D
What will we learn today? • What Are Variables • Variables in Real-Life • Variables in Programs • Types of Variables • Creating a Timer Example
Variables • A Variable is a storage location (place in computer’s memory) which contains information • Variables are analogous to "Buckets" or "Envelopes" where information can be maintained and referenced • Each variable has a name (an identifier), a type and a value (piece of information) • E.g. (a=5, b=“hello”) • Variables allow us to keep track of changes as our program runs
Variables in Real Life • We use variables to track the balance of money in a bank account • We use variables to track the amount of gasoline in the tank of the car
Variables in Programs • We often need to keep track of changes in position, color, and other properties of objects in our programs • In a game, we may want to keep track of number of steps or times or numbers of goals..etc • For example, we want to know the number of times a player has put the basketball through the hoop or how much time is left on the clock
Types of Variables • Numeric • They can store a whole number value, e.g. 7 or 1024 • Boolean • They can represent one of two states, true or false • Character/String • They can represent a single character, e.g. 'A' or '$‘ or a group of characters, e.g. “Ali” or “20 minutes left” • Object • They can represent a whole object, e.g. cat or fort
A Timer for a Game Example • A timer is used in most of the games to keep track of how much time the player has left to play • We will construct a timer that counts down in seconds • Our task is to set the timer to an initial number of seconds (e.g. 10s) and then count down the seconds remaining in a game • The number of seconds remaining will be displayed and will be updated with each passing second • We will create a chicken that rotates around itself as the timer count down
A Timer for a Game Example • Add a new object: a 3D text • Type 0.0 which is the value that appears when the time’s over
A Timer for a Game Example • Rename the object to timer
A Timer for a Game Example • Add a new variable in the timer object • Select the timer object • Select the properties tab • Click on the ‘create new variable’ button • Name it ‘timeLeft’ and set it to type ‘number’
A Timer for a Game Example • Drag the ‘timeLeft’ to the ‘world.timer’ method • Create a new parameter and call it ‘amount of time’
A Timer for a Game Example • Set the values as shown • Create a new method and call it ‘countDown’
A Timer for a Game Example • Inside the method, add a while loop with expression ‘a<b’ and set ‘a’ and ‘b’ as follows • Drag the text inside the while loop
A Timer for a Game Example • Set the value of the text as follows, drag ‘what’ from the functions box • Drag time left from properties insert it into the what box
A Timer for a Game Example • Move the timer up and down • Make a function that returns the time left
A Timer for a Game Example • The ‘countDown’ method • Create a method ‘playGame’ and set it as follows
A Timer for a Game Example • The main method ‘world.myFirst’ method
A Timer for a Game Example • Here is our Chicken
Let’s Work with Alice • Counting Fishes A Scuba diver (people) wants to count the fishes (ocean) that eat from the Seaweed (ocean). Make a method where each fish goes to the seaweed, eats, waits, and comes back. As each fish does so, a counter on the top-right corner of the screen (3D-Text) increases by one. After all fish have eaten, the scuba diver says the final count of fishes. You will need to create a number variable ‘NumberOfFish’ for that.