Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Hello, here is a way to draw something on the screen from an array. This snippet will draw a small TV on the screen.

-- hexa color. 1 hexa char = 1 pixel.
tv={"00000d000d",
    "000000d0d",
    "0000000d",
    "44444444444",
    "40000000444",
    "40330330444",
    "40000000464",
    "40033300444",
    "40000000464",
    "44444444444"}
-- position x, pos. y, transparent color to ignore, array to draw
function drawArray(x,y,c,a)
 for i=1,#a do
  for j=1,#a[i] do
   cc=tonumber(string.sub(a[i],j,j),16)
   if(cc~=c) then
    pix(x+j-1,y+i-1,cc)
   end --ifcc
  end --forj
 end --fori
end --drawArray
drawArray(120,60,0,tv)
(+1)

Here's another way to do the same thing which *might* be quicker as it avoids the string and number conversion functions (I haven't tested it!) It also means the data and its descriptors (transparent colour and 'width' are self contained and therefore easier to keep clean/debug) and the numbers are decimals rather than hex. You could even put the drawing function in the table and have it draw itself.

-- drawarray
local test={data={2,2,2,2,2,2,2,2,
                  2,0,0,0,0,0,0,2,
                  2,0,0,0,0,0,0,2,
                  2,0,0,4,4,0,0,2,
                  2,0,0,4,4,0,0,2,
                  2,0,0,0,0,0,0,2,
                  2,0,0,0,0,0,0,2,
                  2,2,2,2,2,2,2,2},
                  trans=0,
                  width=8
           }
--x,y,table
function drawArray(x,y,a)
 local startx=x
 for i=1,#a.data do
   local cc=a.data[i]
   if(cc~=a.trans) then
    pix(x,y,cc)
   end
   x=x+1
   if (i)%a.width==0 then
    y=y+1
    x=startx
   end
 end 
end function TIC()    cls()     drawArray(120,60,test) end

Hope that's of interest!