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

162 Part III: Advanced Programming with Liberty BASIC

To create a separate graphics window, just use the OPEN command, as in the following example:

OPEN “My Drawing Area” FOR Graphics AS #graphWin

The main difference between a graphics window and an ordinary window is that a graphics window displays graphics (hence its name) while an ordinary window displays text.

If you don’t want to create a separate graphics window, you can create a graphics box inside an ordinary window. To create a graphics box inside a window, you need to use the following two commands:

GRAPHICBOX #main.graph, 10, 10, 150, 150

OPEN “Ordinary Window” FOR Window AS #main

The GRAPHICBOX command works as follows:

GRAPHICBOX #Windowhandle.boxname, xpos, ypos, width, height

Here’s what’s happening in that command:

1.The GRAPHICBOX command tells the computer to create a box for displaying graphics. This box appears inside the window that the

#Windowhandle name identifies.

2.The boxname portion identifies this graphic box with a unique name.

3.The xpos and ypos variables define the X and Y position of the graphic box, measuring from the upper-left corner of the window that #Windowhandle identifies.

4.The width and height variables measure the width and height of the graphics box.

After you create a graphics window or graphic box, you’re ready to start giving commands to display graphics on-screen.

Using Turtle Graphics

Turtle graphics get their name from the idea of putting an imaginary robotic turtle with a pen in the middle of your screen. To draw something, your program must tell the robotic turtle when to put its pen down and when to move in a certain direction to draw a line. To stop drawing, you must tell the robot to lift up its pen.

Chapter 12: Drawing Pictures and Making Noise 163

The LOGO programming language uses the idea of turtle graphics extensively to teach children the fundamentals of programming.

The four basic commands for drawing pictures by using turtle graphics in Liberty BASIC (or any other programming language that allows turtle graphics) are as follows:

1.Lift up the pen from the paper (to stop drawing).

2.Lower the pen down to the paper (to start drawing).

3.Move forward a distance that you specify (which draws a line if the pen is down).

4.Turn a number of degrees that you specify in any direction.

By stringing together a list of commands for moving, turning, lifting, and lowering the pen, you can make the imaginary robotic turtle on-screen draw a variety of interesting designs ranging from single lines and rectangles to geometric patterns and shapes.

Liberty BASIC includes special commands for creating turtle graphics, as follows:

UP: Lifts the pen up (don’t draw).

DOWN: Lowers the pen down (draw).

HOME: Moves the turtle (pen) in the center of the graphics area.

GO: Moves forward in the current direction.

GOTO: Goes to a position that you specify. This draws a line if the pen is down.

PLACE: Goes to a position that you specify but without drawing, even if the pen is down.

TURN: Turns the turtle clockwise a specific number of degrees.

NORTH: Causes the turtle to point north (straight up).

POSXY: Returns the X and Y coordinates of the turtle’s current location.

To see how turtle graphics work, try the following program to draw a flag (or the number 4, depending on your perspective) in the middle of the screen, as shown in Figure 12-1:

164 Part III: Advanced Programming with Liberty BASIC

Figure 12-1:

Using turtle graphics to draw a simple design.

NOMAINWIN

WindowHeight = 300

WindowWidth = 250

OPEN “Graphics window” FOR Graphics AS #main

PRINT #main, “HOME”

PRINT #main, “DOWN”

PRINT #main, “NORTH”

PRINT #main, “GO 35”

PRINT #main, “TURN 225”

PRINT #main, “GO 25”

PRINT #main, “TURN 225”

PRINT #main, “GO 20”

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

Here’s what’s happening in the preceding program:

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

2.The second and third lines define the height (300) and width (250) of the window that the fourth line creates.

Chapter 12: Drawing Pictures and Making Noise 165

3.The fourth line of code creates a graphics window containing the text Graphics window in the title bar. The window handle of #main identifies this window.

4.The fifth line of code moves the turtle to the home position, which is in the center of the window.

5.The sixth line tells the turtle to put the pen down and get ready to start drawing.

6.The seventh line tells the turtle to point north, which is up.

7.The eighth line tells the turtle to move 35 pixels in the direction the turtle is currently facing, which is north or up.

8.The ninth line tells the turtle to turn 225 degrees to the right.

9.The tenth line tells the turtle to go 25 pixels forward.

10.The eleventh line tells the turtle to turn 225 degrees to its right.

11.The twelfth line tells the turtle to go 20 pixels forward.

12.The thirteenth line gives the turtle the FLUSH command, which is a special command for making sure that the turtle graphics remain in the window even if the user resizes or moves the window.

13.The fourteenth line uses the trapclose command to tell the computer that after the user closes the program, the computer needs to find instructions to shut down at the [quit] label.

14.The fifteenth line tells the computer to wait for the user to do something.

15.The sixteenth (the one with [quit]) through nineteenth lines tell the computer what to do after the user closes the program. In this case, a Confirm dialog box appears to make sure that the user really wants to quit. If so, the program closes the window that the #main handle identifies.

16.The twentieth line marks the end of the program.

If you don’t use the flush command, your turtle graphics may disappear completely if the user resizes the graphics window.

Rather than type separate turtle graphics commands on each line, you can cram together multiple turtle graphics commands by separating them with a semicolon. Typing these commands takes up a lot of space, as the following example demonstrates:

PRINT #main, “HOME”

PRINT #main, “DOWN”

PRINT #main, “NORTH”

PRINT #main, “GO 35”

PRINT #main, “TURN 225”