Page 1 of 1

Write Functions (Use color in console applications!)

Posted: Tue Jun 05, 2018 11:15 pm
by NTech
Here are functions that allow the use of color in simulated 'console' graphic windows. These functions can be used to create a console application that has all sorts of colors - even colors in the middle of a string. Run the sample code to see a demo of everything.

Code: Select all


'ADD THIS TO YOUR CODE - Startup Variables:
global x,y

x = -6
y = 10
'------------------------------------------

nomainwin

WindowWidth = 800
WindowHeight = 600

open "colors" for graphics as #g

#g, "backcolor black;fill black;discard"

'Example of Write()
a=Write("Hello!","red")
'------------------

'Example of Read$()
name$=Read$("green")
'------------------

'Example of WriteLine$()
a=WriteLine("Ok, ";name$;" now you have to do something.","red")
a=WriteLine("Please press type your name then '+' to exit","red")
'-----------------------

'Example of ReadLine$()
a$=ReadLine$("green")
a=WriteLine("I could have guessed it was you, ","red") : a=Write(a$,"green") : a=Write(", anytime!","red") 'The power of nested Write() statements = colors in-line!
a=WriteLine("Please type '+' to exit","red")
a$=ReadLine$("green")
'----------------------

'Example of Clear()
a=Clear()
'------------------

close #g
end

wait

function Write(string$, color$)
    CharWidth = Char.Width()
    CharHeight = Char.Height()

    for i = 1 to len(string$)
        x = x + CharWidth
        #g, "font courier_new 0 16;";" ";"color ";color$;";goto ";x;" ";y;";down;\";mid$(string$,i,1) : #g, "up;flush"
    next i

end function

function WriteLine(string$, color$)
    CharWidth = Char.Width()
    CharHeight = Char.Height()

    x = -6
    y = y + (CharHeight*2)

    for i = 1 to len(string$)
        x = x + CharWidth
        #g, "font courier_new 0 16;";" ";"color ";color$;";goto ";x;" ";y;";down;\";mid$(string$,i,1) : #g, "up;flush"
    next i

end function

function Read$(color$)
    CharWidth = Char.Width()
    CharHeight = Char.Height()

    nx = x
    ny = y

    #g, "when characterInput [key];setfocus"
    wait

    [key]
    if Inkey$ = "+" then
        ReadLine$ = np$
        exit function
    end if

    np$ = np$ + Inkey$

    nx = x
    ny = y

    for i = 1 to len(np$)
        nx = nx + CharWidth
        #g, "font courier_new 0 16;";" ";"color ";color$;";goto ";nx;" ";ny;";down;\";mid$(np$,i,1) : #g, "up;flush"
    next i

    exit function

end function

function ReadLine$(color$)
    CharWidth = Char.Width()
    CharHeight = Char.Height()

    x = -6
    y = y + (CharHeight*2)

    nx = x
    ny = y

    #g, "when characterInput [key];setfocus"
    wait

    [key]
    if Inkey$ = "+" then
        ReadLine$ = np$
        exit function
    end if

    np$ = np$ + Inkey$

    nx = x
    ny = y

    for i = 1 to len(np$)
        nx = nx + CharWidth
        #g, "font courier_new 0 16;";" ";"color ";color$;";goto ";nx;" ";ny;";down;\";mid$(np$,i,1) : #g, "up;flush"
    next i

    #g, "when characterInput [key];setfocus"
    wait

end function

function Clear()
    x = -6
    y = 10

    #g, "fill black;flush"
end function

function Char.Width()
    Char.Width = 8
end function

function Char.Height()
    Char.Height = 7
end function