50 Line Challenge

@SkyTheCoder sorry for my ignorance…

@TheRogueBatcher Sorry for my bad moods…

@TheRogueBatcher, ignorance is bliss.
@SkyTheCoder bad moods are the spice of life.

Glad to see you guys are friends again :slight_smile:

I am so going to plagiarize and steal that line from above:

“OOOOH that one! I forgot to remove that! It was a prototype experiment for a top secret organisation involving the apocalypse, werewolves, witty one-liners and a lot of pirates! NAWW not really I just forgot to remove it from my original plan”

Maybe I’ll put it in bit the byte, the sequel…

@Ignatz that game is amazing. Quite fun too. Freaking out looking for the killer ball.

@Ignatz yes, but my suggestion of penalties makes it more difficult by putting a punishment on mistakes. The pressure is harder on the player this way, and that makes the game even more interesting (for me). Anyway, it is just a suggestion, if you dont like it, just dont care about my post. Happy new year!

My old eyes aren’t up to the task. Guess I’ll have to tweak the radii up a bit!
Amazing what can be done with so little code.

Pick Up Sticks (rectangles). This program puts 100 sticks (rectangles) on top of each other. The object is to tap the stick that’s on top of the others. There is a count in the upper right corner. Tap the correct stick and the count goes down. Tap the wrong stick and you start over at 100. When you reach 0, the program starts over.


displayMode(FULLSCREEN) supportedOrientations(LANDSCAPE_ANY)
function setup()
    tab,col={},{} rm,a,count=0,0,100
    for z=1,count do
        x=math.random(80,WIDTH-80) y=math.random(80,HEIGHT-80)
        a=not a d=0 if a then d=1 end
        table.insert(tab,vec4(x,y,z,d))
        c=color(math.random(255),math.random(255),math.random(255))
        table.insert(col,c)
    end    
end
function draw()
    background(40,40,50) rectMode(CENTER)
    if rm>0 then
        if rm==count then
            table.remove(tab,rm) table.remove(col,rm)
            rm=0 count=count-1
            if count==0 then setup() end
        else setup() end
    end
    for a,b in pairs(tab) do
        fill(col[a])
        if b.w==1 then rect(b.x,b.y,30,3000)
        else rect(b.x,b.y,3000,30) end
    end
    fill(255) text(count,WIDTH-40,HEIGHT-30)
    if count==1 then text("Congratulations, you're going to win.",WIDTH/2,HEIGHT/2) end
end
function touched(t)
    if t.state==BEGAN then
        for a,b in pairs(tab) do x1=1500 y1=15
            if b.w==1 then x1=15 y1=1500 end               
            if t.x>b.x-x1 and t.x<b.x+x1 and t.y>b.y-y1 and t.y<b.y+y1 then rm=a end
        end
    end
end

Sniper Rifle Simulator, Extracted from a project I’m working on

function setup()
    Clip = 4
    ClipMax = 4
    Extra = 16
    supportedOrientations(LANDSCAPE_ANY)
    cameraSource(CAMERA_BACK)
    parameter.integer("Zoom",1,5)
    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 = 4
        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)
        sound(SOUND_HIT, 32786)
        end
        if Extra < 0 then
        Clip = 4
        Extra = 16
        end
        end

Note I had to remove the cool features to get it to 50 lines, so it’s kinda boring

Here is a simple ball and paddle example. Slide your finger right/left to move paddle.


displayMode(FULLSCREEN)    -- set full screen mode

function setup()
    -- create screen edge
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))    -- left
    e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))    -- top
    e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))    -- right
    e4=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))    -- bottom
    
    -- create multiple balls
    ballTable={}    -- ball table
    for z=1,6 do    -- create 6 balls  
        b=physics.body(CIRCLE,20) -- create a circle physics body
        b.position=vec2(math.random(WIDTH),math.random(HEIGHT)) -- random x,y 
        b.linearVelocity=vec2(500,400)    -- set initial speed
        b.gravityScale=0    -- turn off gravity for balls
        b.restitution=1    -- set bouncyness of balls
        b.friction=0    -- set friction to 0
        table.insert(ballTable,b)    -- put ball info ball table
    end
       
    -- create paddle with a size 100 x 10
    p=physics.body(POLYGON,vec2(-50,5),vec2(50,5),vec2(50,-5),vec2(-50,-5))
    p.position=vec2(WIDTH/2,HEIGHT/2)    -- set initial x,y position
    p.gravityScale=0    -- turn off gravity for paddle
    p.type=STATIC    -- set so paddle won't react to collision
end

function draw()
    background(40, 40, 50)    -- clear screen to defined color
    fill(255)    -- white fill color for balls
    noStroke()    -- don't draw outline around ball or paddle    
    for nbr,ball in pairs(ballTable) do    -- loop thru ball table
        ellipse(ball.x,ball.y,40)    -- draw balls at x,y position
    end    
    fill(255,0,0)    -- red fill color for paddle
    p.x=CurrentTouch.x    -- paddle position from touch
    rect(p.x-50,p.y-5,100,10)    -- draw paddle
end

I updated my house tycoon to draw to the screen, with pressable buttons, text shadows, and how long you’ve been playing the game. (In the game’s time, days, months, and years.)

-- 50 Lines Updated
supportedOrientations(CurrentOrientation)
displayMode(FULLSCREEN)
backingMode(RETAINED)
function buy(type)
    if type == "house" and Money >= cost and houseC < 33 then
        cost, Money, Houses = cost + 40, Money - cost, "|" .. Houses:sub(2, houseC + 1) .. "#" .. (" "):rep(32 - houseC) .. "|"
    elseif type == "upgrade" and Money >= cost and incomePerHouse < 100 then
        Money, incomePerHouse, cost = Money - cost, incomePerHouse + 5, cost + 30
    end
end
function loop()
    houseC = 0
    for i in Houses:gmatch("#") do
        houseC = houseC + 1
        Money = Money + incomePerHouse
    end
    background(255)
    fill(128)
    drawText(Gravity.x * 1.5, Gravity.y * 1.5)
    fill(0)
    drawText(0, 0)
    day = day + 1
    if day > 30 then
        day = 1
        month = month + 1
    elseif month > 12 then
        month = 1
        year = year + 1
    end
    tween.delay(1, loop)
end
function drawText(offX, offY)
    font("HelveticaNeue")
    fontSize(24)
    text("House Tycoon\
" .. string.rep("-", 64) .. "\
You're currently on day " .. day .. ", month " .. month .. ", year " .. year .. "\
" .. string.rep("-", 64) .. "\
$$$$    " .. string.format("%19.0f", Money) .. "    $$$$\
" .. string.rep("-", 64) .. "\
Houses:\
" .. Houses .. "\
" .. string.rep("-", 64) .. "\
Income: " .. houseC * incomePerHouse .. "\
\
Income per house: " .. incomePerHouse .. "\
" .. string.rep("-", 64) .. "", WIDTH / 2 + offX, HEIGHT / 2 + offY)
    text("Buy House ($" .. cost .. ")", WIDTH / 4 + offX, HEIGHT / 8 + offY)
    text("Upgrade Income ($" .. cost .. ")", WIDTH / 2 + WIDTH / 4 + offX, HEIGHT / 8 + offY)
end
function touched(touch)
    if touch.state == BEGAN and touch.y < HEIGHT / 4 then
        if touch.x < WIDTH / 4 then
            buy("house")
        elseif touch.x > WIDTH / 2 + WIDTH / 4 then
            buy("upgrade")
        end
    end
end
cost, incomePerHouse, Money, Houses, day, month, year = 50, 10, 0, "|#" .. (" "):rep(32) .. "|", 1, 1, 1
loop()

I had to come up with a bunch of tricks to get it to fit in 50 lines, but it fits and it works. Just barely.

Here’s another 50 liner. Doesn’t really do much. Might be interesting to watch.


displayMode(FULLSCREEN)
function setup()
    size,ob=25,0
    objects1, objects2, objects3, objects4 = {},{},{},{}
    for z=1,100 do
        a=physics.body(CIRCLE,size)
        a.x=math.random(WIDTH)
        a.y=math.random(HEIGHT)
        a.gravityScale=0
        ob=(ob+1)%4
        if ob==0 then table.insert(objects1,a)
        elseif ob==1 then table.insert(objects2,a)
        elseif ob==2 then table.insert(objects3,a)
        elseif ob==3 then table.insert(objects4,a) end
    end    
    target1 = vec2(WIDTH/2, HEIGHT-150)
    target2 = vec2(WIDTH/2, 150)
    target3 = vec2(150,HEIGHT/2)
    target4 = vec2(WIDTH-150,HEIGHT/2)
end
function draw()
    background(142, 88, 38, 255)   fill(255)
    text("tap screen to swap groups.",WIDTH/2,HEIGHT/2)
    drawMob(objects1,target1,"Planet Cute:Character Boy")
    drawMob(objects2,target2,"Planet Cute:Character Cat Girl")
    drawMob(objects3,target3,"Planet Cute:Character Horn Girl")
    drawMob(objects4,target4,"Planet Cute:Character Pink Girl")
end
function drawMob(obj,tar,spr)
    for i,v in ipairs(obj) do
        move = tar - vec2(v.x, v.y)   
        if move:len() > 10 then move = move:normalize() * 10 end
        v.x = v.x + move.x
        v.y = v.y + move.y
        sprite(spr, v.x, v.y, 20)
    end
end
function touched(touch)
    if touch.state==BEGAN then
        target1,target2=target2,target1
        target3,target4=target4,target3
    end
end

Cool! This gives ideas…

@dave1707 Thats awesome.

I just want to say thank you to everyone who’s posting code in this thread, I for one am finding the code very helpful and informative! I admit a few of them are just too complex for me to understand but they are awesome. Keep 'em coming :slight_smile:

A few nights ago I was fiddling around and got a working Binary-ASCII converter. After some experimentation, I got it to fit 50 lines with a comment to spare.

-- Binary ASCII Converter
function setup()
    parameter.text("Text", "Lorem ipsum")
    parameter.action("To Binary", toBinary)
    parameter.action("To Text", toText)
    tbl = {}
    for i = 0, 255 do
        tbl[string.char(i)] = i
    end
end
function toText()
    assert(string.len(Text:gsub("\
", "")) % 8 == 0, "Binary must be divisivle by 8")
    local outp = ""
    for i = 0, string.len(Text:gsub("\
", "")) / 8 do
        local str = Text:gsub("\
", "")
        local val = 0
        for j = 1, i do
            str = string.sub(str, 9, string.len(str))
        end
        for j = 1, 8 do
            if string.sub(str, j, j) == "1" then
                val = val + 2 ^ (8 - j)
            end
        end
        outp = outp .. string.char(val)
    end
    Text = outp
    hideKeyboard()
end
function toBinary()
    local outp = ""
    for i = 1, string.len(Text) do
        outp = outp .. numToBinary(tbl[string.sub(Text, i, i)])
    end
    Text = outp
    hideKeyboard()
end
function numToBinary(num)
    local outp = ""
    local calc = num + 1
    for i = 7, 0, -1 do
        if calc > 2 ^ i then
            calc = calc - 2 ^ i
            outp = outp .. "1"
        else
            outp = outp .. "0" 
        end
    end
    return outp
end

It’s also a good excuse to bump this terrific resource for people just starting! I don’t like to necro-post, but even the mods say it’s good for this thread.

@SktTheCoder It doesn’t seem to work if the text is changed.

@dave1707 What do you mean? It works fine for me. Do you get an error or something?

@SkyTheCoder When I run the code and press “To Binary”, it shows the binary value. If I key in some other text and press To Binary, it doesn’t show the binary value. If I change the text even before pressing To Binary the first time, it doesn’t show the binary value.

@SkyTheCoder Anytime something is changed in the text box, the result from the binary convert doesn’t show in it.

@SkyTheCoder You can eliminate 4 lines of code in setup() because you don’t need to create the tbl. Lookup the use of string.byte().