50 Line Challenge

Here’s what I made:

--GDC
--CodeaNoob
function setup()
    supportedOrientations(LANDSCAPE_ANY)
    saveProjectInfo("Description","50 Line Challenge Entry")
    saveProjectInfo("Author","CodeaNoob")
    readProjectInfo("Description")
    readProjectInfo("Author")
    print("Tap to open the Keyboard")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    saveProjectData("Version#", 1.4)
    parameter.action("Help?",function()
        output.clear()
        print("GDC Help Guide\
\
1) Enter the desired Global Variable in the textbox\
\
2) Press Save as Global Data to have the data available to all your projects")
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        end)
    parameter.text("GlobalVar","DefaultVar")
    parameter.action("Save as Global Data",function()
    saveGlobalData(GlobalVar,buffer)
    sound(DATA, "ZgBANgBAQD5AQEBAAAAAABC8UD1GWfU+ZABAf0BAQEBAQGpl")
    print("Saved as Global Variable: "..GlobalVar)
    print("Contents of "..GlobalVar..":\
"..buffer)
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    end)
        parameter.color("Background",0,0,0)
        parameter.color("Colour",0,255,0)
end
function draw()
    Time = os.date("%c")
    background(Background)
    fill(Colour)
    textMode(CORNER)
    buffer = keyboardBuffer()
    
    _,bufferHeight = textSize(buffer)
    
    if buffer then
        text( buffer, 10, HEIGHT - 30 - bufferHeight )
        text(Time,10,10)
        text("Version: Beta "..readProjectData("Version#"),10,30)
    end
end
function touched(touch)
    showKeyboard()
    end

Here’s another submission. Again, I’m taking “one line” as what will fit on one line in portrait mode without wrapping. The object of this game it to make the squares either all red or all green. Tapping on a square will flip the square and the 4 adjacent squares, right, left, up, down to the opposite color. If all the squares are red or green, “Complete” will show on the screen.

EDIT: I made an error in the code and reposted the correct code.


displayMode(FULLSCREEN)
function setup() fTab={{0,1},{-1,0},{0,0},{1,0},{0,-1}} tx,ty=0,0 tab={} 
    for x=1,8 do tab[x]={} for y=1,8 do tab[x][y]=false if math.random(100)<51 then 
    tab[x][y]=true end end end end
function draw() background(40, 40, 50) for x=1,8 do for y=1,8 do fill(0,255,0)
    if tab[x][y] then fill(255,0,0) end rect(x*70,y*70,65,65) end end
    if checkAll() then text("Complete",WIDTH/2,HEIGHT-50) end end
function flip() for a=1,5 do xx=tx+fTab[a][1] yy=ty+fTab[a][2] 
    if xx>0 and xx<9 and yy>0 and yy<9 then tab[xx][yy]=not tab[xx][yy] end end end
function touched(t) if t.state==BEGAN then tx=math.floor(t.x/70)
    ty=math.floor(t.y/70) flip() end end
function checkAll() t=0 for x=1,8 do for y=1,8 do if tab[x][y] then t=t+1 end end end 
    if t==0 or t==64 then return true end return false end

@dave1707, Isn’t this an app on the appstore as well?

@CodeaNoob Since I don’t normally play games anymore, I don’t look for them on the app store. It’s a simple concept, so I’m sure there would be a lot of versions of it. It’s just something that I remember from long ago. I had to repost a correct version of the code above because I was flipping too many squares when I first posted it.

Some moderately ugliness in getting this < 50, but I’m pretty pleased with the line intersect (and now someone will tell me it’s built into math lib).

screenshot

Trace a continuous line to collect jewels, but don’t lift your finger or cross your line.

My high score is 11

function setup()
    displayMode(FULLSCREEN)
    lines = {}
    prize = nil
    score, state = 0
    fontSize(32)
end

function ccw(A,B,C) return (C.y-A.y)*(B.x-A.x) > (B.y-A.y)*(C.x-A.x) end

function intersect(l1, l2)
    return ccw(vec2(l1.x, l1.y), vec2(l2.x, l2.y), vec2(l2.z, l2.w)) ~=
    ccw(vec2(l1.z, l1.w), vec2(l2.x, l2.y), vec2(l2.z, l2.w)) and
    ccw(vec2(l1.x, l1.y), vec2(l1.z, l1.w), vec2(l2.x, l2.y)) ~=
    ccw(vec2(l1.x, l1.y), vec2(l1.z, l1.w), vec2(l2.z, l2.w))
end

function linesIntersect()
    for h = 1, #lines do for j=h + 1, #lines do 
    if intersect(lines[h], lines[j]) and math.abs(h-j) > 2 then return true 
    end end end return false
end

function draw()
    background(40, 40, 50)
    if state == 1 then stroke(118, 146, 171, 255) else 
        text("Trace to treasure without crossing your line.", WIDTH/2, HEIGHT/2) 
        stroke(210, 27, 27, 255) 
    end
    strokeWidth(5)
    if #lines then for i, l in ipairs(lines) do line(l.x, l.y, l.z, l.w) end end
    if state == 1 then
        if not prize then prize = vec2(math.random(WIDTH-100)+50,
        math.random(HEIGHT-100)+50) elseif #lines > 0 then 
            if linesIntersect() then state= 4 return end
            sprite("Planet Cute:Gem Blue", prize.x, prize.y, 32)
            if math.abs(prize.x-lines[#lines].z) < 30 and
            math.abs(prize.y-lines[#lines].w) < 30 then score = score + 1 prize = nil
            end end end
    text(score, WIDTH/2, HEIGHT-30)
end 

function touched(touch)
    if touch.state == BEGAN then state = 1 lines = {} score=0
    elseif touch.state == MOVING then
        l = vec4(touch.prevX, touch.prevY, touch.x, touch.y)
        table.insert(lines, l)
    elseif touch.state == ENDED then state = 0 end
end

My not very original and buged entry. I don’t know if parameters were alowed, but that was fun to make :). Use the slider to play.

local sub,rep = string.sub,string.rep
local row = string.rep(" ",35)
function setup()
    parameter.watch("PARAM_BREAKER")
    parameter.integer("bar",0,30,15)
    score = 0
    f = 0
    n = 1
    ball = vec2(17,5)
    vel = vec2(0,1)
    lev = row..rep("#",36).." * AWESOME * "..rep("#",46).." * FXs * "..rep("#",36)..rep(row,4)
end
function draw()
    local tb = ball + vel
    local pos,b = math.floor(tb.x + tb.y*35)+1, rep(" ",bar).."====="..rep(" ",30-bar)
    local s = b..lev..b
    if f >= 8 then
        f = 0
        lev = string.gsub(string.gsub(lev,"-"," "),"+","-")
        local hit = sub(s,pos,pos) ~= " " and sub(s,pos,pos) or nil
        if ball.x == 35 or ball.x == 0 then
            vel.x = vel.x * -1
        end
        local win = #string.gsub(s," ","") == 10 and "WOW, IMPRESSIVE !"
        if ball.y == 0 or ball.y == 10 or win then
            PARAM_BREAKER = rep(row.."\
",4).."\\t\\t"..(win and win or "GAME OVER")
            PARAM_BREAKER = PARAM_BREAKER.."\
\\t\\t\\t\\t"..score.." points"..rep("\
",7)
            draw = function() end
            return
        end
        if hit == "=" then
            vel = vec2((ball.x - bar - 2)/2,vel.y * -1)
        elseif hit and not string.match(hit,"[%+%-]") then
            score = score + string.byte(hit)
            lev = sub(lev,1,pos-36).."+"..sub(lev,pos-34)
            vel.y = vel.y * -1
        end
        local fx = hit and sound(SOUND_HIT, string.byte(hit)*50)
        ball = ball + vel
        n = math.ceil(ElapsedTime)%5 == 0 and n + .02 or n
    end
    s = sub(s,1,math.floor(ball.x + ball.y*35)).."O"..sub(s,math.floor(ball.x + ball.y*35)+2)
    PARAM_BREAKER = row.."\
"
    for i=1,#s,35 do
       PARAM_BREAKER  = PARAM_BREAKER..string.sub(s,i,i+34).."\
"
    end
    f = f + n
end

@toffer I think you have done a first! Thats just awesome.

@-) +1 for @toffer’s.

I think everyone’s entry is better than mine, so good job lads, @toffer that’s just awesome :o

@CodeaNoob that contest idea was great! Congrats.

Challenge accepted.

-- 50 Lines
function setup()
    cost, incomePerHouse, Money, Houses = 50, 10, 0, "|#" .. (" "):rep(32) .. "|"
    updateParams()
    loop()
end
function buy()
    if Money >= cost and houseC < 33 then
        cost, Money, Houses = cost + 40, Money - cost, "|" .. Houses:sub(2, houseC + 1) .. "#" .. (" "):rep(32 - houseC) .. "|"
        updateParams()
    end
end
function upgrade()
    if Money >= cost then
        Money, incomePerHouse, cost = Money - cost, incomePerHouse + 5, cost + 30
        updateParams()
    end
end
function updateParams()
    parameter.clear()
    parameter.action("Buy House ($" .. cost .. ")", buy)
    parameter.action("Upgrade Income ($" .. cost .. ")", upgrade)
end
function loop()
    output.clear()
    houseC = 0
    for i in Houses:gmatch("#") do
        houseC = houseC + 1
    end
    for i = 1, houseC do
        Money = Money + incomePerHouse
    end
    print("House Tycoon\
\
$$$$    " .. string.format("%19.0f", Money) .. "    $$$$\
\
Houses:\
" .. Houses .. "\
\
Income: " .. houseC * incomePerHouse .. "\
\
Income per house: " .. incomePerHouse)
    tween.delay(1, loop)
end

I’m late to the party, but there is is. A house tycoon. Uses parameters and the tween library if you count those as cheats. Proper indentation on everything though, not stacking multiple lines into one.

Only 34 lines, too.

Here is another one, its the first project I’ve made on Codea so it might be a bit sloppy.

function setup()
        --Set the score to 0
          score = 0     
    --List of posible places for the bug to be
        bugy = {50, 100, 200, 300, 400, 500 ,600, 700, 800}      
  --Coordinates of the bug
    coords = vec2(600, 600)
    time = 0  
  --Width and height if the bug
    h = 200 
    w = 200  
end
        function draw()
    
 --Add 1 to the time
    time = time + 1
    
    --If the time is equal to 120 then redi the bugmand the time,cand takeaway a bit of the score
    if time == 120 then
bugmove()
        times()
        score = score - 50
        end
        
 background(124, 177, 69, 255)
--Locations of the bug
    sprite("Planet Cute:Enemy Bug",coords.x, coords.y, w, h)
    --Show how many points the player has
    fill(184, 72, 47, 255)
    font("Marion-Regular")
    fontSize(70)
text(score, 600, 700)
end
--Test if your finger is on the bug, or "mole", if so, call function bugmove and time
    function touched(touch) 
    if math.abs(touch.x-coords.x)<w/2 and math.abs(touch.y-coords.y)<h/2 then
         bugmove()
    times() 
        score = score + 200    
end

end

function times()    
    time = 0   
    end
        function bugmove()  
            coords.x = bugy[math.random(1, 9)]
            coords.y = bugy[math.random(1, 9)]
        end

It doesn’t cheat with parameters or putting multiple lines on one. It also includes comments.

Here’s another one I wrote a long time ago. Got it to 50 lines.

EDIT: Had to add portrait mode.

displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
    cx,cy,limit,tab1,tab2=0,0,150,{},{}  
    create()
    circ1=physics.body(CIRCLE,5)
    circ1.x=300
    circ1.y=HEIGHT-50
    circ1.gravityScale=.5
    circ1.restitution=1
    line1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    line2=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    line3=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    line4=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))     
end
function create()
    for x=1,limit do
        tab1[x]=physics.body(CIRCLE,10)  
        tab1[x].x=math.random(20,WIDTH-20)
        tab1[x].y=math.random(HEIGHT)
        tab1[x].type=STATIC
        tab2[x]=0
    end       
end
function collide(contact)
    if contact.state == BEGAN then
        cx=contact.bodyA.x
        cy=contact.bodyA.y
        if circ1.y < 20 then
            circ1.y=HEIGHT-50
            circ1.x=math.random(WIDTH)
        end
    end   
end
function draw()
    background(0, 0, 0) 
    fill(255,0,0)    
    ellipse(circ1.x,circ1.y,10,10)
    for x=1,limit do
        if cx==tab1[x].x and cy==tab1[x].y then
            tab2[x]=1 
            cx,cy=0,0
        end        
        fill(255)
        if tab2[x] > 0 then
            fill(math.random(255),math.random(255),math.random(255),255)
        end        
        ellipse(tab1[x].x,tab1[x].y,20)     
    end
end

Fly through an asteroid belt

function setup()
    LoadImages()
    minX,maxX,minY,maxY,shipX,shipY=-WIDTH/2,WIDTH/2,-HEIGHT/2,HEIGHT/2,0,0
    startDist,asteroidRate,timer,alreadyCreated,A=100,20,0,0,{}
    displayMode(FULLSCREEN)
end

function LoadImages()
    Rocks={} for i=1,9 do 
    Rocks[#Rocks+1]=readImage("Tyrian Remastered:Space Ice "..i) end
    for i=1,7 do Rocks[#Rocks+1]=readImage("Tyrian Remastered:Rock "..i) end
end

function AddAsteroid()
    local a={}
    a.x,a.y,a.rs=math.random(minX,maxX),math.random(minY,maxY),math.random(-1,1)
    a.s,a.d,a.r,a.img=math.random(1,3),startDist,0,Rocks[math.random(#Rocks)]
    table.insert(A,a)
end


function draw()
    background(0)
    timer=timer+DeltaTime
    local a=timer*asteroidRate
    for i=1,a-alreadyCreated do AddAsteroid() end
    alreadyCreated=math.floor(a)
    local n=0
    for i,a in pairs(A) do
        n,a.d=n+1,a.d-.1
        if a.d==0 then table.remove(A,i) 
        else 
            a.r=a.r+a.rs
            local v=(startDist/a.d)^.5
            local xx,yy=WIDTH/2+a.x*v,HEIGHT/2+a.y*v
            if xx<0 or xx>WIDTH or yy<0 or yy>HEIGHT then table.remove(A,i)
            else
                local w=a.img.width*a.s/a.d
                if w>3 then
                    pushMatrix()
                    translate(xx,yy)
                    rotate(a.r)
                    sprite(a.img,0,0,w)
                    popMatrix()
                else sprite(a.img,xx,yy,w) end
            end
        end
    end
end
displayMode(FULLSCREEN)
    
function setup() score1=0 ; score2=0 ; init() end

function init()
    tx={10,10,10,10,10} ; tour=1 ; etape=1 ; c1={1,2,3,4,5} ; c2={1,2,3,4,5} ; arrivee=math.random(400,700) ; tc=""
    for i=1, 50 do  j=math.random(5) ; tmp=c1[j] ; k=math.random(5) ; c1[j]=c1[k] ; c1[k]=tmp
       j=math.random(5) ; tmp=c2[j] ; k=math.random(5) ; c2[j]=c2[k] ; c2[k]=tmp  end
end

function draw()
    background(234, 164, 29, 255) ; spriteMode(CORNER) ; fontSize(35) ; textMode(CORNER) ; fill(0, 0, 0, 255)
    if etape==1 then text("Strategy's Game for 2 players",10,400)
    elseif etape==2 then text("Player 1 your combi             Score = "..score1,10,400)
    elseif etape==3 then text(table.concat(c1, "," ),10,400)
    elseif etape==4 then text("Player 2 your combi             Score = "..score2,10,400)
    elseif etape==5 then text(table.concat(c2, "," ),10,400)
    elseif etape==6 then 
        fill(214, 73, 42, 255) ; if tour==1 then xc=20 else xc=850 end ; rect(xc-5,100,140,50)
        fill(16, 14, 14, 255) ; text("Player 1",20,100); text("Player 2",850,100)
        text(score1,20,50); text(score2,850,50)
        if tc=="" then else text(table.concat(tc, "," ),300,50) end
        for i=1, 5 do sprite("SpaceCute:Rocketship",tx[i],700-i*100,25*5,18*5) ; text(i,800,700-i*100) end
        stroke(255) ; rect(arrivee,200,1,500)
    end
    if ( CurrentTouch.state == BEGAN and oldState ~= CurrentTouch.state )
    then
        if etape==6 then val=7-math.ceil((CurrentTouch.y-110)/100+0.5)
            if val>=1 and val<=5 then tx[val]=tx[val]+math.random(30)+30 ; text(tx[val],900,700-val*100)          
                text(val,10,10) ; if tour==1 then tour=2 else tour=1 end 
                if tx[val]>=arrivee-25*5 then tc={1,2,3,4,5} ; itemCount=5
                  repeat hasChanged = false ; itemCount=itemCount - 1
                    for i = 1, itemCount do
                        if tx[i] < tx[i+1] then
                            tx[i], tx[i+1] = tx[i+1], tx[i] ; tc[i], tc[i+1] = tc[i+1], tc[i] ; hasChanged=true
                         end
                    end
                  until hasChanged == false
                  for i=1, 5 do
                    if c1[i]==tc[i] then score1=score1+1 end ; if c2[i]==tc[i] then score2=score2+1 end
                  end
                  init()
                end
            end
        else
          if etape==1 then etape=2 elseif etape==2 then etape=3 elseif etape==3 then etape=4
          elseif etape==4 then etape=5 elseif etape==5 then etape=6 end
        end
    end oldState=CurrentTouch.state
end

it’s an original strategic game for two players in 50 lines
the goal is to find the best combination and have the best score

Another one for kids. Touch the biggest fish on screen at any time, to score points. Touch the wrong fish and lose.

I don’t claim any great programming skills in here, but anyone who is starting out may be interested in how you can make the fish swim up and down using math.sin, and how the fish colours tend to match each other because a single base colour is blended with a random colour.

function setup()
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    start()
end

function start()
    if score then if score>high then high=score end else high=0 end
    fish,bias,timer,score={},color(255,0,0),0,0,0
    for i=1,20 do MakeFish() end
end

function MakeFish()
    local w=math.random(50,150)
    local h=w*(.25+.75*math.random())
    local f=image(w,h)
    pushStyle() setContext(f)
    local r,g,b=math.random(0,255),math.random(0,255),math.random(0,255)
    local c=color(r,g,b,255*math.random())
    fill(c:blend(bias))
    strokeWidth(1)
    ellipse(w*.4,h*.5,w*.8,h)
    ellipse(w*0.9,h*.5,w*.2,h*.5)
    ellipse(w*0.1,h*0.6,8)
    setContext() popStyle()
    local ff={}
    ff={f=f,x=WIDTH*(1+math.random()*.5)+w,y=50+math.random(HEIGHT-100),s=0.5+2*math.random(),w=w,h=h}
    table.insert(fish,ff)   
    fill(43, 48, 138, 255) fontSize=24 textMode(CORNER)
end

function draw()
    background(161, 196, 203, 255)
    timer=timer+DeltaTime
    if timer>10 then bias=color(math.random(0,255),math.random(0,255),math.random(0,255)) timer=0 end
    for i=#fish,1,-1 do
        local h=math.sin(fish[i].y+fish[i].x/90)
        sprite(fish[i].f,fish[i].x,fish[i].y+h*20)
        fish[i].x=fish[i].x-fish[i].s
        if fish[i].x<0 then table.remove(fish,i) MakeFish() end
    end
    text("Score "..score,50,HEIGHT-75) text("High score "..high,50,HEIGHT-100)
end

function touched(t)
    if t.state==ENDED then
        table.sort(fish,function(a,b) return a.w*a.h>b.w*b.h end)
        for i=1,#fish do if fish[i].x<WIDTH then f=fish[i] j=i break end end
        if t.x>f.x-f.w/2 and t.x<f.x+f.w/2 and t.y>f.y-f.h/2 and t.y<f.y+f.h/2 then 
            score=score+math.floor(f.w*f.h) 
            sound(SOUND_PICKUP, 49249) table.remove(fish,j) MakeFish()
        else sound(SOUND_EXPLODE, 49249) start() end
    end
end

@Ignatz Asteroid belt was very cool. Reminded me of the old Windows screensaver. You can depth-sort the asteroids each frame to stop the distant ones overlapping the closer ones:

table.sort(A, function(a1,a2)
    return (a2.d < a1.d) end)
for i,a in pairs(A) do ...
  -- ... existing code

```

I’m jelly about how you guys come up with ideas, I never have such great ideas

@stevon8r - I thought exactly the same way. But all you need to do is start by drawing something really simple. Then watch your brain start coming up with ideas.