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

168 Part III: Advanced Programming with Liberty BASIC

NOMAINWIN

WindowHeight = 300

WindowWidth = 250

OPEN “Graphics window” FOR Graphics AS #main

PRINT #main, “HOME; DOWN; NORTH”

PRINT #main, “COLOR green”

PRINT #main, “GO 35; TURN 90; UP; GO 35; TURN 90”

PRINT #main, “COLOR pink”

PRINT #main, “DOWN; go 35”

PRINT #main, “FLUSH”

PRINT #main, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #main END

Drawing Circles

Because drawing individual lines can become tiresome, you may want to tell Liberty BASIC to draw circles instead. To draw a circle, you can use the CIRCLE command as in the following example:

PRINT #Windowhandle, “CIRCLE R”

Here’s what’s happening in this example:

1.The #Windowhandle portion defines the graphics window in which the circle that turtle graphics draws appears.

2.The CIRCLE R command tells the computer to draw a circle, at the current position of the turtle (pen), with a radius that R defines, where R is a number such as 35 or 90.

If you want to draw your circle in a specific color, you can use the COLOR command prior to the CIRCLE command, as follows:

PRINT #Windowhandle, “COLOR darkpink; CIRCLE R”

Chapter 12: Drawing Pictures and Making Noise 169

You can also fill in your circle with a specific color by using the BACKCOLOR command prior to using the CIRCLEFILLED command, as follows:

PRINT #Windowhandle, “BACKCOLOR yellow; CIRCLEFILLED R”

To see how turtle graphics can create circles, try the following program, which draws two circles, as shown in Figure 12-2:

Figure 12-2:

Drawing two circles using turtle graphics.

NOMAINWIN

WindowHeight = 300

WindowWidth = 250

OPEN “Graphics window” FOR Graphics AS #main

PRINT #main, “HOME; DOWN”

PRINT #main, “COLOR red; CIRCLE 40” PRINT #main, “PLACE 45 50”

PRINT #main, “COLOR darkblue; BACKCOLOR yellow; CIRCLEFILLED 40”

PRINT #main, “FLUSH”

PRINT #main, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #main END

170 Part III: Advanced Programming with Liberty BASIC

The seventh line in the preceding program uses the PLACE command, which moves the turtle (pen) position without drawing a line.

Drawing Boxes

Just as you can draw circles, Liberty BASIC also enables you to draw boxes.

To draw a box, you can use the BOX command as in the following example:

PRINT #Windowhandle, “BOX x y”

Here’s what’s happening in this example:

1.The #Windowhandle portion defines the graphics window where turtle graphics draws the box.

2.The BOX x y command tells the computer to draw a box where the current turtle (pen) position defines one corner of the box and the x and y coordinates define the location of the opposite corner.

If you want to draw your circle in a specific color, you can use the COLOR command prior to the BOX command, as follows:

PRINT #Windowhandle, “COLOR red; BOX x y”

You can also fill in your circle with a specific color by using the BACKCOLOR command prior to using the BOXFILLED command, as in the following example:

PRINT #Windowhandle, “BACKCOLOR pink; BOXFILLED x y”

To see how turtle graphics can create boxes, try the following program, which draws two boxes.

NOMAINWIN

WindowHeight = 300

WindowWidth = 250

OPEN “Graphics window” FOR Graphics AS #main

PRINT #main, “HOME; DOWN”

PRINT #main, “COLOR red; BOX 190 190” PRINT #main, “PLACE 45 50”

PRINT #main, “COLOR darkblue; BACKCOLOR pink; BOXFILLED 80 80”

Chapter 12: Drawing Pictures and Making Noise 171

PRINT #main, “FLUSH”

PRINT #main, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #main END

Displaying Text

Besides drawing lines, circles, and boxes, you can also create text in a graphics window. To display text in a graphics control, you just need to move the turtle (pen) to the location where you want the text to appear and then print the text with a backslash in front, which the following program accomplishes:

NOMAINWIN

WindowHeight = 300

WindowWidth = 250

OPEN “Text graphics window” FOR Graphics AS #main

PRINT #main, “HOME”

PRINT #main, “\This is an”

PRINT #main, “\example of text”

PRINT #main, “FLUSH”

PRINT #main, “trapclose [quit]”

WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #main END

The backslash character (\) displays the text and causes a new line to print, as shown in Figure 12-3. That way, you can display multiple lines of text without needing to move the turtle (pen) each time.

For variety, you can add color to your text or to the background. To change the color of your text, use the COLOR command, as follows: