1 / 20

StarLogoTNG 101

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.

Télécharger la présentation

StarLogoTNG 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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?

  4. 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). 25 8 False True Hello Bye

  5. 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?

  6. 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.

  7. 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.

  8. Shared vs. Agent • Shared variable • Agent Variable

  9. Variables: Practice 1 Classify each of the following variables as agent or shared: • Size • Xcor • Score • Breed

  10. Variables: Practice 2 How would you define variables for each of these purposes: • 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

  11. 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.

  12. 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.

  13. 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.

  14. 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.

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

  16. 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.

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

  18. 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

  19. 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: • Define the variable • Initialize the variable (setup, create, hatch) • Use the variable (change it, compare it, etc.)

  20. 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?

More Related