Learning to Program With Alice 2.2 Roger Wistar SM&SH 2011 Introduction Welcome to the wonderful world of programming! In this workshop you’ll be introduced to programming using the language Alice, a fun 3D-based system that lets you program just by dragging and dropping your code. Objectives Have fun and fall in love with programming! Create a sweet game that you can play later Learn most of the basic concepts behind programming: o Creating and running your program o Adding user events to your program o Giving commands to your objects o Using conditional statements o Using sequential and simultaneous commands Want to Learn More? If you’d like to do more Alice programming, check out this website: www.alice.org There you can download Alice for FREE and write your own programs! Step 0 – Getting Started Goal: Get Alice started and create your world. Hints: The Alice program is located down on the Dock. The name of the world you are starting with is “Chap09WacAMole” The world is located in the “Textbook” tab. Solution: 1. Find the Alice program icon on the Dock and single-click it. 2. Click the “Textbook” tab in the top of the welcome window. 3. Scroll down and double-click the “Chap09WacAMole” world. 4. You should see the basic world appear on your screen. 5. Save your program! Choose “Save File As” and save it to the Desktop. Step 1 – Moving Moles Goal: Get one of the moles to pop up when you hit the up arrow key. Hints: You’ll need to create a new keyboard event. The individual moles are all listed on the left-hand side of the screen. If you make the mole move up 1 meter, that should do it. You should run your program to make sure it works! Solution: 1. Click “Create New Event” and choose “When a key is typed”. 2. Click on “any key” and choose the up arrow icon. 3. Click and drag the “mole” object from the object list on top of the word “nothing”. 4. Choose mole move > up > 1 meter from the pop-up menus. 5. Test your program by clicking the “Play” button. 6. Save your program again by choosing “Save” from the menu. Step 2 – Random Moles Goal: Make a random mole pop up when you hit the up arrow key. Hints: The list of moles is a property of the World object. Dragging the list into your event will let you choose a random mole. You should run your program to make sure it works! Solution: 1. Click on the World object in the object menu and then click on “properties” down below. 2. Drag the “moles” list icon on top of the world “mole” in your keyboard event from Step #1. 3. Choose random item from list from the pop-up menu. 4. Test your program by clicking the “Play” button. 5. Save your program again by choosing “Save” from the menu. Step 3 – Whacking Moles Goal: Make a specific mole move down when you click it. Hints: You’ll need to create a new world method to handle the moving down. You should use a “For all in order” block to check every mole in the list. You should create a “When the mouse is clicked on something event” to use your method. Note: This is the toughest step by far – please pay close attention and ask for help if you need it! Solution: 1. Click on the World object in the object menu and then click on “methods” down below. 2. Click “create new method” and type “whackMe” for the method name. 3. Click the “create new parameter” button and type “mole” for the name. Choose object for the type. 4. Add the code that you see below by using the building blocks at the bottom of the window. 5. Create a new “When the mouse is clicked on something event” in your program. 6. Drag your whackMe method on top of the word “nothing” in your event. 7. Choose expressions > object under mouse cursor from the pop-up menu. 8. Test your program by clicking the “Play” button. 9. Save your program again by choosing “Save” from the menu. Step 4 – Automatic Moles Goal: Make the moles pop up automatically. Hints: You can create a Loop block that will run infinity times (forever). You can pause in between moles by using the Wait block. You should run your program to make sure it works! Solution: 1. Click on the “World.my first method” tab so that it appears in front (an empty yellow window). 2. Drag a Loop block and drop it in the yellow window. 3. Choose infinity times from the pop-up menu. 4. Drag a “Wait” block and drop it on top of “do nothing” in the Loop block. 5. Choose 1 second from the pop-up menu. 6. Drag the “random item from World.moles move up 1 meter” block from your keyboard event up and drop it right underneath the Wait block, so that there are two commands in your loop. 7. Drag your old keyboard event to the Trash Can. 8. Test your program by clicking the “Play” button. 9. Save your program again by choosing “Save” from the menu. Step 5 – Noisy moles Goal: Add sounds to when the moles pop up and get whacked. Hints: The sounds are located in the “properties” window of the World object. You should put them in a “Do together” block with the corresponding action. You should run your program to make sure it works! Solution: 1. Select the World object and click on its “properties” tab. 2. Scroll down to where it says “Sounds” and click the plus sign (+) to open the window. 3. Click and drag the “POP” sound into your whackMe method as you see below. 4. Click and drag a “Do together” block into your whackMe method. 5. Put both the move command and the sound command into your “Do together” block. 6. Repeat Steps #2-5 with the “pop2” sound effect in your event that makes the moles pop up. 7. Test your program by clicking the “Play” button. 8. Save your program again by choosing “Save” from the menu. Step 6 – Scoring moles Goal: Add an score bar that moves up when the user whacks a mole. Hints: The score bar is already created (named “playerScore”), but it’s hidden under the ground. Try to make the score bar move up just a tiny amount so that the game seems challenging. You should run your program to make sure it works! Solution: 1. Drag the playerScore object into your whackMe method right under the sound effect. 2. Choose playerScore move > up > other… from the pop-up menus. 3. Type “0.1” in the calculator window and press “Okay”. 4. Test your program by clicking the “Play” button. 5. Save your program again by choosing “Save” from the menu. So what else can I do? Here are some ideas for ways to extend your game even further: Add code so that when the yellow bar is the same height as the gray bar, the program prints out some congratulations message like “YOU WIN!” and makes a fun sound effect. Find better sound effects and import them into your program. Replace the “pop” sounds. Make it so that moles can’t rise up above their holes if they haven’t been whacked yet. Add a sweet title screen that appears before the actual game starts. Remember… The only limit when you are programming is your own imagination. Explore, make mistakes, have fun and keep trying! Thanks! Mr. Wistar rwistar@hotchkiss.org @sophist42 (Twitter) (860) 435-3662