Lua(但是是一般性的编程问题) - 如何优化绘制像素/代码优化

我几天前接触了 Lua,虽然我之前有一点编程经验,但我还是半新手状态。

无论如何,我正在编写一些基本代码来排列像素并绘制图像。我不需要你重新编写我的代码,因为我喜欢自己尝试和错误中学习编程的挑战。我只希望得到建议,主题以及文档/论文(示例代码片段也可以),这些可以帮助我在现在和将来的编程中应用。当前,我只是从左上到右下打印出数组,我想使它更专业化。

最好的问候,Efraim :)

term.setBackgroundColor(colors.black)
term.clear()

res_w, res_h = term.getSize()

function res()

    res = {res_w,res_h}

        for i = 1, res_w do

            res[i] = {}

        for i2 = 1, res_h do

            res[i][i2] = 0

        end
    end
end

res()

imgw = 3
imgh = 5
img = {1,2,1,1,2,1,2,1,2,1,2,1,1,2,1}

imgw2 = 12
-- 图像宽度
imgh2 = 14
-- 图像高度
img2 = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,32768,32768,32768,32768,1,1,1,1,1,1,1,32768,32768,32768,32768,32768,32768,1,1,1,1,1,32768,32768,1,1,1,1,32768,32768,1,1,1,1,32768,1,32768,1,1,32768,1,32768,1,1,1,1,32768,1,32768,1,1,32768,1,32768,1,1,1,32768,32768,1,1,2,2,1,1,32768,32768,1,32768,32768,1,1,1,1,1,1,1,1,32768,32768,32768,32768,1,1,1,1,1,1,1,1,32768,32768,32768,32768,1,1,1,1,1,1,1,1,32768,32768,1,32768,32768,1,1,1,1,1,1,32768,32768,1,1,1,32768,1,1,1,1,1,1,32768,1,1,1,1,1,2,2,32768,32768,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

-- 最初打印的颜色系列顺序

function bmp(x1,y1,x2,y2,arr)

    i = x1
    i2 = y1
    i3 = 1

    while (i + 1 <= x1 + x2 and i2 + 1 <= y1 + y2) do

        res[i][i2] = arr[i3]
        paintutils.drawPixel(i,i2,res[i][i2])
        i = i + 1
        i3 = i3 + 1

        if (i >= x1 + x2) then

            i2 = i2 + 1
            i = x1

        end
    end
end

bmp(10,10,imgw,imgh,img)
bmp(20,20,imgw2,imgh2,img2)

原文链接 https://stackoverflow.com/questions/70256110

点赞