1K Asteroids Challenge

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

Re: 1K Asteroids Challenge

Post by Ikroth »

I made a version 2 of my entry that is only slightly smaller, but it contains some extra stuff. I added a point total and I also added a vertex to the ship model.

Size: 1,017 bytes

Guidelines: Everything should be okay here. Ship velocity is even properly limited.

Extra:
- Delta
- Score
- Random models for every asteroid
- Difficulty (asteroids break apart)

Minified (using luamin):

Code: Select all

L=love;M=math;R=M.random;u=M.cos;U=M.sin;G=L.graphics;K=L.keyboard.isDown;Q=pairs;I=table.insert;F=99;T=9;C=0;P=0;A=function(b,c,d,e)for f=1,e do a={x=c or R(800),y=d or R(600),X=R(-F,F),Y=R(-F,F),r=b}for g=0,6 do q=R(4)*b/4;I(a,u(g)*q)I(a,U(g)*q)end;I(a,a[1])I(a,a[2])C=C+1;I(E,a)end end;L.keypressed=function(h)if h=="k"then I(E,{x=S.x,y=S.y,X=u(O)*300,Y=U(O)*300,r=3,l=1,1,1,0,0})end end;L.draw=function()D=L.timer.getDelta()if C<1 then E={}S={x=400,y=300,X=0,Y=0,r=6,a=0,-9,-7,9,0,-9,7,-9,-7}I(E,S)A(45,_,_,T)C=T end;O=S.a;S.a=K"a"and O-3*D or(K"d"and O+3*D or O)q=F*F/(S.X^2+S.Y^2)q=q>1 and 1 or q^.5;if K"w"then S.X=S.X+u(O)*F*D*q;S.Y=S.Y+U(O)*F*D*q end;for f,i in Q(E)do i.x=(i.x+i.X*D)%800;i.y=(i.y+i.Y*D)%600;G.translate(i.x,i.y)G.rotate(i.a or 0)G.line(i)G.origin()for j,k in Q(E)do if(k.x-i.x)^2+(k.y-i.y)^2<(i.r+k.r)^2 and#k>9 then if i.l then A(20,k.x,k.y,k.r>20 and 3 or 0)C=C-1;E[f]=_;E[j]=_;P=P+5 end;if i.a then C=0;P=0 end end end;if i.l then i.l=i.l-D;if i.l<0 then E[f]=_ end end end;G.print(P)end
Source:

Code: Select all

L=love
M=math
R=M.random
u=M.cos
U=M.sin
G=L.graphics
K=L.keyboard.isDown
Q=pairs
I=table.insert
-- D = dt
-- _ = nil

F = 99 -- max force
T = 9 -- max asteroids
C = 0 -- asteroid count
P = 0

-- object fields
--  x - x position
--  y - y position
--  X - x velocity
--  Y - y velocity
--  r - radius
--  l - lifetime
--  a - angle

-- create asteroids
-- z = size (radius)
-- x = x position
-- y = y position
-- c = count
A=function(z,x,y,c)
    for i=1, c do
        a = {
            x=x or R(800),
            y=y or R(600),
            X=R(-F,F),
            Y=R(-F,F),
            r=z,
        }
        for t=0, 6 do
            q = R(4)*z/4
            I(a, u(t)*q)
            I(a, U(t)*q)
        end
        I(a,a[1])
        I(a,a[2])
        C = C + 1
        I(E, a)
    end
end

L.keypressed=function(k)
    if k=="k" then
        I(E, {
          x=S.x,
          y=S.y,
          X=u(O)*300,
          Y=U(O)*300,
          r=3,
          l=1,
          1,1,0,0
        })
    end
end

L.draw=function()
    D = L.timer.getDelta()
    -- reset
    if C<1 then
        E = {}
        S = {
            x=400,
            y=300,
            X=0,
            Y=0,
            r=6,
            a=0,
            -9,-7,9,0,-9,7,-9,-7
        }
        I(E, S)
        A(45,_,_,T)
        C = T
    end
    O = S.a
    S.a = K"a" and O - 3*D or (K"d" and O + 3*D or O)
    -- clamp speed
    q = F * F / (S.X^2 + S.Y^2)
    q = q > 1 and 1 or q^.5
    if K"w" then
        S.X = S.X + u(O)*F*D*q
        S.Y = S.Y + U(O)*F*D*q
    end
    for i, e in Q(E) do
        e.x = (e.x + e.X*D)%800
        e.y = (e.y + e.Y*D)%600

        G.translate(e.x, e.y)
        G.rotate(e.a or 0)
        G.line(e)
        G.origin()

        for j, f in Q(E) do
            -- Check if a collision occurred and at least one of the entities is an asteroid (#vertices of f > 9)
            if (f.x - e.x)^2 + (f.y - e.y)^2 < (e.r + f.r)^2 and #f>9 then
                -- bullets are (currently) the only thing which has lifetime, so e.l indicates a bullet
                -- the ship is the only thing which has an angle, so e.a indicates the ship
                -- bullet vs asteroid
                if e.l then
                    A(20, f.x, f.y, f.r > 20 and 3 or 0)
                    C = C - 1
                    E[i] = _
                    E[j] = _
                    P = P + 5
                end
                -- Ship vs asteroid
                if e.a then
                    C=0 -- reset game
                    P=0 -- reset points
                end
            end
        end

        if e.l then
            e.l = e.l - D
            if e.l < 0 then
                E[i] = _
            end
        end
    end

    G.print(P)
end
Attachments
1k-asteroids-v2.love
(800 Bytes) Downloaded 100 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests