Print: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Nucleartide
m (Add mention of `?` alias for `print`.)
imported>Thisismypassphrase
(From changelog)
 
(16 intermediate revisions by 5 users not shown)
Line 2: Line 2:
|name=print
|name=print
|shortdesc=Prints a string of characters to the screen.
|shortdesc=Prints a string of characters to the screen.
|str||The string of characters to print.
|text||The Lua string of characters to print.
|x|optional|The x coordinate of the upper left corner to start printing.
|x|optional|The x coordinate of the upper left corner to start printing.
|y|optional|The y coordinate of the upper left corner to start printing.
|y|optional|The y coordinate of the upper left corner to start printing.
|col|optional|The color to use for the text.
|color|optional|The color to use for the text.
|result=The x coordinate of the next character to be printed (can be used to calculate printed width)
}}
}}
The <code>print</code> function writes text to the screen using the Pico-8 font.
The <code>print</code> function writes a line of text to the screen using the PICO-8 font, and then moves the cursor to the start of the next line. To write multiple lines in one call, insert one or more newline characters (<code>'\n'</code>) in the text.


Given only a Lua string, <code>print</code> uses the cursor location and draw color of the current [[DrawState|draw state]]. The cursor position is moved to the next line, potentially scrolling the entire display up by a line. This form of <code>print()</code> behaves line a scrolling display of text.
If only the <var>text</var> argument is supplied, <code>print()</code> uses the cursor position and draw color from the current [[DrawState|draw state]] to determine where to draw the text, and what color to use.


<pre>
Unless specific <var>x</var> and <var>y</var> position coordinates are supplied, <code>print()</code> behaves like a retro text-based scrolling display. The <var>text</var> is rendered to the screen and then the cursor moves to the left margin on the next line, as if <var>text</var> had a hidden newline character at the end. If there isn't enough room to have another line on the screen, the screen scrolls upwards to make enough.
 
<syntaxhighlight lang="lua">
print('you are standing at the end of')
print('you are standing at the end of')
print('a road before a small brick')
print('a road before a small brick')
print('building.')
print('building.')
</pre>
</syntaxhighlight>
 
 
The three additional arguments are all optional, and can be supplied in three ways:
 
If there is only ''one'' additional argument, it is assumed to be the <var>color</var> argument. The text is printed in the same manner as above, but in the given <var>color</var>. This also changes the current draw color of the draw state.
 
<syntaxhighlight lang="lua">
print('press x to start', 7)        -- white
</syntaxhighlight>
 
 
If there are ''two'' additional arguments, they are assumed to be the <var>x</var> and <var>y</var> arguments, which are used as the upper left corner of the rectangle containing the text. The current draw color of the draw state is used, and the current draw position and left margin are set accordingly.
 
<syntaxhighlight lang="lua">
print('press x to start', 32, 60)    -- centered
</syntaxhighlight>


Given <code>x</code> and <code>y</code> coordinates, <code>print</code> uses the given coordinates as the upper left corner of the rectangle containing the text.


<pre>
If there are ''three'' additional arguments, they are assumed to be the <var>x</var>, <var>y</var> ''and'' <var>color</var> arguments. The <code>text</code> is printed in the same manner as when there are just <var>x</var> and <var>y</var>, but using the given <var>color</var> instead of the one in the draw state.
print('press x to start', 32, 60)
</pre>


If the <code>col</code> argument is provided, the message is printed in the given color. This changes the draw color of the draw state.
<syntaxhighlight lang="lua">
print('press x to start', 32, 60, 7) -- centered, white
</syntaxhighlight>


<pre>
print('press x to start', 32, 60, 7)
</pre>


Each character of the Pico-8 font is 4 pixels wide and 8 pixels high. At this size, 32 characters can fit on a single line (128 pixels / 4 pixels per character). If the string is too long to display at the given coordinates, it is clipped, similar to other draw operations. (The string does not wrap to the next line.)
The <code>print()</code> function is especially useful at the PICO-8 command prompt. After interrupting a running program by pressing <kbd>Esc</kbd>, global variables and functions that were created while running are available to any code that is entered on the command line.


<code>print()</code> is especially useful at the Pico-8 command prompt. When you interrupt the program by pressing escape, the global variables and functions of the cartridge are available to expressions you type. To see the value of these expressions, you use the <code>print()</code> function.
To see the value of a variable, or even a full expression, run the <code>print()</code> function on the command line like this:


<pre>
<pre class="p8sh">
> print(player.x)
> print(player.x)
42
42
> print(player.hp / player.maxhp)
0.75
</pre>
</pre>


<code>print()</code> may also be called as <code>?()</code>.
== Shorthand form ==


<pre>
PICO-8 provides a [[Wikipedia:BASIC|BASIC]]-like "<code>?</code>" shorthand for <code>print()</code>. Here is an example showing the normal method and then the shorthand method:
> ?(player.x)
 
<syntaxhighlight lang="lua">
-- print the player x position just inside the upper left corner, in red
print(player.x,2,2,8)
 
-- print the player y position to the right of the x position, in green
?player.y,32,2,11
</syntaxhighlight>
 
This shorthand is intended mostly for quick evaluation of variables at the command line:
 
<pre class="p8sh">
> ?player.x
42
42
</pre>
</pre>


== Cursor positioning and scrolling ==
However, it does work in source code and can be used to save a few tokens in dire situations. The only requirement is that it must be alone on a line. Also keep in mind that this shorthand form is not as readable as the function call.
 
== Character size and screen capacity ==


When you call <code>print()</code> with only a string, the message is printed at the current cursor position, and the cursor is moved to the next line. Specifically, the cursor position advances to the next line: <code>y = y + 8</code>. The cursor's <code>x</code> position is unchanged from its original value, as if it were the left margin of a series of <code>print()</code> calls.
Each character in the PICO-8 system font appears to be 3 pixels wide and 5 pixels tall, but the font includes a 1-pixel wide gap on the right, and a 1-pixel-tall gap on the bottom, so the ''final'' size is actually 4 pixels wide and 6 pixels high.


If the cursor's new <code>y</code> position is less than 8 pixels from the bottom of the screen, the entire graphics display is shifted up by eight pixels after the string is printed, producing a scrolling display effect.
At this font size, 32 characters can fit on a single line (128 pixels / 4 pixels per character), and slightly more than 21 lines (128 pixels / 6 pixels per line) can fit on the screen.


'''Note:''' The scrolling effect only occurs when the cursor position reaches the bottom of the physical display (<code>y > 128</code>). It ignores the [[Clip|clipping region]]. It honors the [[Camera|camera]] offset. The scroll effect has unusual behavior when a clipping region or camera offset are in effect. It is probably best to avoid using scrolling <code>print</code> in combination with a clipping region or camera offset.
Some characters are double-width, or 8 pixels. The <code>[[Ord|ord()]]</code> function may be used to get a character's ordinal number, which can be checked to see if it is less than 128. If it is, it is 4 pixels wide, otherwise it is 8 pixels wide. The control codes in characters 0..15 are exceptions to this rule, and may or may not advance the cursor, or may advance the cursor using a special algorithm, such as the <code>'\t'</code> "advance to next tab stop" character.


== Printing at coordinates ==
== Technical notes ==


When given explicit coordinate arguments, <code>print()</code> uses those coordinates as the upper left corner of the rectangle where the text appears.
* If <var>text</var> is too wide to fit on the screen, it does ''not'' wrap to the next line. Instead, it is clipped at the right side of the screen. There is no auto-wrap functionality; if wrapping is needed, it must be done by the code that calls <code>print()</code>.


In this case, the draw state's cursor position is set to the given coordinates, and remain there after the message is printed. The cursor is ''not'' relocated to the next line of text. (This is not particularly useful, just a side effect of how it is implemented.)
* Whenever the cursor position gets close enough to the bottom of the screen that the bottom of new text would be cut off, the entire graphics display is shifted up by enough pixels to make room for a new line of text. This simulates the kind of scrolling-text mode that retro computers had.
 
* The [[Camera|camera offset]] is observed.
 
* The [[Clip|clipping region]] is observed, but is not used for positioning.
 
* The scrolling functionality may have unusual behavior when either a clipping region or a camera offset is in effect, so be sure to test such code thoroughly.


== Examples ==
== Examples ==
<pre>
<syntaxhighlight lang="lua">
-- print a score display at the bottom of the screen in red
-- print a score display at the bottom of the screen in red
score = 1234
score = 1234
print('score: '..score, 0, 120, 8)
print('score: '..score, 0, 120, 8)
</pre>
</syntaxhighlight>


A demonstration of cursor positioning and scrolling:
A demonstration of cursor positioning and scrolling:


<pre>
<syntaxhighlight lang="lua">
-- clear the screen and reset the cursor position
-- clear the screen and reset the cursor position
cls()
cls()
Line 89: Line 126:
print('seven')  -- prints "seven" on top of "six"
print('seven')  -- prints "seven" on top of "six"
print('eight')  -- prints "eight" on the last line, then scrolls up by 8 pixels
print('eight')  -- prints "eight" on the last line, then scrolls up by 8 pixels
</pre>
</syntaxhighlight>


A function that prints a message at the center of the screen:
A function that prints a message at the center of the screen:
<pre>
<syntaxhighlight lang="lua">
-- 64 is half of the screen width (128 / 2). To calculate the x
-- 64 is half of the screen width (128 / 2). To calculate the x
-- coordinate, the length of the string is multiplied by half
-- coordinate, the length of the string is multiplied by half
Line 102: Line 139:


print_centered('you win!')
print_centered('you win!')
</pre>
</syntaxhighlight>


== See also ==
== See also ==
Line 109: Line 146:
* [[DrawState|Draw state]]
* [[DrawState|Draw state]]
* [[Strings]]
* [[Strings]]
* [[Cls|cls]]
* <code>[[Cls|cls()]]</code>
* [[Color|color]]
* <code>[[Color|color()]]</code>
* [[Cursor|cursor]]
* <code>[[Cursor|cursor()]]</code>
 
[[Category:Reference]]
[[Category:Reference]]
[[Category:API]]
[[Category:API]]
[[Category:Lua]]

Latest revision as of 22:54, 31 March 2022

print( text, [x,] [y,] [color] )
Prints a string of characters to the screen.
text
The Lua string of characters to print.

x
The x coordinate of the upper left corner to start printing.

y
The y coordinate of the upper left corner to start printing.

color
The color to use for the text.

return-value
The x coordinate of the next character to be printed (can be used to calculate printed width)

The print function writes a line of text to the screen using the PICO-8 font, and then moves the cursor to the start of the next line. To write multiple lines in one call, insert one or more newline characters ('\n') in the text.

If only the text argument is supplied, print() uses the cursor position and draw color from the current draw state to determine where to draw the text, and what color to use.

Unless specific x and y position coordinates are supplied, print() behaves like a retro text-based scrolling display. The text is rendered to the screen and then the cursor moves to the left margin on the next line, as if text had a hidden newline character at the end. If there isn't enough room to have another line on the screen, the screen scrolls upwards to make enough.

print('you are standing at the end of')
print('a road before a small brick')
print('building.')


The three additional arguments are all optional, and can be supplied in three ways:

If there is only one additional argument, it is assumed to be the color argument. The text is printed in the same manner as above, but in the given color. This also changes the current draw color of the draw state.

print('press x to start', 7)         -- white


If there are two additional arguments, they are assumed to be the x and y arguments, which are used as the upper left corner of the rectangle containing the text. The current draw color of the draw state is used, and the current draw position and left margin are set accordingly.

print('press x to start', 32, 60)    -- centered


If there are three additional arguments, they are assumed to be the x, y and color arguments. The text is printed in the same manner as when there are just x and y, but using the given color instead of the one in the draw state.

print('press x to start', 32, 60, 7) -- centered, white


The print() function is especially useful at the PICO-8 command prompt. After interrupting a running program by pressing Esc, global variables and functions that were created while running are available to any code that is entered on the command line.

To see the value of a variable, or even a full expression, run the print() function on the command line like this:

> print(player.x)
42
> print(player.hp / player.maxhp)
0.75

Shorthand form

PICO-8 provides a BASIC-like "?" shorthand for print(). Here is an example showing the normal method and then the shorthand method:

-- print the player x position just inside the upper left corner, in red
print(player.x,2,2,8)

-- print the player y position to the right of the x position, in green
?player.y,32,2,11

This shorthand is intended mostly for quick evaluation of variables at the command line:

> ?player.x
42

However, it does work in source code and can be used to save a few tokens in dire situations. The only requirement is that it must be alone on a line. Also keep in mind that this shorthand form is not as readable as the function call.

Character size and screen capacity

Each character in the PICO-8 system font appears to be 3 pixels wide and 5 pixels tall, but the font includes a 1-pixel wide gap on the right, and a 1-pixel-tall gap on the bottom, so the final size is actually 4 pixels wide and 6 pixels high.

At this font size, 32 characters can fit on a single line (128 pixels / 4 pixels per character), and slightly more than 21 lines (128 pixels / 6 pixels per line) can fit on the screen.

Some characters are double-width, or 8 pixels. The ord() function may be used to get a character's ordinal number, which can be checked to see if it is less than 128. If it is, it is 4 pixels wide, otherwise it is 8 pixels wide. The control codes in characters 0..15 are exceptions to this rule, and may or may not advance the cursor, or may advance the cursor using a special algorithm, such as the '\t' "advance to next tab stop" character.

Technical notes

  • If text is too wide to fit on the screen, it does not wrap to the next line. Instead, it is clipped at the right side of the screen. There is no auto-wrap functionality; if wrapping is needed, it must be done by the code that calls print().
  • Whenever the cursor position gets close enough to the bottom of the screen that the bottom of new text would be cut off, the entire graphics display is shifted up by enough pixels to make room for a new line of text. This simulates the kind of scrolling-text mode that retro computers had.
  • The scrolling functionality may have unusual behavior when either a clipping region or a camera offset is in effect, so be sure to test such code thoroughly.

Examples

-- print a score display at the bottom of the screen in red
score = 1234
print('score: '..score, 0, 120, 8)

A demonstration of cursor positioning and scrolling:

-- clear the screen and reset the cursor position
cls()

-- print "try this" at the center of the screen
print('try this', 46, 60, 8)

-- reset the cursor to (0, 0) and the color to 7
cursor()
color(7)

print('one')  -- prints "one" in the upper left corner, advances cursor
print('two')  -- prints "two" on the line below "one", advances cursor

print('three', 16, 16)  -- prints "three" at (16, 16), sets cursor
print('four')  -- prints "four" on top of "three", advances cursor
print('five')  -- prints "five" on the line below "four", advances cursor

print('six', 0, 112)  -- prints "six" on the second-to-last line, sets cursor
print('seven')   -- prints "seven" on top of "six"
print('eight')   -- prints "eight" on the last line, then scrolls up by 8 pixels

A function that prints a message at the center of the screen:

-- 64 is half of the screen width (128 / 2). To calculate the x
-- coordinate, the length of the string is multiplied by half
-- of the character width (4 / 2), then subtracted from 64.
-- Similarly, the y coordinate is 60 = 64 - (8 / 2).
function print_centered(str)
  print(str, 64 - (#str * 2), 60) 
end

print_centered('you win!')

See also