50 Line Challenge

@SkyTheCoder - picking up @dave1707’s suggestion, try this under 30 line version

It’s not only shorter, but it avoids using power functions in the binary conversion. Power functions are very slow, so an iterative approach works better.

function setup()
    txt="Lorem Ipsum"
    b=TextToBinary(txt)
    print(b)
    print(BinaryToText(b))
end

function TextToBinary(txt)
    local bin=""
    for c in txt:gmatch(".") do
        local s,n="",c:byte()
        while n>0 do
            n,y= math.modf(n/2)
            s=(y*2)..s
        end
        bin=bin..string.rep("0",8-#s)..s
    end
    return bin
end

function BinaryToText(bin)
    local s=""
    for i=1,#bin,8 do --process 8 chars at a time
        s=s..string.char(tonumber(string.sub(bin,i,i+7),2))
    end
    return s
end

@Ignatz Are you an ex race car driver. You seem care a lot about speed. Yes, power functions are slower than other similar calculations, but this program isn’t the kind that’s going to be slowed down by its use. I would go more for the “here’s another way of doing it” than saying the power function is slowing down the code. :smiley:

@dave1707 - first, I come from a time (as you do), when speed mattered.

Second, if you are going to write graphics code, especially shaders, you’d better learn what is fast and what is slow, if you want decent FPS. Every tiny little bit counts when you are in a fragment shader. An image 1/4 the size of the screen requires about 750,000 fragment shader calculations per draw, by my count. Just one power function makes a huge speed difference, with those sorts of numbers.

So it’s not really about making this particular program as fast as possible. I know my friend @SkyTheCoder is writing some advanced stuff, and I am just trying to help him optimise his code.

@Ignatz I see your point and agree with you. You care about the best code used for every program, where I just care about the code for the current one.

@CodeaNoob try this

function setup()
    Clip = 8
    ClipMax = 8
    Extra = 32
    supportedOrientations(LANDSCAPE_ANY)
    cameraSource(CAMERA_BACK)
    parameter.integer("Zoom",1,5)
    parameter.boolean("Slincer",true)
    print("Sniper Rifle Scope\
"..Clip.." / "..Extra)
    end
    function draw()
        sprite(CAMERA,WIDTH/2,HEIGHT/2,WIDTH*Zoom,HEIGHT*Zoom)
        fill(255, 255, 255, 0)
        stroke(0, 0, 0, 255)
        strokeWidth(10)
        ellipse(WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        ellipse(WIDTH/2,HEIGHT/2,7)
        strokeWidth(1.5)
        line(10,HEIGHT/2,WIDTH-10,HEIGHT/2)
        line(WIDTH/2,10,WIDTH/2,HEIGHT-10)
        line(100,HEIGHT/2-10,100,HEIGHT/2+10)
        line(WIDTH-100,HEIGHT/2-10,WIDTH-100,HEIGHT/2+10)                   
        line(WIDTH/2-10,HEIGHT-100,WIDTH/2+10,HEIGHT-100)
        line(WIDTH/2-10,100,WIDTH/2+10,100)
        line(WIDTH/2-10,300,WIDTH/2+10,300)
        line(WIDTH/2-10,HEIGHT-300,WIDTH/2+10,HEIGHT-300)
        line(300,HEIGHT/2-10,300,HEIGHT/2+10)
        line(WIDTH-300,HEIGHT/2-10,WIDTH-300,HEIGHT/2+10)
        fill(255, 255, 255, 255)
        fontSize(20)
        text("Zoom: \
    "..Zoom,60,60)
        if Clip <= 0 then
        Clip = 8
        Extra = Extra - ClipMax
        end
        if Extra < 2 then
        fill(255, 0, 0, 255)
        text("LOW AMMO",WIDTH/2,500)
        end
        end
        function touched(touch)
        if touch.state == ENDED then
        Clip = Clip - 1
        print(Clip.." / "..Extra)
        if Slincer == true then
        sound(SOUND_HIT, 32786)
        end
        if Slincer == false then
        sound(DATA, "ZgNAPQA/Qw5HR0QnqAZouZsMpD6cT+k+XQASZz9EQzo/OyNX")
        end
        end
        if Extra < 0 then
        Clip = 8
        Extra = 32
        end
        end

@Dantheman2001 Remember to use three tildes (~~~) at the top and bottom of your code for it to format, otherwise it’s a big jumble of text.