This is a function that draws a book with some custom content to the screen. My inspiration was morrowind and its plethora of books. All this function needs to work is an external,global page variable.
How to use:
- copy this function to your code
- declare a global
pagevariable (that starts at -1, which is the cover page) - keep in mind that it resets
camera()to print the book on screen regardless of the camera offset, so use it after you already printed you game with desired offset - add your book as a pair of title and a long content string like this:
{"Title","Super long and cool content"},to the books table - consider using P8SCII to achieve certain effects cheaply - it breaks the function in fun, weird ways

function show_book(title)
camera()
local given_title=title or "how to use show_book"
local books={
{"how to use show_book",
"this book will teach you how to use the show_book function in pico-8. the show_book function takes a given title and displays the book content on the screen. you can use the⬅️ and ➡️buttons to turn the pages, and the 🅾️ button to exit the book. you can also add your own books to the books table, as long as they have a title and a content string. the function will automatically split the string into lines and pages, and draw the book cover and pages for you. you can also change some variables in the function, such as the maximum length of a line, the maximum number of lines per page, and the colors and positions of the book elements. to use the function, you need to call it with a title argument, like this: show_book ('how to use show_book'). have fun reading and writing books with pico-8!"
},
}
local function split_str(str,length)
local max_length=length or 13
local strings=split(str," ",false)
local result={}
local cur_str=""
for i in all(strings) do
if #cur_str+#i<max_length then
if #cur_str!=0 then
cur_str=cur_str.." "..i
else
cur_str=i
end
else
add(result,cur_str)
cur_str=i
end
end
add(result,cur_str)
return result
end
--end split_str
local function draw_cover(tit)
local title=split_str(tit,11)
local line_nr=0
rectfill(64,25,117,100,2)
line(118,25,118,100,15)
rect(66,27,115,98,15)
for i in all(title) do
line_nr+=1
print(i,70,35+line_nr*6,15)
end
end--draw_cover_end
local function give_book(tit)
local title=tit or "the hanging tree"
local book_lines={}
local lib={}
local ret={}
--lookup the book and split
--the strings
for i in all(books) do
if i[1]==title then
book_lines=split_str(i[2])
end
end
for i in all(book_lines) do
add(lib,i)
if #lib==10 then
add(ret,lib)
lib={}
end
end
if(#lib!=0)add(ret,lib)
return ret
--
end--give_book end
local content=give_book(given_title)
--dislay background
fillp(▒)
rectfill(0,0,127,127,0)
fillp()
if page>0 then
--draw the book itself
rectfill(10,25,117,100,7)
line(10,24,118,24,2)
line(118,25,118,100,15)
line(64,25,64,100,15)
--page numbers
print(page,64-print(page,0,-1000),95,4)
print(page+1,66,95,4)
--print left page
trash=0
for i in all(content[page]) do
trash+=1
print(i,12,24+trash*6,13)
end
if#content+1>=page then
--print right page
trash=0
for i in all(content[page+1]) do
trash+=1
print(i,66,24+trash*6,13)
end
end
elseif page==-1 then
--draw the book cover
draw_cover(given_title)
end
--controls
print("🅾️",111,115,4)
if page+1<#content then
print("➡️",111,95,4)
if(btnp(➡️))page+=2
end
if page>-1 then
print("⬅️",11,95,4)
if(btnp(⬅️))page-=2
end
end
|
401 tokens
[Please log in to post a comment]




