160 likes | 171 Views
Learn how to create a treasure hunt game using programming blocks in StarLogoTNG. This lesson covers forever loops and procedures.
E N D
StarLogoTNG 101 Treasure Hunt Game Unit Lesson 4: Forever and Procedures
Today’s Agenda • Review • Mini-lesson on new programming blocks • Create v1 of treasure hunt game • 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.
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 movementwhen theplayer 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: 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?
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.
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?
New Blocks: Score • A simpler way of increasing or decreasing the score is to use “inc score” and “dec score” • So instead of “set score (score + 5)”, you can just use “inc score 5” • Similarly, instead of “set score (score – 5)”, you can just use “dec score 5”
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 • What collision results did you try? • 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?