output commands in fig

this is part 3 of an introductory series on fig programming. for part 2, go here. for the next part, go here.

 

part 3: output commands in fig

 

while input commands get information from a device (or file, or connection, or from some part of the computer,) output commands send information to a device, or file, connection, or to some part of the computer.

obvious output devices are the screen and speakers. sending files to the printer counts as output, although the printer itself also sends information back to the computer, making it an input/output device.

a touchscreen is a separate device than the screen itself and an input device, although the display directly behind it is for output. these are technical distinctions and not always important, but are sometimes relevant to programming.

x  "hello there"  print

 

sets x to a string and sends x to stdout, which generally means the screen (or window.)

x  "hello there"  prints

 

like the previous line using print, except prints stays on the same line instead of advancing.

x   "text.txt"  open "w"
y   "ok"        fprint x
now x           close

 

you would normally find these used separately.

the first line sets x to a string containing the path and filename of the file to save information to; then it opens the file for writing (the “w”.)

the fprint command writes the value of the main variable to x, which is still holding the filename. in this case, the string “ok” is saved.

the close command will update / save / stop writing to a file that was opened before. it closes whatever file is specified by the main variable. dont close the file until youre done fprinting (each and every line you want to save) to it.

each time you open a file for writing, it starts writing at the beginning. python lets you “append” or add to a file, which could even be added as a feature to fig.

(it doesnt have that feature yet. if it did, you would open “a” for append, instead of “r” for read or “w” for write.)

if you need to, you can already add to a file this way:

  • open to an array using arropen
  • open again using open “w”
  • write the array with the existing information using fprint, before adding to it.

 

writing an array using fprint is possible by using a loop, or by turning the array into a string, using join. both loops and the join command are covered in later chapters.

most languages use a filenumber or handle to track an open file; which is a different thing than a string or a numeric variable. ive always thought that is weird or unfriendly for beginners.

for python coders and the extra curious: fig keeps the handles in a special type of array called a dictionary, with strings as the key to each handle. fig coders dont need to worry about any of that, so long as they make certain to use the same string (contents) to close or fprint that they used to open it.

in other words, if you use the string “../text.htm” with the open command, you will need exactly the string “../text.htm” to read or write or close that
file. it doesnt matter if it’s a constant (written out) or two variables holding the same value, as long as theres a perfect match.

for what its worth, ending the program with end or system (or just letting the program stop normally) is supposed to close all the files that are still open.

x  cls

 

clears (and goes to the top left of) the screen.

x  display

 

the first time this is called, it turns off graphics auto-updating (the default is to update every time you use a graphics command.)

if already called once, display updates the screen with any graphics that were drawn to it. this only affects the graphics windows, not “text-mode” graphics.

textmode

 

forces text-mode graphics– or dont try to display graphics commands in a graphics window; display them using ansi and unicode text codes.

graphics

 

the default mode; open a graphics window when any graphics commands are used. if the graphics window fails (perhaps pygame is not installed) then fig falls back to text-mode graphics.

if textmode was used explicitly, the graphics command turns real graphics back on.

x  line 5 7  10 20  14

 

draws a line from point (5, 7) to point (10, 20) in yellow– where (5, 7) is the 5th dot horizontally and the 7th pixel vertically.

x  pset 5 7 10

 

draws a point at (5, 7) in light green.

x  locate row column

 

move to (row, column) on text screen.

x  colortext 5

 

changes text color to magenta:

0 black         8 grey
1 blue          9 l. blue
2 green         10 l. green
3 cyan          11 l. cyan
4 red           12 l. red
5 magenta       13 pink
6 brown         14 yellow
7 white         15 b. white

 

x highlight 0

 

changes text background to black:

0 black         4 red
1 blue          5 magenta
2 green         6 brown
3 cyan          7 white

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s