QBasic For All 2D Graphics

Tutorial By Mallard
HTML by Anastasios Yalanopoulos

ANIMATED GRAPHICS IN QBASIC   a tutorial by Mallard

Note:- This tutorial focuses on EGA graphics. For information on VGA graphics, send e-mail to Mallard at:- mallard@gcomm.com

This tutorial is intended for intermediate programmers who are proficient with standard QBasic commands.

Table of contents:
  1. What can QBasic do?
  2. Using DATA to create graphics.
  3. GETting and PUTting graphics.
  4. So how does this work?
1. What can QBasic do?

QBasic was not meant to be a great environment for graphic-oriented applications. It was meant to be a learner's language for structured programming as well as basic programming techniques. However, using some of it's functions, we can achieve a level of graphics quality.
The best screen mode for this is the 320 x 200, 16-color SCREEN 7. While this may seem primitive, the mode is fast and has multiple "pages" to eliminate all flicker.
VGA graphics can be done, but without a very fast computer there is a lot of flicker.
Without using the methods of this tutorial, you could still use LINEs, CIRCLEs, and PSETs, but this method outlined is far faster, and better.
The methods explained in this document are MY OWN, developed by myself over the years.

2. Using DATA to create graphics

Graphics using this method are created beforehand using the GRAPHICS editor written by myself specifically for this tutorial, or using the DATA statement to read in color values. I'll start by explaining how to use the DATA statement to create graphics.

First, you have to start off by deciding how large your graphic is going to be. For instance, you might choose 10 pixels by 10 pixels. To make the graphic, start by creating a table like this:

DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00
DATA 00,00,00,00,00,00,00,00,00,00

Then, changing the QB editor from insert to overwrite using the INSERT key on your keyboard, change the color value of black (your back- ground color as 00) to the color you desire. The color table is as foll- ows:

00 - black
01 - dark blue
02 - dark green
03 - dark cyan
04 - dark red
05 - dark purple
06 - orange brown
07 - grey
08 - dark grey
09 - light blue
10 - light green
11 - light cyan
12 - light red
13 - magenta
14 - yellow
15 - bright white

For instance, to create a graphic of a fire, use this set of DATA statements:

DATA 00,00,00,04,00,00,04,00,00,00
DATA 00,00,04,04,00,04,04,04,00,00
DATA 00,00,04,04,04,04,04,04,00,00
DATA 00,04,04,12,12,04,04,04,00,00
DATA 00,04,04,12,12,04,04,00,00,00
DATA 00,04,04,12,12,04,04,00,00,00
DATA 00,04,12,12,12,04,04,04,00,00
DATA 00,04,12,14,14,12,04,04,00,00
DATA 04,04,12,14,14,14,12,04,00,00
DATA 04,04,12,14,14,14,12,04,00,00

Note that these DATA statements do nothing without using the method explained below to READ these color values into usable form. Before going on, however, try making some graphics on your own of varying sizes to try out using the following method.

3. GETting and PUTting graphics

Now you should have a set of DATA statements that you think will look like an object of some sort. What next? You'll have to use this routine to read the values into an array and then place them on the screen.
Make sure you've already used the SCREEN 7 statement first.

xlength = 10     'replace 10 with the length of your picture
ylength = 10     'replace 10 with the width of your picture

FOR y = 1 to ylength
  FOR x = 1 to xlength
    READ z
    PSET(x, y), z
  NEXT
NEXT

This should give you what your graphic looks like. If you're satisfied with it, go on. If not, alter it and try loading it again.
Now that you've got a good picture (the graphic in the top left corner of the screen), you'll need to put it into memory for repeated use so the program doesn't have to re-read the DATA each time you reproduce it. This saves a lot of computing power and time. To do this, you'll have to DIM an array and then GET the graphic into the array as shown:

DIM graphicname(xlength * ylength)   'replace graphicname as reqd.
GET (0,0)-(xlength, ylength), graphicname   'a rectangular capture
CLS       ' clears the screen

Okay! You've got your picture saved in the computer's temporary memory. To view the graphic, use the PUT statement like this:

'replace x & y with where you want to be appear m(& change graphicname)
PUT (x, y), graphicname

That's it for this section. You can now do simple graphics. For a good way to create smooth animation, read on to the next section.

4. So how does this work?

You now know how to create graphic data using the DATA statement, how to read that data and place it on the screen, and how to GET and PUT the graphic itself. However, creating graphics that move is a bit more involved.
What you'll have to do is set the number of *screen pages* you will be using. When you use the SCREEN 7 statement, you're only using one page (ie page 0). However, with modifications of the SCREEN 7 statement, you can create graphics on a different "page" and then copy them to the screen. This eliminates CLS'ing all the time.
Firstly, your SCREEN 7 statement should be changed to SCREEN 7,0,1,0. This means that you'll be using page 1 to do graphics and page 0 will be what the user sees. Once you've done this, all your graphics work will not be displayed to the user until you use the PCOPY 1,0 statement.
Here's an example of moving the graphic that you created earlier (see above) across the screen.

SCREEN 7,0,1,0   '- using page 1 as work page & page'0 as visual.
....         '- insert the code you used to create your graphic using the method above
CLS

WHILE x < 270   '(could use DO here with LOOP UNTIL x = 270 below)
PUT (x, 100), graphicname   '(replace graphicname as reqd)
PCOPY 1,0   'place the graphic on the screen
CLS
x = x + 1
WEND   '(could use LOOP UNTIL x = 270 here with DO above)

--oo0oo--