Treasure Hunt Game Programming Activity

Updated: 7/17/2014
Treasure Hunt Game Programming Activity
An extensible, build-from-scratch, game programming activity in StarLogo Nova
This activity is designed for users with some experience working with and extending StarLogo Nova projects and
would like to learn how to build a new project from start to finish. Rather than present step-by-step instructions, it
contains a series of small challenges paired with some commands that are useful for completing the challenges.
The result of completing the challenges is a fully functional, extensible treasure hunt game.
Goals




Create a treasure hunt game in StarLogo Nova
Learn to start a project from scratch
Explore new commands without fear of breaking something
Learn essential concepts that apply to every StarLogo Nova game.
Instructions
Read each challenge and examine any commands or features that are given to help complete the challenge. Then
try to complete the challenge in any way you want to. Do not be afraid to explore and make mistakes! Use any
commands or features you want to use, but if you are stuck make sure to look at what is supplied with the
challenge. Feel free to ask your neighbors for help, but always make sure to make your own mistakes first.
Challenges 1-5 will lead to a basic, but fully functional game. Challenges 6-10 extend the basic game and teach the
use of more blocks. There are programming tips scattered throughout the activity sheets. If you do not have time to
complete the remaining challenges, you may still want to read the programming tips, which are relevant when you
are programming any StarLogo Nova project, not just for this activity.
Game Description
The player controls an agent by using keyboard arrow keys to collect as many treasure agents as possible while
avoiding obstacles.
IMPORTANT NOTE: There are blocks provided with every challenge. These blocks are only suggestions to
complete the challenge. IT IS NOT NECESSARY TO USE ALL THE BLOCKS PROVIDED.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
1
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 1: Choose and create your game objects/player


Create a player breed, treasures breed, and obstacles breed.
Create 1 player agent, many treasures, and some obstacles. Scatter some or all of your agents.
Useful features/commands:
: Scroll up to the SpaceLand to use Edit Breed.
Drawer
Interface
Agents
Blocks
What the agent does:
Executes commands inside this block (once, from
top to bottom) when the push button widget is
pushed. Widgets can be created using the Edit
Widgets feature.
Deletes everyone including itself. *The World
cannot delete itself.
Creates the specified number of agents of the
chosen breed.
Agents
The 2nd block adds the feature where each
newly created agent immediately follows the
directions the block makes them "do".
Traits
Agents
Agents
"set my ___ to ___": Sets its trait to some value.
The trait is chosen from the drop down menu
and includes color, shape, size, etc.
“color: ____”: Represents a specific color or a
color at random chosen from the drop down
menu..
Sets its location to a random position in
SpaceLand.
Sets all the agents' locations to random positions
in SpaceLand.
Test your setup code by clicking “Run Code”, then the setup button in SpaceLand to see if setup is working as you
intended.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
2
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 2: Set camera to 1st person perspective
To start the game in 1st person perspective of the player agent, program the player to take camera when the player
agent is created.
Useful features/commands:
Drawer
Agents
Block
What the agent does:
Enables perspective of over-agent’s-shoulder view; i.e.,
the camera follows the agent around.
Reset Camera brings the view back to default.
Agent View follows the agent who has taken the
camera as it moves around.
Sample Code:
Test your setup code by clicking “Run code”, then the setup button in the SpaceLand window. You should be
looking over the shoulder of your player agent.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
3
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 3: Keyboard Controls
Procedures are useful when you want to use a set of instructions multiple times or when you want to break down a
complicated task into discrete parts and program each part separately in an easy-to-read way.
There are two important parts to using procedures:
 Defining a procedure: adding instructions to the procedure block. It is good programming practice to give
the procedure a name that describes the set of instructions.
 Calling a procedure: using the procedure after it has been defined (for example, in a while forever toggled
block).
Define a procedure in StarLogo Nova by dragging a “Procedure” block from the Procedure drawer to the
Workspace on the Player’s page.
Double click on the Procedure name and change it to Keyboard Controls.
Programming Tip
Which page should I use to
define procedures?
Under the Procedure block, attach the following blocks:
Define procedures on the page of the
breed that will follow the procedure. If
multiple breeds follow the procedure,
define it on the Everyone page.
These blocks read: Agent tests to see if the up arrow key is held down. If so, go forward 1 step.
Program additional blocks to do the following:
 Down arrow = Back 1
 Left arrow = Turn left 5 degrees
 Right arrow = Turn right 5 degrees
You may want to use cut and paste, and drop
menus on blocks.
To call a procedure means to program a breed of
agents to follow the procedure. You want the player
agent to follow the Keyboard Controls procedure so
you will call the procedure in the “While forever
toggled” block in the “Player’s” page. (see next
challenge)
Programming Tip
Windows computer: Ctrl-C = copy and Ctrl-V=
paste
Mac computer: Cmd-C = copy and Cmd-V =
paste
Example: Drag a box around the chunk of blocks you want
to copy. It’s fine if the box outline overlaps with other
blocks but only whole blocks within the outline will be
copied. Use drop down menus to change blocks to the
desired arrow key.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
4
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 4: Agents’ behavior
Recall that the “While forever toggled” block runs instructions from top to bottom in a loop while the “forever”
button is toggled on.
Program the following:
 Have the player agent call the Keyboard Controls procedure.
It will look like this:

Test your code before continuing.
Useful features/commands:
Drawer
Block
What the agent does:
Interface
Executes commands (in a forever loop) when the
toggle button widget is turned on. When the toggle
button widget is turned off, the commands in this
block don’t run. Widgets can be created using the
Edit Widgets feature.
Procedures
Agent calls a defined procedure to follow from the
drop down menu.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
5
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 5: Interactions between agents
Use Collision blocks to program what happens when agents collide with each other. Remember that the basic idea
of the treasure game is for the player agent to collect treasure and avoid obstacles.
Program the following:
 Treasure page: On collision with player, treasure agent increases score by 1. Treasure deletes itself. Use
the score data box widget to change and display the score, like this:




Player page: On collision with obstacle, player deletes itself
Initialize (set) score to 0 in the existing setup block.
To make the game harder or easier, adjust the numbers of starting obstacles and treasures
Test your code before continuing.
Useful features/commands:
Drawer
Block
What the agent does:
Detection
Tells the "owner" of the block what
to do when it bumps into an agent
of the chosen breed. The “owner” is
an agent of the breed whose page is
where this collision block is put.
Agents
Deletes itself.
Agents
Deletes the specified agent (use
"collidee", "nearest", or "my parent"
blocks).
Detection
Interface
Refers to the agent currently
colliding with the "owner" of the
block. (Only works inside an on
collision with block)
Assigns a value to a data box.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
6
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Interface
Returns the value of the data box.
Math
Tells me the value of the computed
expression.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
7
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 6: “Lives” for Player agent
Instead of the Player agent dying immediately when it collides with an obstacle, you can program a “lives” (or
“health”) data box for the player agent that decreases every time the player agent collides with obstacles. When
lives get to zero, the player dies. One way to program this is to use a data box widget.
1. Create a data-box widget called “lives” or “health” (no quotes).
2. Initialize (set) the lives data box to a starting value of your choice in the when setup pushed block. For example,
starting number of lives given could be 4, 5 or 10.
Set the lives data box to decrease every time player collides with an obstacle. (Hint: Similar to the above Score
data box.) Also program the obstacle to delete so you do not get multiple collisions.
3. Player breed checks if its lives data box is ≤ to 0. If true, then player dies.
4. Test your code before continuing.
Useful features/commands:
Drawer
Block
What the agent does:
Logic
Test for something. If true, then the
agent follows the commands in the
hook.
Logic
Determines if the first input is less
than or equal to the second input and
returns true or false
Interface
Returns the value of the data box.
Interface
Assigns a value to a data box.
Math
Returns the difference of two inputs
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
8
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 7: Adjust traits for fun
Agents come with a number of built-in traits that you can adjust. Click the drop down menu to see the available
built-in traits.
Explore and try adjusting the traits of your agents in the Create-Do blocks.
Sample code:
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
9
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 8: Tweaking
Try programming some of these features or think of other things you can do to make your game more
interesting!


Make different color treasure worth different number of points. You may want to create batches of treasure
with particular colors in the existing Setup block.
o Hint: Use “Create-Do” blocks to control the traits
Program an ending for the game if the player wins (passes some threshold for score) or dies.
o Hint: Use the “toggle forever off for everyone” block to program the Player agent to turn off the
forever button when either of these conditions are met.
o Hint: Create a new data box widget to display a message like You Win! or Try again! OR play a
recorded sound to do this.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
10
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 9: Rival agents
To increase the excitement level of your game, add rival agents who can also collect treasure, thus reducing the
number available for the player.
1.
2.
3.
4.
Add the rival breed.
Create one or more rival agents.
Program the rival agents to move by creating a procedure. Start with something simple at first, like forward 1.
Program the collision between rival agents and treasures so that the treasures delete
Improvements:
 Try different movement instructions to see which will allow the rival agent(s) to cover more ground.
 Adjust the number of rival agents until you achieve the right level of challenge.
 Try adding randomness to your movement instructions.
Useful features/commands:
Drawer
Block
What the agent does:
Movement
Moves forward with a set number of steps
per tick
Movement
Moves backward with a set number of steps
per tick
Movement
Turns left with a set number of degrees
during each tick
Movement
Turns right with a set number of degrees
during each tick
Math
Returns a random number between 1 and
the specified number. Generates a new
random number each time.
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
11
Treasure Hunt Game Programming Activity - Updated: 7/17/2014
Challenge 10: Rival agents have their own score
Program the rival agents keep their own score so that the player’s goal is to beat the rival agents’ score. This
involves creating a data box widget that functions like score.
1. Create a new data box widget and rename it to something like “rival score” (no quotes).
2. Initialize (set) it to 0 in the existing setup block.
3. Update the value when it increases in the appropriate collision block.
Useful features/commands:
Drawer
Block
What the agent does:
Any agent can modify data box widget.
Can be renamed to a more meaningful
name that describes its function.
In this case, it should be called “Rival
Score”
Test for something. If true, then the agent
follows the commands in the hook.
Logic
Assigns a value to a data box.
Interface
For example, to increase the rival’s score,
set the rival’s score text to rival’s score
data+1
By: MIT Scheller Teacher Education Program. This work is licensed under a Creative Commons Attribution 4.0 License, which allows anyone to re-distribute, re-use,
and modify, on the condition that the creator is appropriately credited.
12