Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

A Horse Race Game


This carnival-style horse race is a variation of the horse race example presented in Chapter 8, but you can easily write the program here without having studied Chapter 8.) Unlike a traditional horse race, where horses run around an oval-shaped track and each horse breaks to the inside of the track, horses in a carnival game move straight ahead on a mechanical track. Start a new world and add an instance of the Race- HorseGame (Amusement Park) and three circles (Shapes). Position the circles near the start line of each racetrack, as shown in the initial scene below. In a carnival game, you win a prize if you pick the winning horse. In this game, the player will pick the horse by clicking one of the circles at the beginning of the track. For example, if the player clicks on the green circle, the player picks the horse in the nearest track. Create a class-level Boolean variable named isClicked for each of the circles. The isClicked variable should be false. When the player clicks on the circle, change isClicked to true and start the game. When the game begins, use a Do together to move all three racehorses forward a short random distance. This action should be repeated until one of the horses gets to the finish line and wins the race. You can use either a While loop (Chapter 7) or recursion (Chapter 8) to repeat the action until the race is over. An essential step is to know when the game is over. Write a function named isGameOver that asks: “Is a horse within 0.5 meters of the finish line?” We suggest 0.5 rather than 0 meters because the distance between the finish line and the horse is measured from the horse’s center, not from the tip of its nose. (You may need to adjust this distance, depending on how much you resized the raceHorseGame object in setting up the initial scene.) Write a function named whichHorseWon that returns the horse that is closest to the finish line. When the loop ends, determine whether the player picked the winning horse (use the circles’ isClicked variable). If the player has selected the winning horse, display the message “You won!” If not, display “Try again!” Because of the random distances used in this program, different horses should win the race if the program is run several times. Attachments Tags Clarifications