Creating a Night Star Shape in QBasic

 Creating a  Night Star Shape in QBasic

QBasic, a programming language renowned for its simplicity and versatility, allows enthusiasts to unleash their creativity. In this blog post, we'll explore the fascinating world of graphics programming in QBasic by creating a vibrant and colorful star shape. Follow along as we combine basic programming principles with artistic flair to bring a stunning starry night to your QBasic canvas.


Setting the Stage:
Before diving into the code, ensure you have QBasic installed on your system. Open the QBasic editor and set the graphics mode to a mode that supports color. For example, use `SCREEN 13` for a resolution of 320x200 with 256 colors.

Creating the Star:
Let's start by drawing a simple star using lines. Add the following code to your QBasic editor:

```qbasic
SCREEN 13 ' Set graphics mode with 256 colors

CLS ' Clear screen

' Coordinates for the star
x = 160
y = 100

' Loop to draw the star
FOR i = 0 TO 360 STEP 72
    ANGLE = i * (PI / 180) ' Convert angle to radians
    LENGTH = 50 ' Length of the star's rays
    x1 = x + LENGTH * COS(ANGLE)
    y1 = y - LENGTH * SIN(ANGLE)
    DRAW "LINE (x, y)-(x1, y1), " + STR$(i \ 72 + 1), RGB(i \ 72 + 1)
NEXT i

DO
LOOP UNTIL INKEY$ <> "" ' Wait for a keypress before exiting
```

Understanding the Code:

1. `SCREEN 13`: Sets the graphics mode with 256 colors.
2. `CLS`: Clears the screen.
3. `FOR i = 0 TO 360 STEP 72`: Iterates through angles to draw the star with five rays.
4. `ANGLE = i * (PI / 180)`: Converts angles to radians for trigonometric calculations.
5. `x1` and `y1`: Calculate the endpoint of each star ray.
6. `DRAW "LINE (x, y)-(x1, y1), " + STR$(i \ 72 + 1), RGB(i \ 72 + 1)`: Draws a line with a color based on the ray number.

Customization:
Feel free to experiment with the star's size, position, and the number of rays. You can also change the colors by modifying the `RGB` function parameters. For instance, replace `RGB(i \ 72 + 1)` with `RGB(i \ 36, i \ 2, i \ 4)` for a more dynamic color palette.
Congratulations! You've created a colorful star shape in QBasic. This basic graphics programming adventure can serve as a foundation for more complex and visually appealing projects. Use your newfound skills to explore the vast possibilities of artistic expression within the QBasic environment. Happy coding!

No comments:

Post a Comment