Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

172 Part III: Advanced Programming with Liberty BASIC

PRINT #main, “HOME”

PRINT #main, “COLOR red”

PRINT #main, “\This is an”

PRINT #main, “\example of text”

PRINT #main, “FLUSH”

The second line in the preceding example tells the computer to display text in red.

If you want to change the backdrop on which your text appears, use the

BACKCOLOR command instead, as follows:

PRINT #main, “HOME”

PRINT #main, “BACKCOLOR red”

PRINT #main, “\This is an”

PRINT #main, “\EXAMPLE OF TEXT”

PRINT #MAIN, “FLUSH”

The second line in the preceding example displays text against a red background.

Figure 12-3:

Displaying text in a graphics window by using turtle graphics.

Making Sounds

Many programs use sound for a variety of reasons, such as alerting a user that something’s gone wrong or playing soothing music in the background while the program’s running. Sound can make your program more interesting to use.

Chapter 12: Drawing Pictures and Making Noise 173

Making a beeping noise

At the most primitive level of sound-making, Liberty BASIC can make a simple (and annoying) beeping noise by using a special BEEP command as follows:

PROMPT “How many beeps do you want to hear”; Answer

FOR I = 1 TO Answer

BEEP

NEXT

END

You often use the BEEP command as a warning message to the user, such as if the user presses the wrong key or tries to choose an invalid command.

Playing WAV files

Because the BEEP command is simplistic and relatively limited, Liberty BASIC also provides the PLAYWAVE command, which can play WAV files that you can download off the Internet or record on your own computer.

Windows includes several WAV files in the C:\Windows\Media directory.

To use the PLAYWAVE command, you need to specify which WAV file you want to play and how you want it to play. The three choices for playing a WAV file are as follows:

SYNC: Temporarily halts your program until the WAV file finishes playing.

ASYNC: Enables your program to continue running while the WAV file continues playing.

LOOP: Plays the WAV file over and over again until your program gives the computer the PLAYWAVE “” command to shut the WAV file off.

The PLAYWAVE command looks as follows:

PLAYWAVE filename, mode

So if you want to play the tada.wav file that you store in the C:\Windows\ Media directory, you can use the following command:

PLAYWAVE “C:\Windows\Media\tada.wav”, SYNC

In this example, the tada.wav file plays, temporarily halting your program until the WAV file finishes playing.

174 Part III: Advanced Programming with Liberty BASIC

Make sure that you specify the correct directory where the PLAYWAVE command can find your WAV file.

To see how a real Liberty BASIC program may work, try the following on your own computer:

NOMAINWIN

FILEDIALOG “Pick a .WAV file to play.”, “*.wav”, filename$

PLAYWAVE filename$, SYNC

END

The following steps tell you how the preceding program works:

1.The first line tells Liberty BASIC not to display the main window.

2.The second line displays a dialog box, enabling the user to choose a WAV file to play.

3.The third line plays the WAV file that the user chooses from the dialog box that appears as a result of Line 2 of the program.

4.The fourth line ends the program.