# StarLogoTNG 101 Treasure Hunt Game Unit Lesson 9: Variables.

## Presentation on theme: "StarLogoTNG 101 Treasure Hunt Game Unit Lesson 9: Variables."— Presentation transcript:

StarLogoTNG 101 Treasure Hunt Game Unit Lesson 9: Variables

Today’s Agenda Review Mini-lesson on variables and new blocks Implement new game features Wrap-up

Review Whenever a moving enemy agent collides with a hazard, program the enemy to “hatch” a half size version of itself and have the child enemy head in a different direction from the parent. The hazard dies. Whenever the moving enemy collides with a treasure, if the moving enemy is smaller than 1, it increases its size back to 1. The treasure dies. Whenever the moving enemy collides with itself, program the enemy to “hatch” a new treasure. Challenge: How can you use “random” so that the enemy only has a 50% chance of hatching a new treasure?

Variables What is a variable? (hint: what are some examples from math or science classes?) A variable in programming is like a container that holds a value that can change. The value can be a number, a boolean (true or false), or a string (characters like a word or phrase). Hello 258 TrueFalse Bye

Variables You’ve already used some variables such as score, xcor, and color. What are some other examples of already defined variables in StarLogo TNG? Look in the Traits drawer. What type of variables are pen down?, pen up? and alive? How can you tell?

Agent monitor window If you click once on any agent in Spaceland, you bring up its monitor. Here you’ll see the current values of its already defined variables. You can even change the values and immediately see the effect on that particular agent.

Variables You define (or create a container for) a variable using the blocks in the Variables drawer. There are two kinds of variables: agent and shared. If you define a shared variable, there is only one variable container for the whole game. If you define an agent variable, every agent of a certain breed gets its own variable container.

Shared vs. Agent Shared variable Agent Variable

Variables: Practice 1 Classify each of the following variables as agent or shared: 1.Size 2.Xcor 3.Score 4.Breed

Variables: Practice 2 How would you define variables for each of these purposes: 1.The number of treasures left in the game 2.The energy content of each bullet. 3.The number of “lives” that the player has 4.The number of bonus “speed boosts” that can be triggered by the player

Setting the value of a variable Set variable block found in My Blocks palette. Shared variable only: Slider – can be adjusted by the user in the Runtime window.

Setting the value of a variable Typically, you want to set the starting value of each variable in the Setup block as a part of resetting the game. For agent variables, you can set the starting value as part of the Create-Do block or the Hatch-Do block. When the game is running, you’ll change the variable as needed by using the set variable block with an expression or constant.

Getting the value of a variable You want to “get” the value of a variable as part of changing its value (see previous set variable example) or to compare it to another number as a way of making a decision. To get the variable, go to the My Blocks palette and look inside the drawer of the canvas section where you declared the variable.

Getting the value of a variable So, in our example with the mana variable, the player collects “mana” and then can use mana to do special things like let the player move faster for a certain period of time.

Monitor Use the monitor block in the Setup and Run drawer to show the value of shared variables.

Guided Programming Open the current version of your “treasure game your initials.sltng” Save next version Define an agent number variable called “energy” in the thrown projectile section of the canvas. When hatching the projectiles, set energy to 100. In the Run (forever) block, in the projectile’s section, decrease the energy variable by 1. When the energy gets down to zero, the projectile dies.

Guided Programming Note the addition of the energy variable in the agent monitor window of a projectile agent.

Brainstorm uses for Variables What other uses for variables can you think of for your game? Here are some ideas: The number of treasures left in the game The energy content of each bullet. The number of “lives” that the player has The number of bonus “speed boosts” that can be triggered by the player Timer to track the “life” of certain agents or events

Programming Activity Implement a new game element that uses at least one variable. Decide on the game element, consider which type of variable is most appropriate, and then follow the steps below. Three basic steps to using a variable: 1.Define the variable 2.Initialize the variable (setup, create, hatch) 3.Use the variable (change it, compare it, etc.)

Wrap Up Describe your new game feature that uses one or more variables. What worked? What didn’t work? What challenges did you have? What was fun? What questions do you have? What new game ideas do you have?