Page 2 of 3

Re: 1K Asteroids Challenge

Posted: Wed Dec 28, 2016 9:24 pm
by Inny
Alright, this was my first attempt. I was aiming for the most out there technique in my solution and ended at 1332 bytes after using lua-minifier. I'm bailing at this attempt, because it lead me astray and the only way to get smaller now is to remove features.

Code: Select all

_=function(a,b,r)a,b=a:match("([^:]*):(.*)")r='return 'return loadstring(r.."function("..a..")"..b:gsub('&',r).." end")()end;l,u,s,c,M,m,T,W,H,E,F,G=love,unpack,math.sin,math.cos,math.max,math.min,table,400,300,{},{},{}T,R,K,g,I,J=T.insert,T.remove,l.keyboard.isDown,l.graphics,{[11]=F,[4]=G},{[18]=11,[11]=4}v=_"D,i,r:T(D,c(i)*r)T(D,s(i)*r)"for e=1,6 do v(E,e,21)v(F,e,13)v(G,e,4)end;k=_"D,v,j,X,Y,r:&{X,Y,r,c(r)*v,s(r)*v,0,j,D}"r=_"a:if a==0 then A,B,P={},{},k({10,0,-5,5,-5,-5},0,4,W,H,0)for i=1,6 do A[i]=k(E,i*7,18,W+c(i)*160,H+s(i)*120,i) end end"o=_"D,O,p,q:p,q=D[1]-O[1],D[2]-O[2];&M(0,m(1,p*p+q*q-D[7]*D[7]))"z=_"D,X,Y,r,x,y:D[1],D[2],D[6]=(X+d*x+9)%818-9,(Y+d*y+9)%618-9,D[6]+d"q=_"D:g.translate(u(D));g.rotate(D[3]);g.polygon('line',u(D[8]));g.reset()"l.draw=_":q(P)for i=1,#A do q(A[i]) end;for i=1,#B do q(B[i]) end"l.keypressed=_"d:if d=='p' then T(B,k(G,149,3,u(P)))end"C=_"n,c:&M(-99,m(99,P[n]+c(P[3])*(K'w'and 1 or 0)))"r(0)f=_"a,f:for i=#a,1,-1 do f(a[i]) end"function l.update(h)d=h;P[3],P[4],P[5]=P[3]+d*(K'a'and-3 or(K'd'and 3 or 0)),C(4,c),C(5,s)z(P,u(P))f(B,_'b:z(b,u(b))')for e=#B,1,-1 do b,h=R(B,e),2;for i=#A,1,-1 do a=R(A,i)t=J[a[7]]or 0;h=m(h,o(a,b)*2)for l=1,(1-o(a,b))*m(4,t)do T(A,k(I[t],l*9,t,a[1],a[2],l*2))end;T(o(a,b)<1 and{}or A,a)end;T(b[6]>h and{}or B,b)end;f(A,_'a:z(a,u(a))r(o(a,P))')r(#A)end
What I accomplished:
Yes: external assets (images, sounds, fonts) are not allowed
Yes: asteroids, the player ship and bullets should wrap around the screen
Yes: the gameplay must reset when you destroy all the asteroids or when you die
Yes: bullets should have a limited range/lifetime
Yes: the ship speed should not exceed that of the bullets
Yes: delta
Yes: difficulty (asteroids should break apart as you progress) -- This is the one I think wrecked my budget the most.

Maybe: different "models" for the asteroids, does rotating them count? :P

No: explosion effects
No: score
No: sound effects

Failed: the entire game must be contained in 1024 bytes or less

I'm going to try again with a different technique.

Re: 1K Asteroids Challenge

Posted: Wed Dec 28, 2016 9:38 pm
by easy82
Hello guys!

This challenge is really cool! :D

It took me a while, first I've made a feature-packed version which was about 3000 bytes and then I've started to cut out stuff and compress the code. Ive learned a lot, altough I'm not sure about the value of this knowledge. :)

Guidelines: All OK.
Extras: Uses delta time and features different asteroid shapes.
Bonus: My version comes with a very powerful laser!

Here are the controls:
- W to accelerate, S to decelerate,
- A and D to rotate the ship,
- Hold the space bar to unleash hell!

887 bytes:

Code: Select all

L=love;G=L.graphics;I=L.keyboard.isDown;Y=L.load;M=math;C=M.cos;S=M.sin;R=M.random;T=M.pi;Z=pairs;E=20;K=70;Q=300;W,H=800,600;J=0;function L.draw()if J<1 then P={q=Q,x=W/2,y=H/2,r=0,s=0,p={-5,-5,-5,5,9,0,-5,-5}}O={P}for a=2,K do p={-5,-9,5,-9,9,1,0,9,-9,1}for a=1,#p do p[a]=p[a]-3+R(6)end;p[11],p[12]=p[1],p[2]O[a]={q=K,x=R(W),y=R(H),r=R(T*2),s=R(K),p=p}end end;d,J=L.timer.getDelta(),0;for b,c in Z(O)do if c.q>0 then if c.q==Q then O[#O+1]=R(E)<6 and I" "and{q=1,x=P.x,y=P.y,r=P.r,s=Q,p={0,0,9,0}}or nil;c.r=c.r+d*3*(I"d"and 1 or I"a"and-1 or 0)c.s=c.s+d*K*(I"w"and 1 or I"s"and-3 or 0)c.s=M.max(0,M.min(Q,c.s))elseif c.q==K then J=(c.x-P.x)^2+(c.y-P.y)^2<E^2 and-Q or J+1 else c.q=c.q-d;for b,e in Z(O)do e.q=e.q==K and(c.x-e.x)^2+(c.y-e.y)^2<E^2 and 0 or e.q end end;c.x=(c.x+C(c.r)*c.s*d)%W;c.y=(c.y+S(c.r)*c.s*d)%H;G.origin()G.translate(c.x,c.y)G.rotate(c.r)G.line(c.p)end end end
asteroids-clone-1k.love
(660 Bytes) Downloaded 100 times

Re: 1K Asteroids Challenge

Posted: Wed Dec 28, 2016 10:40 pm
by Positive07
easy82, I can't shoot anything

Re: 1K Asteroids Challenge

Posted: Wed Dec 28, 2016 11:19 pm
by easy82
Positive07 wrote:easy82, I can't shoot anything
I've made it in Love 0.92, where space key is " ", and I guess you have 0.10.x, where it's "space".

I've made another version, here you can shoot with "L":
asteroids-clone-1k-l.love
(661 Bytes) Downloaded 197 times

Re: 1K Asteroids Challenge

Posted: Thu Dec 29, 2016 12:45 am
by Positive07
Well I thought I changed that to K but it didn't work either, this one does though, so amazing!

Re: 1K Asteroids Challenge

Posted: Thu Dec 29, 2016 9:21 am
by Jasoco
Inny wrote:Alright, this was my first attempt. I was aiming for the most out there technique in my solution and ended at 1332 bytes after using lua-minifier. I'm bailing at this attempt, because it lead me astray and the only way to get smaller now is to remove features.

Code: Select all

_=function(a,b,r)a,b=a:match("([^:]*):(.*)")r='return 'return loadstring(r.."function("..a..")"..b:gsub('&',r).." end")()end;l,u,s,c,M,m,T,W,H,E,F,G=love,unpack,math.sin,math.cos,math.max,math.min,table,400,300,{},{},{}T,R,K,g,I,J=T.insert,T.remove,l.keyboard.isDown,l.graphics,{[11]=F,[4]=G},{[18]=11,[11]=4}v=_"D,i,r:T(D,c(i)*r)T(D,s(i)*r)"for e=1,6 do v(E,e,21)v(F,e,13)v(G,e,4)end;k=_"D,v,j,X,Y,r:&{X,Y,r,c(r)*v,s(r)*v,0,j,D}"r=_"a:if a==0 then A,B,P={},{},k({10,0,-5,5,-5,-5},0,4,W,H,0)for i=1,6 do A[i]=k(E,i*7,18,W+c(i)*160,H+s(i)*120,i) end end"o=_"D,O,p,q:p,q=D[1]-O[1],D[2]-O[2];&M(0,m(1,p*p+q*q-D[7]*D[7]))"z=_"D,X,Y,r,x,y:D[1],D[2],D[6]=(X+d*x+9)%818-9,(Y+d*y+9)%618-9,D[6]+d"q=_"D:g.translate(u(D));g.rotate(D[3]);g.polygon('line',u(D[8]));g.reset()"l.draw=_":q(P)for i=1,#A do q(A[i]) end;for i=1,#B do q(B[i]) end"l.keypressed=_"d:if d=='p' then T(B,k(G,149,3,u(P)))end"C=_"n,c:&M(-99,m(99,P[n]+c(P[3])*(K'w'and 1 or 0)))"r(0)f=_"a,f:for i=#a,1,-1 do f(a[i]) end"function l.update(h)d=h;P[3],P[4],P[5]=P[3]+d*(K'a'and-3 or(K'd'and 3 or 0)),C(4,c),C(5,s)z(P,u(P))f(B,_'b:z(b,u(b))')for e=#B,1,-1 do b,h=R(B,e),2;for i=#A,1,-1 do a=R(A,i)t=J[a[7]]or 0;h=m(h,o(a,b)*2)for l=1,(1-o(a,b))*m(4,t)do T(A,k(I[t],l*9,t,a[1],a[2],l*2))end;T(o(a,b)<1 and{}or A,a)end;T(b[6]>h and{}or B,b)end;f(A,_'a:z(a,u(a))r(o(a,P))')r(#A)end
What I accomplished:
Yes: external assets (images, sounds, fonts) are not allowed
Yes: asteroids, the player ship and bullets should wrap around the screen
Yes: the gameplay must reset when you destroy all the asteroids or when you die
Yes: bullets should have a limited range/lifetime
Yes: the ship speed should not exceed that of the bullets
Yes: delta
Yes: difficulty (asteroids should break apart as you progress) -- This is the one I think wrecked my budget the most.

Maybe: different "models" for the asteroids, does rotating them count? :P

No: explosion effects
No: score
No: sound effects

Failed: the entire game must be contained in 1024 bytes or less

I'm going to try again with a different technique.
You could have saved a bit more if you made math its own variable before declaring all those math functions. You did it with Table and Love but not Math.

Re: 1K Asteroids Challenge

Posted: Thu Dec 29, 2016 10:40 am
by zorg
Jasoco wrote:
Inny wrote:...
You could have saved a bit more if you made math its own variable before declaring all those math functions. You did it with Table and Love but not Math.
Exactly 7 bytes would have been saved.
math math math math -> _ math , _ _ _
4*4=16 -> 1+4+1+3*1=9

Re: 1K Asteroids Challenge

Posted: Thu Dec 29, 2016 4:33 pm
by Inny
zorg wrote:
Jasoco wrote:
Inny wrote:...
You could have saved a bit more if you made math its own variable before declaring all those math functions. You did it with Table and Love but not Math.
Exactly 7 bytes would have been saved.
math math math math -> _ math , _ _ _
4*4=16 -> 1+4+1+3*1=9
Yeah, this is why I bailed out. I needed to find another 308 characters and extracting out to one letter variables wasn't going to cut it. My overall approach was too bloated. I thought I'd be able to do some tricks with strings by using a lambda syntax, but it ended up not getting me anything.

Re: 1K Asteroids Challenge

Posted: Thu Dec 29, 2016 9:39 pm
by easy82
Positive07 wrote:Well I thought I changed that to K but it didn't work either, this one does though, so amazing!
So it did work finally? Cool! :)

By the way, I'm amazed how much the code can be compressed in LUA. And I'm sure the game could be done in even less code.

Re: 1K Asteroids Challenge

Posted: Fri Dec 30, 2016 12:51 am
by Inny
1024 on the nose. Output from lua-min

Code: Select all

L=love;G=L.graphics;M=math;c=M.cos;s=M.sin;T=table;R=T.remove;I=T.insert;X=130;function K(G,e,f)for g=0,e*2,2 do j=M.pi*g/e;f[g+1]=f.r*c(j)f[g+2]=f.r*s(j)end;f.d=f.v*c(f.o)f.e=f.v*s(f.o)I(G,f)end;function r()B={}K(B,3,{x=400,y=300,r=9,o=0,v=0})P=B[1]A={}for g=0,9 do K(A,5,{x=g*X,y=X+g%4*X,r=30,o=g,v=9})end end;function C(a,b)return(a.x-b.x)^2+(a.y-b.y)^2<a.r^2 end;function L.keypressed(h)_=h=='p'and K(B,4,{x=P.x,y=P.y,v=X,o=P.o,r=4})end;function V(g,a,f)P[g]=M.max(-X,M.min(X,P[g]+f(P.o)*a*9*d))end;function U(i,...)i.x=(i.x+i.d*d)%800;i.y=(i.y+i.e*d)%600;i.t=(i.t or 0)+d;_=...and U(...)end;function D(h)return L.keyboard.isDown(h)and 4 or 0 end;function L.draw()d=L.timer.getDelta()P.o=P.o+d*(D'd'-D'a')V('d',D'w',c)V('e',D'w',s)U(u(A))U(u(B))for g=#A,1,-1 do _=C(A[g],P)and r()a=R(A,g)for j=#B,2,-1 do b=R(B,j)if C(a,b)then a.r=a.r/2 end;_=b.t<1 and I(B,b)end;_=a.r>7 and K(A,5,a)end;_=#A<1 and r()Q(u(A))Q(u(B))end;function Q(i,...)G.translate(i.x,i.y)G.rotate(i.o)G.line(u(i))G.reset()_=...and Q(...)end;r()u=unpack
And my unminified source code

Code: Select all

--[[
-- A-steroids  B-ullets  P-layer
--  .r adius
--  .o rotation (theta)
--  .v velocity initial
--  .d elta X
--  .e delta Y
--  .t ime alive
--]]

L=love
G=L.graphics
M=math
c=M.cos
s=M.sin
T=table
R=T.remove
I=T.insert
X = 130 -- interesting screen position, and interesting velocity, sweet!

function K(G,e,f) -- initialize_polygon(verticies, object)
  for i = 0, e*2,2 do
    j=M.pi*i/e
    f[i+1]=f.r*c(j)
    f[i+2]=f.r*s(j)
  end
  f.d = f.v*c(f.o)
  f.e = f.v*s(f.o)
  I(G,f)
end

function r() -- reset()
  B={}
  K(B,3,{x=400,y=300,r=9,o=0,v=0})
  P=B[1]
  A={}
  for i = 0, 9 do
    K(A, 5,{x=i*X,y=X+i%4*X,r=30,o=i,v=9})
  end
end

function C(a,b) -- distance of center minus radius, collision at 0
  return (a.x-b.x)^2+(a.y-b.y)^2<a.r^2
end

function L.keypressed(k)
  _=k=='p'and K(B,4,{x=P.x,y=P.y,v=X,o=P.o,r=4})
end

function V(i,a,f) --add velocity(index, amount, trig func)
  P[i]=M.max(-X,M.min(X,P[i]+f(P.o)*a*9*d))
end

function U(o,...)
  o.x=(o.x+o.d*d)%800
  o.y=(o.y+o.e*d)%600
  o.t=(o.t or 0)+d
  _=... and U(...)
end

function D(k) return L.keyboard.isDown(k) and 4 or 0 end

function L.draw()
  d=L.timer.getDelta()
  P.o=P.o+d*(D'd'-D'a')
  V('d',D'w',c)
  V('e',D'w',s)
  U(u(A))
  U(u(B))
  for i=#A,1,-1 do
    _=C(A[i],P)and r()
    a=R(A,i)
    for j=#B,2,-1 do
      b=R(B,j)
      if C(a,b)then
        a.r=a.r/2
      end
      _=b.t<1 and I(B,b)
    end
    _=a.r>7 and K(A,5,a)
  end
  _=#A<1 and r()
  Q(u(A))
  Q(u(B))
end

function Q(o,...) -- draw polygon
  G.translate(o.x,o.y)
  G.rotate(o.o)
  G.line(u(o))
  G.reset()
  _=... and Q(...)
end

r()
u=unpack -- minifies better down here
So, here's how I deeply compromised my integrity:

Yes: the entire game must be contained in a single Lua file (1024 bytes or less)
Yes: external assets (images, sounds, fonts) are not used
Yes: asteroids, the player ship and bullets should wrap around the screen, but it's at the hard border as soon as the center of something crosses, instead of enough overlap to not notice it suddenly jumping.
Yes: the gameplay must reset when you destroy all the asteroids or when you die
Yes: bullets should have a limited range/lifetime, which is 1 second
Yes: the ship speed should not exceed that of the bullets. Well no. Fly diagonally instead of straight up and down and you go faster than bullets. Velocity is capped, just not as well as I'd like.
Yes: delta, did jasoco's tactic of getting dt despite all work happening in love.draw
Not really: different "models" for the asteroids, I rotated them.
No: sound effects
No: explosion effects
No: score
Maybe: difficulty. asteroids get smaller as you progress. About the only challenge to the game is scoring a direct hit. Near hits make the asteroid smaller.

So did I win some street cred? :P