Introduction to Graphics in QBasic

In this section, we’ll delve into the exciting world of graphics programming using QBasic. One of the great advantages of using QBasic is its simplicity, which allows you to rapidly prototype and implement graphical programs. We will explore how to draw shapes, manipulate colors, and create visually engaging programs.

Setting Up Your Environment for Graphics

Before you start drawing shapes or playing with colors, ensure that you have your QBasic environment ready. If you haven’t already, install a QBasic interpreter like QB64 or the original QBasic DOS version. Once you have set it up, run the interpreter, and let's begin!

Entering Graphics Mode

First things first, to work with graphics in QBasic, you need to switch to graphics mode. You can do that using the SCREEN command. The most commonly used graphics modes in QBasic are:

  • SCREEN 12: This mode allows for a 640x480 resolution with 16 colors.
  • SCREEN 13: This mode is a popular choice as it supports 320x200 resolution with 256 colors.

You can switch to graphics mode as follows:

SCREEN 12

Now you are ready to start drawing!

Drawing Shapes in QBasic

Drawing Basic Shapes

QBasic provides several built-in functions to draw shapes. Here are some of the essential ones:

  • Circle: Draws a circle.
  • Line: Draws a straight line between two points.
  • Rectangle: Draws a rectangle or square.
  • Ellipse: Draws an ellipse or oval.

Drawing a Circle

To draw a circle, use the CIRCLE function. The syntax is:

CIRCLE (x, y), radius, color

Where x and y are the coordinates of the circle's center, radius is the radius of the circle, and color is the color code.

Example:

CIRCLE (320, 240), 50, 4 ' Draws a radius 50 circle centered at (320,240) in color code 4

Drawing a Line

You can draw a line using the LINE function:

LINE (x1, y1)-(x2, y2), color

Where (x1, y1) and (x2, y2) are the start and end points of the line.

Example:

LINE (100, 100)-(200, 200), 2 ' Draws a line from (100, 100) to (200, 200) in color code 2

Drawing a Rectangle

To draw a rectangle, use the RECTANGLE function:

LINE (x1, y1)-(x2, y2), color, BF ' BF fills the rectangle

Example:

LINE (150, 100)-(400, 300), 3, BF ' Draw a filled rectangle

All these functions allow you to create basic geometrical shapes, which forms the basis for more complex graphics.

Managing Colors

Colors play a crucial role in graphics programming. In QBasic, colors are represented by integers, ranging from 0 to 15 in many modes. You can define a color palette using the COLOR command.

Selecting a Color

To select a color for future drawing, use:

COLOR colorNumber

Example:

COLOR 2 ' Set the color to green
LINE (10, 10)-(100, 100) ' This line will now be green

Drawing with Different Colors

You can create visually appealing graphics by changing colors frequently in your drawings. For instance:

FOR i = 0 TO 15
    COLOR i
    CIRCLE (320, 240), 10 + (i * 2) ' Draws multiple circles with varying radius and color
NEXT

This loop will draw multiple circles with increasing radius and changing colors, showcasing the versatility of using colors in your graphics.

Advanced Graphics Techniques

Using PSET for Points

The PSET function allows you to plot individual pixels on the screen. The syntax is:

PSET (x, y), color

Where (x, y) are the coordinates of the pixel you want to set.

Example:

PSET (100, 150), 3 ' Set a pixel at (100, 150) to color code 3

Filling Shapes

To fill shapes (like rectangles and circles), you can use the BF keyword along with the LINE function. It stands for “border fill,” which fills the shape with the current foreground color.

Example:

LINE (50, 50)-(150, 100), 14, BF ' Draws a filled rectangle in color code 14

Drawing Ellipses

You can also draw ellipses with the ELLIPSE function:

ELLIPSE (x, y), xradius, yradius, color

Example:

ELLIPSE (320, 240), 100, 50, 5 ' Draws an ellipse centered at (320,240)

Creating a Simple Graphics Program

Now let’s put these principles into practice. We will create a simple program that animates the movement of a circle across the screen.

SCREEN 13
DO
    CLS ' Clear screen
    CIRCLE (x, 100), 30, 4 ' Draw circle
    x = x + 1 ' Move circle to the right
    IF x > 320 THEN x = 0 ' Reset position if it goes off-screen
    SLEEP 0.01 ' Control speed of animation
LOOP UNTIL INKEY$ <> "" ' Exit loop when a key is pressed

This program uses a simple DO...LOOP structure to animate a circle across the screen. Adjust the speed and movement parameters to see different results.

Conclusion

Working with graphics in QBasic is a delightful experience, allowing programmers to express their creativity. With the ability to draw shapes, manage colors, and create animations, you can develop various graphical applications. Experiment with the functions discussed to explore more complex graphics programming and take your QBasic skills to the next level!

Remember, the best way to learn is through practice, so try out different projects and challenge yourself to create something unique. Happy coding!