So far, we've only been dealing with text (words and numbers).
How do we do pictures in QBASIC? First, we need to
use SCREEN to change from text mode to graphics mode.
SCREEN
SCREEN lets you select a "graphics" screen instead of the
"text" screen we've been using. This will let you draw
pictures. In the next program, we'll use DRAW to draw a
square on the screen in SCREEN 12 graphics mode.
SCREEN 12 CLS DRAW "D100 R100 U100 L100"
There are many other SCREEN numbers you can use, but 12
is probably the easiest to work with. It gives you a lot of
space and the color numbers are familiar. QBASIC Help explains
all the possible values of SCREEN. You can always try them
and see what happens.
DRAW
DRAW is kind of like the turtle in the programming language Logo. With
DRAW, you can move around the screen and draw lines along the
way. In the above example we used the following DRAW commands:
- D100 - Go down 100 units
- R100 - Go right 100 units
- U100 - Go up 100 units
- L100 - Go left 100 units
DRAW can do a lot more than that. It is like PLAY. It's
a small programming language inside of QBASIC.
Look at QBASIC Help for
a complete description of everything it can do. Here's a
filled in box:
SCREEN 12 CLS DRAW "C15 D100 R100 U100 L100 BF1 P15,15"
"C15" sets the color to bright white. "BF1" moves into the square,
then "P15,15" fills it with
bright white. Finally, here's something very Logo-like:
SCREEN 12 CLS FOR I = 0 TO 360 STEP 10 DRAW "D100 R100 U100 L100 TA" + STR$(I) NEXT I
"TA" means to turn to a specific angle. STR$ converts the
value in I
to a string. This lets DRAW turn to the angle in the
variable I. It's not quite as easy as Logo, but it's still pretty
impressive.
LINE
QBASIC also lets you draw using a coordinate system. It's
like drawing graphs on graph paper. Try this:
SCREEN 12 CLS LINE (0, 0)-(320, 240), 15
LINE lets you draw a line between two points. The points
are specified in (x, y) coordinates. You may have seen this
when learning about graphs in math class. In QBASIC, the
coordinates are almost the same. The only thing that is
different is the Y coordinate. In QBASIC, the Y coordinate
is upside down. "0" is at the top, and bigger numbers go
toward the bottom of the screen.
"LINE (0, 0)-(320, 240), 15" draws a line starting at
coordinate
(0, 0) which is the upper left corner of the screen. The line
ends at (320, 240) which is the center of the screen. The
last number is the color (15 which is bright white).
Box
By adding a "B" to the end of a LINE statement, you can
draw a box. Try this:
SCREEN 12 CLS LINE (0, 0)-(320, 240), 15, B
The first coordinate is the upper left corner while the
second coordinate is the lower right.
Try "BF" instead of "B". Interesting?
CIRCLE
QBASIC can also draw circles using the CIRCLE statement:
SCREEN 12 CLS CIRCLE (320, 240), 100, 15
The coordinate (320, 240) tells the computer where to
put the center of the circle. "100" is the radius, or how
big the circle will be. "15" is the color number (bright
white again).
PAINT
Notice how that circle was not filled in. LINE has a "BF"
option that will let us fill in the boxes it draws. CIRCLE has
no such option, so we have to use PAINT:
SCREEN 12 CLS CIRCLE (320, 240), 100, 15 PAINT (320, 240), 15, 15
PAINT fills an area with a color. It stops painting when it runs
into a certain color on the screen.
The coordinate (320, 240) tells PAINT where to start filling
in, and the first "15" tells PAINT to use bright white as the paint
color. The second "15" tells PAINT to stop painting when it runs into
anything that is bright white.
Circle Art
Concentric circles are very easy to draw:
SCREEN 12 CLS FOR I = 5 TO 200 STEP 5 CIRCLE (320, 240), I, 15 NEXT I
With CIRCLE, PAINT and some random numbers, we can
make some interesting pictures:
SCREEN 12 CLS FOR I = 1 TO 50 X = INT(RND * 640) Y = INT(RND * 480) R = INT(RND * 100) Color1 = INT(RND * 16) CIRCLE (X, Y), R, Color1 PAINT (X, Y), Color1, Color1 NEXT I
Source: http://jpsor.ucoz.com |