StarLogoTNG
Treasure Hunt Game UnitLesson 1: Basic Movement
Today’s Agenda
• Starlogo TNG basics• Mini-lesson on programming movement• First programming challenge• Wrap-up
What’s What in SLTNG
Definition of “Agent”
• Like a character or object in a game
• Can be programmed to have behaviors
• Can have more than 1
First Programming “Blocks”
• Give an agent behavior instructions by dragging blocks from the palette onto the canvas
• Many blocks take arguments
• An agents “executes” an instruction when the agent does what the instruction says
Arguments
Your First Program
• File menu > Open project “first program.sltng”• File menu > Save project as “first program your
initials.sltng”• In runtime window, click once on “set up” and
then “run” to start the program. The program will end after 60 seconds. Repeat to run the program again.
• Practice: run the game a few times, move around in spaceland and try different camera angles using the Guide to Spaceland activity sheets.
About First Program
• What did you notice about how first program works?– How did the score change? What do you
think the score is based on?– How did the turtle agents behave? What
happened when they reached the edge of Spaceland?
How Score is Calculated
• The score is the running added total of the height of each patch that a turtle is standing on.
• Green patches = 0 (no effect on score)
• Blue patches = negative (varies)
• Red patches = positive (fixed)
• Yellow patches = positive (varies)
Programming Activity
• Get the highest score possible by programming additional movement instructions when the turtles detect that they’re on a particular color patch
Movement Blocks
Found in Movement drawer:
• Forward steps• Back steps• Left degrees• Right degrees
Dragged onto canvas
Quick geometry review!
• How many degrees are there in a circle?
• Degrees for turning left or right are relative to the direction the turtle is currently facing – for example, the labels in the diagram on the right shows the degrees for turning right.
• What should be the degree labels if we’re turning left?
0 degrees
90 degrees
180 degrees
270 degrees
45 degrees
About First Program
Setup:Create and scatter 20 turtles
Run: All turtles move 1 step forward
Run: Your instructions
Run: Calculate score
Time=60 sec? End
No
Yes
Programming Activity 1
• Get the highest score possible by programming additional movement instructions when the turtles detect that they’re on a particular color patch
• For example, how should the turtle behave if it detects that it’s standing on a blue patch?
Programming Activity
• Try out different commands and sequences; see what happens to the score each time. There are many ways to achieve a high score. Try to use as few blocks as possible.
One more thing…
• Every time you want to test out your program, click setup once (turtles created and scattered) and then run once (wait for green highlight around the run block).
• Use the blue slider bar to speed up or slow down the program.
Programming Tip
To delete a block, drag it to the trash can in the corner
of the canvas
Programming Tip
Save often.Save now.
Wrap Up
StarLogoTNG 101
Treasure Hunt Game UnitLesson 2: Treasures and Hazards
Today’s Agenda
• Review• Mini-lesson on breed editor and new
programming blocks• Modify first program• Wrap-up
Review
• What do you predict will be the agent’s movement when it executes the following block?
• What about this stack of blocks?
Breed Editor
• Click “Edit Breeds” button near top of canvas to bring up the editor.
• Used to assign “shapes” and names to breeds.
• Breeds are used to create different types of agents.
• Each breed has its own set of programs and blocks (or block section).
• Exercise: Click “new” button and rename the newly created agent to “treasure” and select a new shape.
New Block: Setup
• Found in the “Setup and Run” drawer
• Used to set initial conditions of the game
• Example: what happens in first program when you click on setup?
• Brainstorm starting conditions for your treasure game
New Blocks: Clear
• Found in the “Setup and Run” drawer
• “Clear everyone” makes all agents disappear
• You can learn about the other clear blocks in your reference guide
• Look at the setup block in first program. Why is it important to include “clear everyone” in setup? What do you think will happen if you remove this block? Make a prediction and test it!
New Blocks: Create
• Create is used to create agents of a particular breed.
• Because it is specific to each breed, these blocks are found in the “My Blocks” palette.
• On the “My Blocks” palette, each drawer is named for a breed or a section of the canvas.
• Select the breed you want to create agents of, and look for the “Create breed name” block.
• This block takes a number argument that specifies how many agents you want to create.
• There is also a “Create breed name” that has two parts – the number of agents and a “do” section.
• The “do” section allows you to specify additional instructions, such as placement of the agent in spaceland and setting the camera angle.
New Block: Scatter
• Located on the “My Blocks” palette
• Found in each breed’s drawer
• Usually used after agents are created to place them randomly in spaceland
• There is also a “scatter everyone” block in the Setup drawer for scattering all agents that have been created, regardless of breed.
Guided Programming 1
• In the existing setup block, attach the appropriate blocks to create 10-20 treasure agents and scatter them.
• Test out your new setup by clicking setup in the runtime window.
New Block: Collision
• Found in each breed’s drawer in the “My Blocks” palette
• Used to determine what happens when one agents touches another agent
• What happened in second program when the player agent collided with a tree agent?
New Block: Die
• Found in the “Logic” drawer
• The agent that runs the block deletes itself (disappears completely from Spaceland).
• How do you think this block was used in the second program?
Guided Programming 2
• Drag a collision block that shows “treasure” and “turtles” onto the collisions section of the canvas
• Think: what do you want to happen when the turtle collides with the treasure?
• Explore different possible results for collision. Use blocks that you already know, such as set color*, set size, movement blocks, die, and set score.
*To see an agent’s color, you also need to use the block “model skin off” found in traits.
Programming Activity
• Create a new breed for Hazards• Add create and scatter hazards to setup.• Think: what do you want to happen
when the player collides with a hazard? • Explore different possible results for
collision (check out the reference guide for new blocks or explore new blocks on your own).
Wrap Up
Review
• What is the purpose of the Setup block? What blocks typically go in the Setup block?
• If you don’t put a “scatter” block, where are the agents placed when you create them?
• Program a collision block between turtle and treasure agents so that the treasure moves up into the air 1 step and change its color to purple.
StarLogoTNG 101
Treasure Hunt Game UnitLesson 4: Forever and Procedures
Today’s Agenda
• Review• Mini-lesson on new programming blocks• Create v1 of treasure hunt game• Wrap-up
Programming Tip
Use the canvas sections to keep your program
organized
Programming Tip
Use the mini-map on the upper right corner of the canvas to quickly navigate to different sections of
your program.
Treasure Game v.1
• File menu > Open Project > “treasure game”• Save as “treasure game your initials.sltng”• About the treasure game
– Setup: creates 1 player agent; creates treasure agents and non-moving hazards agents
– Run (forever): player agent(s) calls a procedure that executes movement when the player presses various keyboard keys to control the player agent from 1st person point of view
– Collisions: between player and treasure and hazard agents
Guided Programming 1: Setup
• Breed Editor:– Rename turtle breed to “player” (or your choice) and
select a new character– Make new breed called “treasure” (or your choice)– Make new breed called “hazard” (or your choice)
• Drag setup block to setup section of canvas– Attach “clear everyone” block– Create 1 player agent– Create treasure and hazard agents and randomly scatter
them
• Click on setup in runtime window to check that the setup does what you want.
New Block: If / then
• Found in the “Logic” drawer• First part (“test condition”) must evaluate to
true or false• If test condition is true, the blocks in the “then”
section is executed. If false, ignore the “then” section.
• Example: see first program
Exercise: If / then block
• What do you think these IF blocks will do when executed by an agent? Explain your thinking.
New Blocks: Keyboard detect
• Found in the “Controls” drawer.
• Question mark means that the blocks tests if that particular key is pressed – evaluates to true or false.
• Click on down arrow on the right side of the block to select a different keyboard key to test.
Programming Activity 1
Program instructions to control the agent’s movement from the agent’s point of view:– Up arrow key = move forward– Left arrow key = turn left– Right arrow key = turn right– Down arrow key = move backward
New Block: Forever
• Found in the “Setup and Run” drawer
• Repeats instructions placed in this block over and over again, in order from top to bottom, and then starts at the top again.
• Each breed has its own section.
• Turn on/off by clicking on the green arrow
• Can also run from runtime window
Guided Programming 2: Forever
• Drag a Forever block to the runtime section of canvas.
• Rename the Forever block “Run.”• Attach the If/then keyboard controls stack to
the Player section of the Run block.• Turn on the Run block and test the keyboard
controls.• What would happen if you move the Keyboard
Controls stack to the treasure section?
Do Now: Set Agent Camera, ID, and views
• In first person games, it’s helpful to start the game from the player’s perspective.
• You do this using the following blocks when you create the player agent in Setup:
About Procedures
• Recall the cake making analogy: procedures are like those sub-tasks (make the batter, bake the cake, decorate the cake)
• Procedures make programs easier to build and to understand.
Make a cake
Make the batter
Bake the cake
Decorate the cake
New Block: Procedure
Guided Programming 3: Make a procedure of the keyboard movement controls that are currently in the player section of the run block. Replace those instructions with a call to the procedure.
To make (or “define”) a procedure:• Position your canvas in the Player
section.• Drag a green “procedure” block
from the “Procedure” drawer.• Rename the procedure “keyboard
controls.”• Drag the if/then blocks and
connect them to the procedure block.
New Block: Procedure
• To “call” a procedure is to use a block with the procedure’s name; the procedure’s instructions are executed in the place where it’s called.
• Each breed and programming section has its own procedures (found in “my blocks” palette).
To call the “keyboard controls” procedure:• Go to the “my blocks” palette and
open the “Player” drawer. • Find the procedure that’s labeled
“keyboard controls” and drag it to the empty player section of the run block.
• Start the run block to make sure that the keyboard controls are working properly.
New Blocks: Score
• Score is a variable. This means that it represents a number that can change.
• You can change the value of score using set score, found in the “Setup and Run” palette.
• Set score takes either a single number or a mathematical expression that is evaluated to a single number.
• For example, if you want to increase the score, you can drag out the addition block from the “Math” palette.
• The pink score block represents the current value of score. Drop that in the left side of the expression. Drag a pink number block on the right side of the expression and change the number to the amount you want the score to increase.
• You can also change the sign of the math expression block by clicking on the down arrow next to the operator symbol.
• Think: What are some ways to use score in your treasure game? When should the score be changed?
Programming Activities
• Implement collision results between the treasure and hazard agents and the player.
• Implement score-keeping. (Hint: don’t forget to attach the “show score” and “set score 0” blocks in the setup block!)
• Explore different starting conditions. (Hint: you can have more than 1 setup block)
Wrap Up
StarLogoTNG 101
Treasure Hunt Game UnitLesson 5: Moving Enemies
Today’s Agenda
• Review and share ideas• Mini-lesson on new programming blocks• Implement moving enemies• Wrap-up
Review
• Add a keyboard control so that if you press the “L” key, your player makes the “laugh” sound.
• Define a procedure in the treasure section called “announce if blue” that makes the agent say “I’m blue!” if the agent is blue. Have the treasure agents call the procedure in the Forever block.
• In the collision block between player and treasure, add an IF statement that checks if the treasure is blue. If it is blue, player gets an extra 100 points.
Moving Enemies
• Another way of making the treasure hunt game more interesting and challenging is to implement moving “enemies.”
• These enemies can do any number of things to make game play difficult for the player, but today, we’ll program their movement.
• Brainstorm: What do you want to happen to the player and the enemy when they collide?
New block: Random
• Found in the Math drawer• Takes a number
argument• Returns a random value
between 1 and the number argument
• For example, random 10 returns a value between 1 and 10
Random block exercise
• What will happen when each of the following uses of the random block is executed?
New block: Say
• Found in the Text drawer• Takes a text argument• Text includes letters, punctuation
marks, and numbers displayed as characters
• When executed, a speech bubble pops up above the agent that shows the text argument
• How did the say instruction help you figure out the obstacle course game? What are some creative ways you can use it in your treasure hunt game?
Guided Programming
• Create a new breed called “moving enemy” and select a shape for it.
• In the setup block, create 10 moving enemy agents and scatter them.
• Create a procedure called “Enemy movement” and just put one instruction “forward 1” for now.
• Call the procedure in the “moving enemy” section of the run block.
• Click setup in the runtime window. Before you click run, what do you think will happen when you click run? How will the enemy agents move? Now click run.
• Questions: What happens when the moving enemy agents hit the edge of the board? Is it hard or easy to avoid the enemy agent?
Programming Activity
• Program different movement instructions in the “enemy movement” procedure and see what happens. (Hint: Try using random)
• Implement collision results between the moving enemy and the player.
• What can you do with a collision between an enemy agent and another enemy agent? Be creative!
Wrap Up
Review
How do you code the following game features?When you create the treasure, set their size randomly
(from size 1 to size 3).When the player collides with a treasure, the score
gained is equal to the size of the treasure times 100 (e.g., size 1 treasure is worth 100 points, etc.).
When a moving enemy agent collides with a treasure, the color of the treasure is set to orange.
If the player collides with an orange treasure, the score is reduced by the size of the orange treasure times 100.
StarLogoTNG 101
Treasure Hunt Game UnitLesson 7: Coordinate System
Today’s Agenda
• Review• Mini-lesson on coordinate system in
SLTNG and new programming blocks• Programming activity• Wrap-up
Review
• Program the moving enemy agents to compete with the player agent to “collect” treasure.
Coordinate System
• Spaceland uses an x and y coordinate system.
• (0, 0) is located in the center of the green square.
• The range of x and y coordinates is -50 to +50.
y = +50
y = -50
X = +50X = -50
New blocks: set x, set y, set xy
• Found in the Movement drawer
• Take number arguments
• xcor and ycor are variables that keep track of each agent’s present location
Set x, Set y, Set xy Practice
• What do the following two sections of blocks do?
• How can you change the setup section above so that the player appears in the bottom right corner?
New blocks: set heading, heading
• Found in the “Movement” drawer.
• Takes a number argument.• Heading is a variable that
represents the current direction of an agent.
• A heading of 0 means that the agent is facing “up,” or “north” when one looks down on the green plane from above.
• Recall that there are 360 degrees in a circle. See diagram on right for what different values of heading mean.
0 degrees
90 degrees
180 degrees
270 degrees
45 degrees
Set heading Practice
• What do the following sections of programming code do?
• How can you use the “set heading” block in your treasure hunt game?
Guided Programming 1
• Open the current version of your treasure game. Save next version.
• Use “set xy” and “set heading” in the setup block to place your player agent in a certain location facing a certain direction at the start of the game.
• You may need the Create [Do] block.
Wrap Up
StarLogoTNG 101
Treasure Hunt Game UnitLesson 8: Hatch
Today’s Agenda
• Review• Mini-lesson on new programming blocks• Implement “shooting” or “throwing”
game feature• Wrap-up
Review
• Program a keyboard control for the player to teleport to a random location when ‘t’ is pressed.
New block: Hatch
• Hatch is found in the Logic drawer.• The plain hatch block creates a new
agent (“child”) that is the same as the agent that hatches it (“parent”).
• The hatch-do block does the same thing but also allows you to modify the new agent – for example, change its breed, change its size, put it in a certain location, etc.
• What game features can you add to your treasure game using Hatch blocks? (hints: collision, keyboard command)
New block: set breed
• “Set breed” is found in the traits drawer.
• It takes a “breed” argument, which can be found in the “My Blocks” drawers.
• It sets the breed of an agent to a different breed.
“Shoot” feature
• A popular game feature is for your player agent to “shoot” or “throw” something – e.g., paintballs to paint another agent, bullets to kill an enemy, net to capture treasure.
• What other “shooting” or “throwing” game features can you think of?
Guided Programming
• Open the current version of your “treasure game your initials.sltng”
• Save next version• How can you implement “shooting” using what
you’ve already learned plus Hatch and Set Breed?– What object (agent) do you want the player to shoot?– How should the shot agent behave after it’s been
shot?– What do you want to happen when the shot agent
collides with different agents in your game?– What key should trigger the shot?
Programming Activity
• Find another use for the Hatch block in your game – for example, if two moving enemies collide, they can “hatch” a new moving enemy.
• Improve the real-world physics of shooting – for example, how can you make the shot agents die when they hit the edge of the board?
Wrap Up
StarLogoTNG 101
Treasure Hunt Game UnitLesson 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. The hazard dies.
• Whenever the moving enemy collides with a treasure, the moving enemy increases its size back to 1. The treasure dies.
• Whenever the moving enemy collides with itself, program the enemy to “hatch” a new treasure.
• Extra credit: 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), text, or a list.
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 window. 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 shared variable container for the whole game.
• If you define an agent variable, every agent 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.Size2.Xcor3.Score4.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
has4.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 number 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
• 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 variable2. Initialize the variable (setup, create, hatch)3. Use the variable (change it, compare it, etc.)
Wrap Up
StarLogoTNG 101
Treasure Hunt Game UnitWrap Up
Today’s Agenda
• Review• Mini-lesson and programming practice
on set camera and ID• Finish making the treasure game• Create documentation for playing the
game• Optional programming activity• Wrap-up
Review
• The treasure game has a global variable called “mana.” When students accumulate 5 or more mana points, they can spend it by activating a “bonus” mode.” Implement the bonus mode feature.
• This “bonus mode” is a global boolean variable, meaning that it’s true when the bonus mode is activated and false when not in bonus mode.
• When the bonus mode is true, all the hazards behave like treasures for a certain period of time. At the end of the time, the bonus mode is set back to false.
Smell
• An agent uses the “smell” block to detect agents that are around it.
• There are different variations of “smell.”
• We will use “smell” as a feature in a game where there are many player agents. So if one dies, the camera switches to another player that’s still alive.
How will your game end?
What are the conditions for winning and losing the game?– Time limit?– Collecting a certain number of treasure?– Losing all “lives”?– Losing all “energy?”– When all the players die?
How will you let the player know when the game ends?– Say bubble?– Play a sound?– Agents do a little dance?
Documentation for Your Game
• “How to Play” document/manual– A few sentences on why the game is fun and
what it’s about– Objective of game – how to win– Keyboard controls– Screen shots?
• Comments for other programmers– Explain different parts of your program
Comments
• To add a comment, right-click on any block in the canvas and select “add comment.”
• A yellow text box will appear. Click the arrow to make the box larger and type your comments.
• You can resize the comment box.
Comments
• To see which comment box goes with which block, left-click on the comment box and follow the dotted line.
• To delete a comment, right click on the block that the comment is attached to, and select “delete comment.”
Optional Programming Activity
• Save next version• Swap computers w/ another classmate,
play his or her game for a while, and program one more game features to improve his or her game.
• Write comments describing any code that you added or changed.
To get StarLogo TNG for your home computer
Website: http://education.mit.edu/starlogo-tng/
downloadRegister, download, install
On-line reference of blocks:http://education.mit.edu/starlogo-tng/
documentation
Wrap Up