Post by nthrilla on Aug 10, 2024 17:57:06 GMT
Hello,
I've always though the basic print command in qbasic would be better if you could add color tags inside the text string to change the foreground and background color on the fly.
the sub 'P' allows you to do this by using "#(2 digit text color)(2 digit background color)"
so a gray text on black background would use #0700. The program is set up to look for the # and then grab the two colors directly after it. There may be a better way to do this, but simply enough, I was able to get it to work using this method.
The 'CP' routine allows text to center print to the screen.
I've always though the basic print command in qbasic would be better if you could add color tags inside the text string to change the foreground and background color on the fly.
the sub 'P' allows you to do this by using "#(2 digit text color)(2 digit background color)"
so a gray text on black background would use #0700. The program is set up to look for the # and then grab the two colors directly after it. There may be a better way to do this, but simply enough, I was able to get it to work using this method.
The 'CP' routine allows text to center print to the screen.
DECLARE SUB p (p.x, p.y, string.txt$)
DECLARE SUB cp (cp.x, p.string.txt$)
CLS
text$ = "#1500T#0700he #1500Q#0700uick #0400B#1200rown #0700Fox #2000J#2100u#2200m#2300p#2400e#2500d #0700Over The #0500Lazy #0500Sleeping #0100D#0200o#0300g"
cp 6, text$
SUB cp (cp.x, p.string.txt$)
string.length = LEN(p.string.txt$) 'length of string!
ticks = 1 'advances through the string variable one char at a time
tmp.string$ = ""
DO
IF MID$(p.string.txt$, ticks, 1) = "#" THEN ticks = ticks + 5 'skip color info
tmp.string$ = tmp.string$ + MID$(p.string.txt$, ticks, 1) 'add non color char to temporary string to determine length
ticks = ticks + 1
LOOP UNTIL ticks = string.length + 1
'determine center location on screen with color tags removed
center.y = INT(80 / 2 - LEN(tmp.string$) / 2)
p cp.x, center.y, p.string.txt$
END SUB
SUB p (p.x, p.y, string.txt$)
'sub routine that looks for meta tag and changes color of text
'on the fly.
'Syntax: #(2 digit text color)(2 digit background color)
'#0700 is gray text on a black background
string.length = LEN(string.txt$) 'length of string!
ticks = 1 'advances through the string variable one char at a time
string.y = 1 'location on screen not dependent on ticks
'p.y can be set to 0 since string.y starts at 1
fcolr = 7 'text color
bcolr = 0 'background color
DO
IF MID$(string.txt$, ticks, 1) = "#" THEN 'looks for # and puts color
fcolr = VAL(MID$(string.txt$, ticks + 1, 2)) 'info into fcolr and bcolr
bcolr = VAL(MID$(string.txt$, ticks + 3, 2))
ticks = ticks + 5
END IF
COLOR fcolr, bcolr: LOCATE p.x, p.y + string.y: PRINT MID$(string.txt$, ticks, 1);
ticks = ticks + 1
string.y = string.y + 1
LOOP UNTIL ticks = string.length + 1
END SUB