simplify this code function init() appstate = mktitlest() end function update()…
- User
simplify this code function _init() app_state = mk_title_st() end
function _update() app_state:update() end
function _draw() cls() app_state:draw() end -->8 --title state
function mk_title_st() local st = {} --state
function st:update() end function st:draw() local t -- title local lp -- line position local lp_tbl local ctp_opt t={ "cabin jam 2024 project", "bingo", "jammo" } ctp_opt = { x=0, y=0, w=128, h=128 }-- lp=cent_txt_pos(t,ctp_opt) lp_tbl=cent_txt_tbl_pos(t,ctp_opt) for i, lp in ipairs(lp_tbl) do print(t[i],lp[1],lp[2]) end end
return stend -->8 --utilities
----- constants ----- const={ letter_width=4, letter_height=6, line_height=8 }
----- text -----
--center text position --(line of text, options) --desc: --provides the x and y --coordinates to print a --line of text in the center --of a given space function cent_txt_pos(txt,opt) --opt { -- x, x position of space -- y, y position of space -- w, width of space -- h height of space --) local x=opt.x local y=opt.y local w=opt.w local h=opt.h
local cent_x local cent_y cent_x=cent_txt_x(txt,x,w) cent_y=cent_txt_y(tst,y,h) return{cent_x,cent_y}end
--center text table position --(table of text lines, options) --desc: --provides the x and y --coordinates to print a set of --lines of text in the center --of a given space function cent_txt_tbl_pos(txt,opt) --opt { -- x, x position of space -- y, y position of space -- w, width of space -- h height of space --) local x=opt.x local y=opt.y local w=opt.w local h=opt.h
local cent_pos --center positions local cent_y local lh = const.line_height cent_y=cent_txt_tbl_y(txt,y,h) cent_pos={} for i, txt_l in ipairs(txt) do local cent_x local ly_off--line y offset cent_x=cent_txt_x(txt_l,x,w) ly_off=(i-1)*lh add( cent_pos, {cent_x,cent_y+ly_off} ) end return cent_posend
--get center text y --(text, space y, height y) --desc: --returns the y coordinate of --a line of text centered in a --space starting at the provided --y and is as tall as the --provided height function cent_txt_y(txt,y,h) local lh = const.line_height local dif_h=h-lh return y+dif_h/2 end
--center text table y --(text table, space y, height y) --desc: --finds the y coordinate of a table --of text lines in a space at the --provided y coordinate and is --as tall as the given height function cent_txt_tbl_y(txt,y,h) local lh = const.line_height txt_h=#txt*lh dif_h=h-txt_h return y+dif_h/2 end
--get center text x --(line of text, width of space) function cent_txt_x(txt,x,w) local lw=const.letter_width local txt_lw -- text line width local dif_w -- width difference
- Assistant
To simplify and clean up the given code, we can first remove any unused variables and redundant comments, then reorganize it for clarity. Here's a refactored version of your code:
function _init() app_state = mk_title_st() end function _update() app_state:update() end function _draw() cls() app_state:draw() end -->8 -- Title state function mk_title_st() local st = {} function st:update() -- Add logic here if necessary end function st:draw() local titles = { "cabin jam 2024 project", "bingo", "jammo" } local ctp_opt = { x = 0, y = 0, w = 128, h = 128 } local lp_tbl = cent_txt_tbl_pos(titles, ctp_opt) for i, lp in ipairs(lp_tbl) do print(titles[i], lp[1], lp[2]) end end return st end -->8 -- Utilities const = { letter_width = 4, letter_height = 6, line_height = 8 } function cent_txt_tbl_pos(txt, opt) local cent_pos = {} local cent_y = cent_txt_tbl_y(txt, opt.y, opt.h) for i, txt_l in ipairs(txt) do local cent_x = cent_txt_x(txt_l, opt.x, opt.w) local line_y_offset = (i - 1) * const.line_height table.insert(cent_pos, {cent_x, cent_y + line_y_offset}) end return cent_pos end function cent_txt_y(y, h) local height_diff = h - const.line_height return y + height_diff / 2 end function cent_txt_tbl_y(txt, y, h) local total_text_height = #txt * const.line_height local height_diff = h - total_text_height return y + height_diff / 2 end function cent_txt_x(txt, x, w) local text_width = #txt * const.letter_width local width_diff = w - text_width return x + width_diff / 2 end - User
runtime error on line 62
- Assistant
- User
put it all together
- Assistant
Certainly! Here's the complete and corrected version of the Lua code in one place:
- User
still doesn't work
- Assistant