Input and Output Tutorial II

In the last tutorial you learned the very basics of input and output in FreeBASIC. In this tutorial, you will learn how to make your output a bit less dull by using some techniques to colorize the text.

FreeBASIC Input and Output Tutorial II

You already know the basics of input and output. So far you've only learned how to output basic, dull text to the screen. Even most text games are not this dull. So, let's learn to put some color in the output.

Color

The color function changes the color of the text output. It can change both the foreground color and the background color. The foreground color is the color of the text itself. The background color is the color of the text background. For an example, try this code :

color 12,15
print "DANGER!"
sleep
end

Notice how only the background of the text "DANGER" is white. This is what is meant by "background color".

Foreground Color

The two numbers used with color set which foreground color and background color you want to use. FreeBASIC, in the default console mode that we are using only has 16 (in other modes it has more). Each color is numbered 0 to 15.

The first number is the foreground color and you do not have to include a background color if you only want to change the text color.

color 10
print "This is green!"
sleep
end

Background Color

The background color of the text can be changed independently as well. However, you need to include a comma to tell FreeBASIC that you are not including the first number that represents the foreground color.

color ,5
print "purple background."
sleep
end

Multicolored Text

You can change only parts of the text on a line by changing the color between chunks.

hitPoints = 1
print "You have";
color 12
print hitPoints;
color 7
print " hitpoint left."
sleep
end

Clearing The Screen

Sometimes you'll have too much leftover output on the screen that you will want to clear it away. To do this, use cls.

print "Press any key to clear the screen."
sleep
cls
sleep
end

cls and the Background Color

cls clears the screen using whatever the background color is. If we set the background color to white, then clear the screen with cls, the entire screen will be white.

color ,15
cls
sleep
end

You should note that when the text scrolls, the white background will be replaced by black, so this isn't quite as useful as it should be.

Conclusion

With what you know now, you can add a bit of color to your output. However, it isn't very well organized. In the final input and output tutorial, you will learn to organize your output and make it more readable.

page tags: input output tutorial
page_revision: 1, last_edited: 1208862712|%e %b %Y, %H:%M %Z (%O ago)