“Catch the Bananas!” CS4HS2010 - University of Michigan, Ann Arbor SUMMARY: In this workshop, you’ll make a game in which the user controls a monkey and when the user presses the arrow keys, the monkey runs across the screen, trying to catch bananas that are falling from a tree. Every time the monkey catches a banana, the user gets a point. Fig. 1: Screenshot of the “Catch the Bananas!” Game CONCEPTS COVERED: You will learn about if statements, variables, loops, algorithms and implementation. An “if statement” asks a question and then executes some code if and only if the answer to the question was true. With an if statement, your program can ask “did the monkey catch the banana”; if it does, then the code can increment the number of points the monkey has. A variable is a way for your program to remember something. For example, the number of points (or, the number of bananas the monkey has caught) is stored in a variable. Loops allow you to execute the same code over and over again. For example, we are going to want the bananas to continuously fall from the tree. It would be a pain to write code for every time the banana has to fall, so instead, we can use a loop to automate that process. An algorithm is a general method for solving a problem composed of a finite number of steps. Implementation is the actual code you use to get the algorithm to work. The algorithm for the monkey game has these steps: Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 1 1. 2. 3. 4. Make the monkey walk when user presses an arrow key. Make bananas fall from the tree If the banana touches the ground, go back to step 2 If the banana is caught (touches) the monkey, update the score and go back to step 2. It’s… Scratch Time!!!! Set up the files for the game o Import background to stage and appropriate costumes to your project sprite STEP 1 – Make Monkey Walk We first want the user to be able to control the monkey by pressing the left and right arrow keys on the keyboard. When the user presses the left arrow key, the monkey should move to the left; when the user presses the right arrow key, the monkey should move to the right. How can we detect a user pressing the keyboard? ______________________________________________________________________________________________ ______________________________________________________________________________________________ To make the monkey move right, combine the point in direction 90 block with the move 5 steps block. How should you then activate that script? The easiest way is to use the When right arrow key pressed Control block. Try this out. You can make the monkey left similarly: combine the point in direction -90 with the move 5 steps block and activate it with the When left arrow key pressed Control block Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 2 You should now have something like this for the right AND left arrows in the scripts area for the monkey sprite STEP 2 – Make Bananas Fall From The Tree You now need to choose an object that you want the Monkey to catch. We’ll assume you import a banana Sprite, but you could theoretically pick any object you like. We’ll start by writing the code for only one group of bananas; once we get this code perfect, we’ll copy the Banana Sprite so we have many of them falling from the tree. Lets pick a place where you want the bananas to fall from. Pick any x and y position you want for now. When the Green Flag is clicked, you should make the bananas go to this x and y position. Now, what should happen? ______________________________________________________________________________________ To make the bananas go downwards, should their x or their y position change? Should x (or y) get bigger or smaller? _________________________________________________________________________________________ o o NOTE: You should change x, y to represent the spot that you want to start the bananas from Let’s make the bananas fall from the tree. Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 3 We’ll use a new Motion block to make the bananas move in this game. You can tell the bananas to move down with the Motion block “change y by ___”. For now, we want the bananas to keep moving down forever. Connect the forever and change by ___ blocks together and try to get the bananas to fall from the tree. o o o HINT: how are the above blocks to be connected together? What does changing by y do? What would happen if you change by x? ______________________________________________________________________________________________ ______________________________________________________________________________________________ What should happen if the bananas make it all the way down to the bottom of the screen and the monkey misses it? ______________________________________________________________________________________________ ______________________________________________________________________________________________ Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 4 STEP 3 - Sense if the Banana Touches the Ground or Touches the Monkey Let’s make the bananas go back to their starting x and y position so it looks like another set of bananas are falling. o But how are we going to find out if the bananas are on the edge of the Stage? This is an example of where an if-block is helpful. The if block lets us run code if and only if a question is true. In this case, the question is “did the banana reach the bottom of the screen”, we want move the banana back to the top of the screen. o We can use the if block and the touching ____ block to detect if the banana has reached the bottom of the screen. Set the touching block to “touching edge” and place it inside the ifblock. o Inside of the if block, insert a go to x: y: block. There are lots of interesting questions under the Sensing Menu. o Blocks to use: After this step, you should have something like this in the scripts area for the banana sprite (you need to fill in the right values): Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 5 Now, we want the bananas to be able to be caught by the monkey. So, lets make the bananas also ask the question “Is the monkey touching me?” If the monkey touches the bananas, lets make the bananas go back to their starting x y position so it looks like it is another set of bananas falling. You can find these question under Sensing too. o We can use the if block and the touching ____ block to detect if the banana has touched the monkey sprite. Set the touching block to the correct value representing the monkey sprite in the machine and place it inside the if-block. o Inside of the if block, insert a go to x: y: block. o Blocks to use: o At this point, your code should be close to this skeleton code (i.e. code without correct values for blocks but shows basic control flow) Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 6 STEP 4 – Give Points for Catching Bananas We want the Monkey to get points for each of the bananas that he catches. We need the game to remember how many points the Monkey has and give him more points every time the Monkey catches the bananas. Points are an example of a Variable. Variables are used any time you need your program to remember something for you. Under the Variable Menu, click on Make a variable, and give the variable a good name like Points or Bananas! Make sure that the option For all Sprites is selected; this means that all Sprites can see and use this same variable. If the checkbox is marked next to the variable name, then that variable will be displayed on the Stage; we want the user to see their points, so make sure it is checked. Whenever you have a variable, you need to always decide how that variable should be initialized (i.e. what value it should have when the game starts up). How many points should the Monkey have when the game starts (initialized)? _______________________________________________________________________________________ Let’s initialize the number of points the user gets. A good place to put initialization code is with the Stage. Click the Stage icon and insert the following code there: o Use the block set–to block to initialize your points correctly and run that block When the Green Flag is Clicked. Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 7 Let’s give the monkey a certain amount of points every time it catches the banana. o When should we increment the number of Points the Monkey has? ___________________________________________________________________________________ o From Step 3, we already have an if block checking to see if the banana is touching the monkey, right? In that if-block insert the block change Points by X before go to x: y: block. X represents the point amount for catching a banana. Try it out! Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 8 STEP 5 – Make The Bananas Fall From Different Points in the Tree Of course, right now the game is too easy because the bananas always fall from the same place. You can change this with the pick random block for the x and y positions. You should figure out x and y ranges that make sense in your game. For example, for the Monkey game, the bananas should look like they are falling from the top of the tree, not from the sky or the sun! You’ll want to pick the minimum and maximum values you pass to pick random carefully. o o o NOTE: 1 is the minimum and 10 is the maximum value in this example Does it make sense to randomize the x value? the y value? ______________________________________________________________________________________________ What range of values restrict the bananas to the upper-left quadrant of the stage? ______________________________________________________________________________________________ What range of values restrict the bananas to the upper-right quadrant of the stage? ______________________________________________________________________________________________ You can make the game a little more interesting by making the speed at which the bananas fall random too. To do this, put pick random as the amount to change y by. I like y values between 0 and -5. o At this point in your game, the code skeleton might have this format o (note: w/out code for keeping score): Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 9 o STEP 6 – More Bananas! Now that you have the correct Scripts for the bananas, you can now duplicate the banana Sprite to make the game more interesting. Copy the Banana Sprite as many times as you like! Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 10 CHALLENGE 1: Change the “Speed” of the Bananas Create a variable named “level” for your game and initialize it to 0. Increase the level by 1 after the monkey has caught 5,10,and 15 bananas (points). Using your level variable, make the bananas fall faster if the user is on level 1, 2 or 3. CHALLENGE 2: Implement Monkey Power-Ups! For one of your banana sprites, create an alternate costume in which the bananas are red o You can use the paint editor to just paint your original banana red after copying it. Randomly select the bananas to change to the red costume If the monkey catches a “red” banana, then the monkey will “power up” and grow in size for 5 seconds. o To implement the power-up for 5 seconds you’ll need to: In the banana sprite: Hide the red banana once it gets caught using a hide block. Create and set a variable named “shrink” of your choice to “1” once this happens. Use the wait for 5 seconds block to wait 5 seconds After you wait, switch the banana back to the yellow costume In the monkey sprite: Use a forever block and a if block to check if the shrink variable is 1 and if so, shrink the monkey’s size using the “set size to” block Use the wait for 5 seconds block to wait 5 seconds After you wait, change the monkey’s size back to it’s original state (100%) CHALLENGE 3: Implement Monkey Power-Downs! Create an alternate black costume for one your banana sprites Randomly select the bananas to change the black costume When the monkey catches the black banana, have the monkey shrink in size for a limited time Worksheet adopted from “Wisconsin Catapult Project”: http://pages.cs.wisc.edu/~dusseau 11
© Copyright 2026 Paperzz