Drawing Shapes in QBasic
In this article, we're diving straight into the world of graphical programming with QBasic! If you're excited to learn how to draw shapes on the screen using different colors and styles, you’re in the right place. Grab your keyboard and let’s start coding!
Setting Up the Graphics Mode
Before we can start drawing shapes, we need to switch to the graphics mode. QBasic provides various modes, but one of the most commonly used is mode 13h, which supports 256 colors and a resolution of 320x200 pixels. Here’s how to set it up:
SCREEN 13
Running this line of code at the start of your program will initialize the graphics mode. You’ll notice a colorful, often quite retro screen appearing when you execute it!
Choosing Colors
QBasic allows you to use a range of colors by specifying the color number. Here’s a quick breakdown of the color palette in mode 13h:
- 0: Black
- 1: Blue
- 2: Green
- 3: Cyan
- 4: Red
- 5: Magenta
- 6: Brown
- 7: Light Gray
- 8: Dark Gray
- 9: Light Blue
- 10: Light Green
- 11: Light Cyan
- 12: Light Red
- 13: Light Magenta
- 14: Yellow
- 15: White
You can use these color codes while drawing shapes, providing a wide color range for customization.
Drawing Basic Shapes
1. Drawing a Line
To draw a simple line, use the LINE statement. Here’s a basic example of drawing a line from point (100, 100) to (200, 200) in red:
LINE (100, 100)-(200, 200), 4 ' Draws a red line
2. Drawing a Circle
Drawing a circle is easy with the CIRCLE statement. Specify the center coordinates and the radius, along with the color. For example:
CIRCLE (160, 100), 50, 2 ' Draws a green circle
3. Drawing a Rectangle
You can create rectangles using the LINE statement again, but with two pairs of coordinates:
LINE (50, 50)-(150, 100), 5, BF ' Draws a filled blue rectangle
Here, BF stands for “Border Fill,” which fills the rectangle with the specified color.
4. Drawing a Filled Circle
To draw a filled circle, just include the BF parameter:
CIRCLE (250, 150), 30, 6 ' Draws a filled brown circle
5. Drawing Polygons
For more complex shapes, like polygons, you’ll use the PUT command to define points. Here’s a quick example of how you can draw a triangle:
DIM points(1 TO 3, 1 TO 2) ' Array for the triangle coordinates
points(1, 1) = 100: points(1, 2) = 100
points(2, 1) = 150: points(2, 2) = 50
points(3, 1) = 200: points(3, 2) = 100
FOR i = 1 TO 3
LINE (points(i, 1), points(i, 2))-(points((i MOD 3) + 1, 1), points((i MOD 3) + 1, 2)), 3 ' Draws the triangle with a magenta border
NEXT
Combining Shapes
One of the fun parts of drawing in QBasic is combining different shapes. Let’s create a face using circles and lines:
SCREEN 13
' Draw face outline
CIRCLE (160, 100), 50, 14 ' Yellow face
' Draw eyes
CIRCLE (140, 90), 5, 0 ' Left eye
CIRCLE (180, 90), 5, 0 ' Right eye
' Draw mouth
LINE (140, 110)-(180, 110), 0 ' Mouth
Clearing the Screen
To draw new shapes without old drawings interfering, clear the screen with:
CLS
Adding Interactivity
You can also add interactivity to your shapes. For instance, detect mouse clicks or key presses to draw shapes dynamically. Here’s a simple implementation that allows you to choose a color based on user input:
DIM userColor AS INTEGER
PRINT "Choose a color (0-15): ";
INPUT userColor
CIRCLE (320 / 2, 200 / 2), 60, userColor ' Draw circle with user-defined color
Saving Your Artwork
Once your masterpiece is finished, you might want to save it. Unfortunately, QBasic doesn’t directly support saving graphics to files, but you can achieve this by writing a routine that saves pixel data to an array and loads it back to draw.
Exiting Graphics Mode
Don’t forget to return to text mode after you finish your drawing project! Use this code before your program ends:
SCREEN 0
By switching back, you can ensure everything is clean and neat for any future programs.
Tips for Effective Drawing in QBasic
-
Plan Your Design: Take a moment to sketch out what you want to draw. Knowing where each shape will go can help a lot.
-
Experiment with Colors: Don’t be afraid to try different color combinations. You might stumble upon some visually appealing palettes.
-
Use Loops for Complex Shapes: If you’re drawing multiple instances of similar shapes, consider using loops to simplify your code and reduce repetition.
-
Maintain Aspect Ratio: Be aware of the resolution when drawing shapes. Some shapes may appear distorted if not proportionately sized.
-
Consider Frame Rate: If you animate shapes or perform frequent redraws, monitor the frame rate to ensure a smooth experience.
Conclusion
Drawing shapes in QBasic opens up a world of creativity and graphical programming fun. With simple commands and a bit of creativity, you can produce everything from basic figures to complex designs right on your screen. Keep practicing, and soon you’ll be crafting your own graphical projects in no time!
Have fun coding, and happy drawing!