Alice Lab 4
(Methods, If/Else, Events, and While Loops)
Part 1:
We’ll start with a simple loop. First, create a scene with a horse (from the animals gallery), a fence (from the building gallery), and Kelly (from the people gallery. Position them as follows:
This actually looks easier than it is. The fence and the horse should be fairly easy to position using the positioning buttons below the scene and in the upper right corner
Getting Kelly to appear as though she is sitting on the horse, however, takes a bit more work. To do this, I selected Kelly from the objects in my world (on the left side). I then selected lower body. On the right side, under the buttons, I chose affect subparts:
Now very carefully select Kelly’s right thigh and position it. Then select her right calf and position it. Finally, if necessary, click on and position her foot.
For Kelly’s left leg, select leftLeg under the Kelly object on the upper left side. Then Choose properties, and next to isShowing, choose false:
Finally, since Kelly is sitting on the horse, we want to make sure that if the horse moves, Kelly moves as well. To do this, select Kelly from the objects on the left side. Then select properties, and then select horse-> the entire horse:
So far so good.
Now write a method that makes the horse do one gallop: I turned both front legs back .125, and both back legs forward .125, then turned both front legs backward .25 and both back legs forward .25, then both front legs frontward and both back legs frontward .125. Feel free to play with the upper and lower legs as well to make it more realistic.
Test by making an event ( in the upper right side of window) such that when the world starts, do the horse.gallopmethod. Now hit play and see if this does what you want. If it doesn’t, play with it until it does.
Next write a jump method for the horse that makes the horse spread his legs straight out, go up 1 meter, and go forward 1.5 meters, then land with its legs down again.
Test this as you did your horse.gallopmethod.
Now we’ll put it all together. Create a brand new method under horse called gallopandjump. In this method you want to select do in order. First have the horse orient to the fence’s top Crossbar 2 (using the orient to method under horse methods). Now drag in the do together control statement. The first thing you want to do inside do together is use the move toward method to move the horse toward the fence’s top crossbar 2 until you are within .5 meters of the fence’s top crossbar2 (you will use the technique for calculating distance you used in Alice lab 2.) At the end of this command, select more, then duration, then make the duration really long (like 6 or 7 seconds using the other option at the bottom of the drop-down menu).
Next we will add a loop. We could just drag the horse.gallop method under the horse.move method above, but the horse’s legs would only gallop once, then the horse would continue to move forward, but its legs would be still. That’s not what we want. We want the horse to do the gallop method again and again until the horse is within .5 meters of the fence (which is where he’ll jump over the fence using your jump method). To make the horse do the gallop method over and over again until he’s within a certain distance of the fence, we’ll use a while loop.
1. Drag While (From the bottom menu) right under the horse.move toward method inside the do together statement. Select True
2. Select the World object (top left), then functions(bottom left) to get math.
3. Drag a<b over True. Select 1, < 0.5.
4. Now select the horse object (from above, left).
5. Under functions, select horse distance to. Drag that over the 1, then pick fence, then top crossbar 2
Now whatever we place inside the while loop will continue to happen as long as the horse’s distance to the fence’s top crossbar is greater than .5 meters.
6. Inside the while loop, drag the horse.gallop method.
Finally, under and outside of the do together control statement drag your horse.jump method. Your code should look like this:
Now, to run your new method, we could just test as before, using when the world starts, but instead, let’s make this method start when we hit the enter key on our computers:
1. Under Events (top center) select create new event->when key is typed
2. Change any key to enter
3. Now switch do Nothingto do horse.walkandjump
Finally, run your program by hitting Play. If you aren’t happy with the results, tweak them to your satisfaction.
Save this world. Make a back-up of it.
Part 1a:
Write a walkingmethod for your horse that is different from the gallopmethod. Now write a goaroundmethod that has the horse turn toward the fence, then go around the fence (directions are in Lab 2).
Now write a methodhorse.walkbackthat has the horse go around the fence (using the goaroundmethod) and then walk back to the point of origin. The easiest way to do this is to place a small object of some sort (preferably a bird or a balloon, because it will be floating in the air) where you first placed the horse, pretty much where Kelly is sitting on the horse, and then have the horse move toward that object until it is within a very small distance of it. It should use a while loop to walk the entire distance from the fence back to the point of origin.
Finally, add another event by using create new event->when key is typed. This time, select letter, then S. Now run your program. If you press the enter key, the horse should gallop toward the fence and jump over it. If you press the S key, the horse should go around the fence and walk back to the starting point. (Note that if you do these out of order, it causes problems. We haven’t added any checks yet).
So far so good. But what if the horse isn’t near the fence? It won’t need to go around the fence. We’ll need to modify our walkbackmethod by adding an if/else condition (from the bottom menu). Drag the if/else above the horse.goaround method call inside the walkback method. Select True. Change True to a<b (from the math function in the World object), with 1 < 2. Now change 1 to horse distance to fence.topCrossbar2. If this is true we want to do in order:
1. the horse goaroundmethod
2. the horse turntoface method(turning to face the object you inserted at place of origin)
3. ado-together control method that:
`a. horse.move toward the object (from 2). It should move the distance to the object.
`b. a while loop that continues while the horse’s distance to object > .25
the while loop contains the horse.walking method you wrote
You want the Else to contain everything from step 2 down.
Save and test. Now if you hit S twice in a row, does this work better?
To turn in: Your completed horse world.
Part 2: Kickball world
First, create a kickball world, with 4 bases placed randomly around the world. The bases can be anything you like (when I was a kid I made my little brother be a base). Add 2 players: a pitcher, and a kicker. Add a ball.
Now write a rotate method for the ball
Write a kick method for the kicker
Write a run method for the kicker
Write a jump method for the kicker
Write pitch method for the pitcher (s/he can pitch by kicking the ball instead of rolling it – I think making a pitcher bend at the waist and rolling the ball would be more complicated than pitching by kicking, but if you’d prefer s/he pitches by rolling the ball, then by all means write that pitching method)
You now have the basics written. Now write a method pitchto that starts with the ball orienting towards the kicker, then the pitcher pitching the ball and then the ball rolling (in a loop) towards the kicker, stopping when it hits the kicker’s foot.
Now write a kickandroll method that starts with the ball orienting away from the kicker, then having the ball’s kicker kick the ball (using the kick method you wrote), and then the ball rolling away (in a loop) for 20 meters.
Next write a runbases method that has the kicker run to first base (using the run method in a loop), jumping on first base for emphasis, then s/he runs to second base (in a loop) and jumps on that, then runs to third base and jumps on that, and then runs home and jumps on that.
Finally, write a kickandrun method that has the kicker kick the ball (using the kickandroll method) if the ball is touching his or her foot, and then run the bases using the
Now create an event such that when you hit the “p” key, the pitchto method is called. Create another event such that when you hit the “k”, the kickandrunmethod is called. Create another event such that when you hit “r”, the kicker just runs and jumps on the bases (for exercise). And create a final event such that when you hit “s” everything is reset to its original position (the ball returns to the pitcher, the pitcher orients toward the kicker (or home plate), and the kicker orients toward the pitcher).
To turn in:Your kickball world
Extra Credit: (10 pts): Create a Frisbee toss. Two players should be running towards one of two goals. They should be throwing the Frisbee back and forth as they run towards the goal. If one gets within a certain range of the goal, the player should throw the Frisbee into the goal. If the other one gets within a certain range of the goal, the other one should throw the Frisbee into the goal. (Note that the Frisbee should rotate flatly as it flys toward the players) The players should catch the frisbee and throw it as one method. Create an event button to start the program.