Developing the Crazy Eight Ball Game

Now it is time to turn your attention to the development of a new Scratch application, the Crazy Eight Ball game. This game simulates the operation of a crazy eight ball fortune-telling toy. As you work on the development of this game, you will get additional experience with different looks code blocks. In total, the application is made up of three sprites and three scripts. Figure 10.12 shows how the game looks when first started.

To play the game, think of a question and then click on the image of the cat located in the center of the eight ball. Once clicked, the image of the cat is replaced with an 8, as demonstrated in Figure 10.13, and over the next four seconds, the sounds of bubbles can be heard.

Figure 10.13. It takes a few moments for the crazy eight ball to come up with an answer.


The crazy eight ball displays any of five randomly selected answers in response to player questions. The range of answers supported by the game includes:

  • Maybe!

  • No!

  • Yes!

  • Ask a different question!

  • Maybe... but then maybe not!

Figure 10.14 shows how the game looks once it has finally decided on an answer to the player’s question.

Figure 10.14. The crazy eight ball has decided not to answer the player’s question.


The development of this application project will be created by following a series of steps, as outlined here:

1.
Creating a new Scratch application project.

2.
Adding and removing sprites.

3.
Adding the variable needed by the application.

4.
Adding an audio file to the application.

5.
Adding a script to control the display of the 8 in the eight ball.

6.
Adding the programming logic required to operate the eight ball.

7.
Saving and executing your work.

Step 1: Creating a New Scratch Project

Begin the creation of the Crazy Eight Ball game by creating a new Scratch project. The easiest way is to start Scratch, which automatically creates a new application project. Alternatively, if Scratch is already open, create a new application by clicking on the New button located on the Scratch menu bar.

Step 2: Adding and Removing Sprites

The Crazy Eight Ball game consists of three sprites and three scripts, as shown in Figure 10.15.

Figure 10.15. An overview of the different components that make up the Crazy Eight Ball game.


The first sprite that you need to add to the game is that of an empty eight ball. The second sprite is that of a number 8. You will find copies of graphics for both of these sprites located on this book’s companion CD-ROM. You can add these sprites to your new Scratch application by clicking on the Choose New Sprite from File button and then selecting these files. Alternatively, you can create them yourself by clicking on the Paint New Sprite button and then using the Paint Editor program. Once added to the stage, reposition these two sprites so that the eight ball is centered in the middle of the stage and the number is centered in the middle of the eight ball.

The application’s third sprite is that of a cat’s face. You can create this sprite by using the Paint Editor program to edit the application’s default sprite, removing the Cat sprite’s body, leaving just its face in place. Once modified, click on the Grow Sprite button located on Scratch’s toolbar and then click on the image of the Cat sprite 12 times to increase the size of the cat’s face. Next, reposition the Cat sprite, moving it onto the center of the eight ball so that it overlaps the Cat sprite. At this point, the overall design of the Crazy Eight Ball game is complete.

Before moving on to the next step, rename these three sprites Cat, EightBall, and Number, as shown in Figure 10.15.

Step 3: Adding a Variable Required by the Application

In order to execute, the Crazy Eight Ball game requires the definition of the variable shown in Figure 10.16. To add this variable, click on the Variables button located at the top of the blocks palette, click on the Make a Variable button, and create a new variable named RandomNo.

Figure 10.16. The Crazy Eight Ball game requires one variable.


This variable will be used to store a randomly generated number that the game will use when generating answers to player questions.

Step 4: Adding an Audio File to the Application

The Crazy Eight Ball game makes use of a single sound effect, which sounds like bubbles being blown in water. This sound is played for four seconds preceding the display of the eight ball’s answer. The audio file that is played must be added to the Cat sprite. To add this sound file, select the Cat sprite thumbnail in the sprite list and then click on the Sounds tab located at the top of the scripts area. Next, click on the Import button to display the Import Sound window, double-click on the Effects folder, select the Bubbles audio file, and then click on OK.

Step 5: Creating a Script to Control the Display of the 8 in the Eight Ball

Of the application’s three scripts, two belong to the Number sprite. These scripts, shown next, are automatically executed based on the receipt of broadcast messages. Specifically, when a message of Show 8 is received, the Eight sprite is made visible. When the message Hide 8 is received, the Eight sprite is hidden. The receipt of these messages serves as triggers, which control when the Eight sprite is visible (which only occurs when the eight ball is in the process of preparing to generate an answer).

As you can see, these two scripts each use a looks code block to control sprite visibility. Since the game begins by displaying only the image of the Cat sprite, go ahead and click on the second script belonging to the Eight sprite, hiding it from view.

Step 6: Adding the Programming Logic Needed to Control the Eight Ball

The last script in the application, shown next, belongs to the Cat sprite. It is executed whenever the player thinks of a question and clicks on the Cat sprite for an answer.

Once started, the script begins by assigning a random number in the range of 1 to 5 to the RandomNo variable. Next, a looks code block is executed, hiding the Cat sprite and then the broadcast message Show 8 is sent. This message will trigger the execution of a script belonging to the Eight sprite. Next, the Bubbles audio file is played, and the script’s execution is paused for four seconds, allowing Scratch time to finish playing the audio file. Once the four seconds is up, a second broadcast message of Hide 8 is sent, triggering the hiding of the Eight sprite.

Next, the Cat sprite is redisplayed on the stage and the value assigned to RandomNo is analyzed. Depending on the value assigned to RandomNo, one of five different text messages is displayed in a speech bubble. After two seconds, the bubble is closed, and the game waits on the player to ask another question.

Step 7: Saving and Executing Your Scratch Project

At this point, you have all of the information you need to create your own copy of the Crazy Eight Ball game. As long as you followed along carefully with the instructions provided in this chapter, you should not run into any problems. If you have not done so yet, save your new Scratch application project and then switch over to Presentation mode and test it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.206