1K Breakout challenge

Show off your games, demos and other (playable) creations.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: 1K Breakout challenge

Post by Inny »

There's a bug in my code where if you resize the window but take your time with it before releasing the mouse button, the ball makes a major jump. :D The fix in the l.update function would be to change A=A*7 to A=m.min(A*7,1), or I guess A=N(A*7,1), which the latter increase code size by 9 or 5 bytes respectively, and limits the ball's jump to under a 1/6th of a second.
User avatar
hatninja
Prole
Posts: 30
Joined: Sun Apr 07, 2013 1:31 am
Contact:

Re: 1K Breakout challenge

Post by hatninja »

I made one with a much different approach, and got it under 1K!
It could be compressed much further, but i think it's fine for now. What a fun challenge!

v1.1 - 990 characters:

Code: Select all

l=love;l.window.setMode(0,0,{resizable=0})m,g,k=math,l.graphics,l.keyboard.isDown a,t,w,h,f,n,p,li,sc,lv=m.abs,g.rectangle,20,20,"fill","\n",g.print,3,0,1 function l.draw()d=l.timer.getDelta()if li==0 then p"Game Over!"return else p(li..n..sc..n..lv)end s=k"right"and 8 or k"left"and-8 or 0 bx,by,px=bx+dx*d,by+dy*d,m.max(m.min(px+s*d,w-3),0)if k"space"then if dy==0 then bx,by=px,py else bx,by=bx+dx*d,by+dy*d end end dx=bx<0 and a(dx)or bx+1>w and-a(dx)or dx if by<0 then dy=a(dy)end if by+1>h then li=li-1;nw()end if by+2>=h and bx+1>px and bx<px+3 then dy=-(lv+2);dx=-(px+1-bx)*(lv+2)end if sc>=lv*100 then lv=lv+1;nw()r()end W,H=g.getDimensions()g.scale(W/w,H/h)t(f,px,py,3,1)t(f,bx,by,1,1)for i=0,99 do x,y=i%10*2,2+m.floor(i/10)if b[i]>0 then t(f,x,y,1.9,.9)if bx<x+2 and x<bx+1 and by<y+1 and y<by+1 then if a(bx-x)>a(by-y)then dx=-dx else dy=-dy end b[i]=0;sc=sc+1 end end end end function nw()px,py,bx,by,dx,dy=9,19,9,0,0,0 end function r()b={}for i=0,99 do b[i]=1 end end nw()r()
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Breakout challenge

Post by ivan »

I made one with a much different approach, and got it under 1K!
Pretty good. It took me a while to clear all the blocks but still, not bad at all. :)
WetDesertRock
Citizen
Posts: 67
Joined: Fri Mar 07, 2014 8:16 pm

Re: 1K Breakout challenge

Post by WetDesertRock »

Alright, by 1K you meant 1024 bytes right?

It accomplishes all the requirements, and the challenges.

Here is my submission. Without luamin it is 1497 bytes:

Code: Select all

local G,l,lk,objheight,sx,sy,m,lw, t = {},love,love.keyboard.isDown,.05, 400,400, math, love.window

lw.setMode(sx,sy,{resizable=true})

function l.load()
  G = {}

  function G.r()
    G[1] = {x=.5,y=.9, .15} -- Paddle
    G[2] = {x=.5,y=.8, .05} -- Ball
    G.b = G[2]
    G.p = G[1]

    function G.b.c(a,b)
      return a.x + a[1] > b.x and a.x < b.x + b[1] and m.abs(a.y - b.y) < objheight
    end
  end

  -- Entities are stored as x,y, width
  G.r()
  G.l = 3

  G.d = {x=-1,y=-1}
  G.s = .3

  for x=0,9 do
    for y=0,3 do
      table.insert(G, {x=x*.1,y=(y*(objheight+.01)) + .05, .09})
    end
  end
end

function l.draw()
  -- Update
  t = l.timer.getDelta()
  G.b.x = G.b.x + G.d.x*t * G.s
  G.b.y = G.b.y + G.d.y*t * G.s

  if G.b.x < 0 then
    G.d.x = m.abs(G.d.x)
  elseif G.b.x > 0.95 then
    G.d.x = -m.abs(G.d.x)
  end

  if G.b.y < 0 then
    G.d.y = 1
  end
  if G.b.y > 1 then
    G.l = G.l - 1
    if G.l <= 0 then
      l.load() -- reset
    else
      G.r()
    end
  end

  if lk'a' then
    G.p.x = G.p.x - .5*t
  elseif lk'd' then
    G.p.x = G.p.x + .5*t
  end
  G.p.x = m.min(m.max(G.p.x,-.08),.92)

  if G.b:c(G.p) then
    t = ((G.b.x + .02) - (G.p.x + .07))/.17
    G.d.x = t*2
    G.d.y = -1
  end

  for i=#G,3,-1 do
    if G.b:c(G[i]) then
      G.d.y = 1
      G.s = G.s + .01

      table.remove(G,i)
    end
  end
  sx,sy=lw.getMode( )

  -- Draw
  for i=1,#G do
    love.graphics.rectangle('fill', G[i].x*sx,G[i].y*sy, G[i][1]*sx, objheight*sy)
  end
end
But here is the minified version (1015 bytes):

Code: Select all

local a,b,c,d,e,f,g,h,i={},love,love.keyboard.isDown,.05,400,400,math,love.window;h.setMode(e,f,{resizable=true})function b.load()a={}function a.r()a[1]={x=.5,y=.9,.15}a[2]={x=.5,y=.8,.05}a.b=a[2]a.p=a[1]function a.b.c(j,k)return j.x+j[1]>k.x and j.x<k.x+k[1]and g.abs(j.y-k.y)<d end end;a.r()a.l=3;a.d={x=-1,y=-1}a.s=.3;for l=0,9 do for m=0,3 do table.insert(a,{x=l*.1,y=m*(d+.01)+.05,.09})end end end;function b.draw()i=b.timer.getDelta()a.b.x=a.b.x+a.d.x*i*a.s;a.b.y=a.b.y+a.d.y*i*a.s;if a.b.x<0 then a.d.x=g.abs(a.d.x)elseif a.b.x>0.95 then a.d.x=-g.abs(a.d.x)end;if a.b.y<0 then a.d.y=1 end;if a.b.y>1 then a.l=a.l-1;if a.l<=0 then b.load()else a.r()end end;if c'a'then a.p.x=a.p.x-.5*i elseif c'd'then a.p.x=a.p.x+.5*i end;a.p.x=g.min(g.max(a.p.x,-.08),.92)if a.b:c(a.p)then i=(a.b.x+.02-(a.p.x+.07))/.17;a.d.x=i*2;a.d.y=-1 end;for n=#a,3,-1 do if a.b:c(a[n])then a.d.y=1;a.s=a.s+.01;table.remove(a,n)end end;e,f=h.getMode()for n=1,#a do love.graphics.rectangle('fill',a[n].x*e,a[n].y*f,a[n][1]*e,d*f)end end
Attachments
1KBreakout_WDR.love
(720 Bytes) Downloaded 102 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Breakout challenge

Post by ivan »

WetDesertRock, that's pretty good. I like the pacing and it's quite playable.
Found a small bug though: after clearing all the blocks but the game didn't reset right away.
The ball kept bouncing for a few seconds before the blocks were reset.
Interesting idea of avoiding love.update altogether although I don't believe l.timer.getDelta() would work in that case. :(
Also note that .5*t could be reduced to t/2.

Inny's version is the smallest in size,
but this is probably the most 'playable' entry in terms of gameplay.
Good job there. :)
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: 1K Breakout challenge

Post by 0x72 »

927 926 920 917 912 898 :) (only 3rd "Extra credit" though)

Code: Select all

L,m=love,math
n,N,a=m.min,m.max,m.abs
g,k=L.graphics,L.keyboard.isDown
R=g.rectangle
H,S,s,T,W=50,500,200,595,150
t=function(l,r,w,h)return not(x<l or x>l+w or y<r or y>r+h)end
w,h=50,20
v,u=1.19,1.95
F='fill'

function D()
X,B,x,y,d,e=325,{},410,500,1,-1
for i=45,750,60 do for j=50,300,30 do B[#B+1]={x=i,y=j}end end
end;D()

function L.update(q)
x,y=x+q*d*s,y+q*e*s
if x>800 or x<0 then d=d*-1;x=x+d*9 end
if y<0 then e=e*-1;y=y+e*9 elseif y>700 then D()end
if k'left'then X=N(X-q*S,0)end
if k'right'then X=n(X+q*S,650)end
if t(X,T,W,H) then e=e*-1; d=(d+(((x-X)/W-0.5)*2))/2; end
for i=#B,1,-1 do b=B[i];if t(b.x,b.y,w,h)then
A=m.atan2(x-b.x-w/2,y-b.y-h/2)
if -v>A and A>-u then d=-a(d)elseif v<A and A<u then d=a(d)else e=(a(A)<v and 1 or -1)*a(e)end
table.remove(B,i)
end end
if#B==0 then D()end
end

function L.draw()
R(F,x-4,y-4,8,8)
R(F,X,T,W,H)
for i=1,#B do R(F,B[i].x,B[i].y,w,h)end
end
resizable version: 972 968

Code: Select all

L,m=love,math
n,N,a=m.min,m.max,m.abs
g,k=L.graphics,L.keyboard.isDown
R=g.rectangle
H,S,s,T,W=50,500,200,595,150
t=function(l,r,w,h)return not(x<l or x>l+w or y<r or y>r+h)end
w,h=50,20
v,u=1.19,1.95
F='fill'
L.window.setMode(800,600,{resizable=0})function D()X,B,x,y,d,e=325,{},410,500,1,-1
for i=45,750,60 do for j=50,300,30 do B[#B+1]={x=i,y=j}end end
end;D()function L.update(q)x,y=x+q*d*s,y+q*e*s
if x>800 or x<0 then d=d*-1;x=x+d*9 end
if y<0 then e=e*-1;y=y+e*9 elseif y>700 then D()end
if k'left'then X=N(X-q*S,0)end
if k'right'then X=n(X+q*S,650)end
if t(X,T,W,H)then e=e*-1; d=(d+(((x-X)/W-0.5)*2))/2; end
for i=#B,1,-1 do b=B[i];if t(b.x,b.y,w,h)then
A=m.atan2(x-b.x-w/2,y-b.y-h/2)if -v>A and A>-u then d=-a(d)elseif v<A and A<u then d=a(d)else e=(a(A)<v and 1 or -1)*a(e)end
table.remove(B,i)end end
if#B==0 then D()end end
function L.draw()c,o=g.getDimensions()g.scale(c/800, o/600)R(F,x-4,y-4,8,8)R(F,X,T,W,H)for i=1,#B do R(F,B[i].x,B[i].y,w,h)end end
Attachments
breakout0x72.love
(781 Bytes) Downloaded 86 times
User avatar
bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: 1K Breakout challenge

Post by bakpakin »

932 chars, resizable (If you provide resizing in a love.conf)

Use A and D to move the paddle, S to shoot the ball. Collisions are not perfect, but what can you do :roll:

Code: Select all

function S()W,H=G.getDimensions()
P,R,T={x=W/2,y=H-5,w=W/8,h=10},{x=W/2,y=H-15,w=10,h=10,u=0,v=0},1
B={[-1]=P,[0]=R}for x=W/20,W,W/10 do for y=10,100,20 do B[#B+1]={x=x,y=y,w=W/10,h=20}end end
end
L,Z=love,math
G,K,a,m,M,r,L.resize=L.graphics,L.keyboard.isDown,Z.abs,Z.min,Z.max,Z.random,S
S()
function L.draw()if K'a'then P.x=M(W/20,P.x-10)end
if K'd'then P.x=m(W-W/20,P.x+10)end
if K's'and T then T,R.v,R.u=nil,-r(4)-4,r(4)-2 end
R.x,R.y=T and P.x or R.x+R.u,R.y+R.v
for i=1,#B do
b=B[i]
X,Y=a(R.x-b.x)-R.w/2-b.w/2,a(R.y-b.y)-R.h/2-b.h/2
if Y<0 and X<0 then
if X>Y then R.u=-R.u else R.v=-R.v end
B[i]=B[#B];B[#B]=nil
if #B<1 then S() end
break
end
end
if R.y>H-15 and a(R.x-P.x)<R.w/2+P.w/2 then R.v,R.u=-a(R.v),m(M(-8,R.u+r(2)-1),8) end
if R.x<5 then R.u=a(R.u) elseif R.x>W-5 then R.u=-a(R.u) end
if R.y<5 then R.v=a(R.v) elseif R.y>H+9 then S() end
for i=-1,#B do b=B[i]G.rectangle('fill',b.x-b.w/2,b.y-b.h/2,b.w,b.h) end
end
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()
WetDesertRock
Citizen
Posts: 67
Joined: Fri Mar 07, 2014 8:16 pm

Re: 1K Breakout challenge

Post by WetDesertRock »

ivan wrote:WetDesertRock, that's pretty good. I like the pacing and it's quite playable.
Found a small bug though: after clearing all the blocks but the game didn't reset right away.
The ball kept bouncing for a few seconds before the blocks were reset.
Interesting idea of avoiding love.update altogether although I don't believe l.timer.getDelta() would work in that case. :(
Also note that .5*t could be reduced to t/2.

Inny's version is the smallest in size,
but this is probably the most 'playable' entry in terms of gameplay.
Good job there. :)
Yeah, there are a couple bugs. The game actually doesn't know you won (I'm sure any method I do this would result in going over the limit), however that wasn't on the list of requirements Let me look into fixing this

Why would getDelta not work?

Thanks :D
Last edited by WetDesertRock on Fri May 20, 2016 1:54 am, edited 1 time in total.
User avatar
hatninja
Prole
Posts: 30
Joined: Sun Apr 07, 2013 1:31 am
Contact:

Re: 1K Breakout challenge

Post by hatninja »

I rewrote a lot of my code, and finally figured out a more solid physics engine! I kept it simple, and now it has gotten pretty low!
Edit: Changed to 3.1, changed the layout of the bricks just to save space!
3.1 - 920 bytes

Code: Select all

function z(X,Y,x,y,w)return X<x+w and x<X+1 and Y<y+1 and y<Y+1 end
function r(n)px=9;bx,by=9,0;dx,dy=0,0 if n then c=0;b={}for i=0,90 do b[i]=1 end end end
l,m=love,math;g,w=l.graphics,l.window;w.setMode(0,0,{resizable=0})w.setTitle"1K Breakout"r(0)t,k,a=g.rectangle,l.keyboard.isDown,m.abs;f="fill"
function l.draw()
D=m.min(l.timer.getDelta(),.1)W,H=w.getMode()g.scale(W/20,H/20)
px=m.min(m.max(k"left"and px-D*8 or k"right"and px+D*8 or px,0),17)
if k"up"and dy==0 then bx,by=px,19;dy=9 end
dx=bx<0 and a(dx)or bx>19 and-a(dx)or dx
dy=by<0 and a(dy)or dy
q=by>19 and r()
if z(bx,by,px,19,3)then dy=-a(dy)dx=(bx-1-px)*3 end
t(f,px,19,3,1)t(f,bx,by,1,1)
nx=bx+dx*D;ny=by+dy*D
for i=0,90 do
	if b[i]then
		x=i%18+1;y=i%5+2
		if z(nx,by,x,y,1)then dx=bx>x and a(dx)or-a(dx)b[i]=q end
		if z(bx,ny,x,y,1)then dy=by>y and a(dy)or-a(dy)b[i]=q end
		c=b[i]and c or c+1
		t(f,x,y,1,1)
	end
end
bx,by=nx,ny
q=c>90 and r(0)
end
3.1 Ultra - 842 bytes

Code: Select all

function z(X,Y,x,y,w)return X<x+w and x<X+1 and Y<y+1 and y<Y+1 end function r(n)Q=9;X,Y=9,0;O,P=0,0 if n then c=0;b={}for i=0,90 do b[i]=1 end end end l,m=love,math;g,w=l.graphics,l.window;w.setMode(0,0,{resizable=0})w.setTitle"1K Breakout"r(0)t,k,a=g.rectangle,l.keyboard.isDown,m.abs;f="fill"function l.draw()D=m.min(l.timer.getDelta(),.1)W,H=w.getMode()g.scale(W/20,H/20)Q=m.min(m.max(k"left"and Q-D*8 or k"right"and Q+D*8 or Q,0),17)if k"up"and P==0 then X,Y=Q,19;P=9 end O=X<0 and a(O)or X>19 and-a(O)or O;P=Y<0 and a(P)or P;q=Y>19 and r()if z(X,Y,Q,19,3)then P=-a(P)O=(X-1-Q)*3 end t(f,Q,19,3,1)t(f,X,Y,1,1)C=X+O*D;V=Y+P*D for i=0,90 do if b[i]then x=i%18+1;y=i%5+2 if z(C,Y,x,y,1)then O=X>x and a(O)or-a(O)b[i]=q end if z(X,V,x,y,1)then P=Y>y and a(P)or-a(P)b[i]=q end c=b[i]and c or c+1;t(f,x,y,1,1)end end X,Y=C,V q=c>90 and r(0)end
Last edited by hatninja on Sat May 21, 2016 6:10 pm, edited 1 time in total.
WetDesertRock
Citizen
Posts: 67
Joined: Fri Mar 07, 2014 8:16 pm

Re: 1K Breakout challenge

Post by WetDesertRock »

Ok, version 2. This time with proper end game behavior and much smaller file size!

Expanded (1455 bytes)

Code: Select all

local G,l,lk,objheight,sx,sy,m,lw,s,dx,dy, b,p, t = {},love,love.keyboard.isDown,.05, 400,400, math, love.window, 0.3, -1,-1

lw.setMode(sx,sy,{resizable=true})

function l.load()
  G = {}

  function G.r()
    G[1] = {x=.5,y=.9, .15} -- Paddle
    G[2] = {x=.5,y=.8, .05} -- Ball
    b = G[2]
    p = G[1]

    function b.c(a,b)
      return a.x + a[1] > b.x and a.x < b.x + b[1] and m.abs(a.y - b.y) < objheight
    end
  end

  -- Entities are stored as x,y, width
  G.r()
  G.l = 3

  for x=0,9 do
    for y=0,1 do
      table.insert(G, {x=x*.1,y=(y*(objheight+.01)) + .05, .09})
    end
  end
end

function l.draw()
  -- Update
  t = l.timer.getDelta()
  b.x = b.x + dx*t * s
  b.y = b.y + dy*t * s

  if b.x < 0 then
    dx = m.abs(dx)
  elseif b.x > 0.95 then
    dx = -m.abs(dx)
  end

  if b.y < 0 then
    dy = 1
  end
  if b.y > 1 then
    G.l = G.l - 1
    if G.l <= 0 then
      l.load() -- reset
    else
      G.r()
    end
  end

  if lk'a' then
    p.x = p.x - t/2
  elseif lk'd' then
    p.x = p.x + t/2
  end
  p.x = m.min(m.max(p.x,-.08),.92)

  if b:c(p) then
    t = ((b.x + .02) - (p.x + .07))/.17
    dx = t*2
    dy = -1
  end

  for i=#G,3,-1 do
    if b:c(G[i]) then
      dy = 1
      s = s + .01

      table.remove(G,i)
      if #G == 2 then
        l.load()
      end
    end
  end
  sx,sy=lw.getMode( )

  -- Draw
  for i=1,#G do
    love.graphics.rectangle('fill', G[i].x*sx,G[i].y*sy, G[i][1]*sx, objheight*sy)
  end
end
luamin'ed (944 bytes):

Code: Select all

local a,b,c,d,e,f,g,h,i,j,k,l,m,n={},love,love.keyboard.isDown,.05,400,400,math,love.window,0.3,-1,-1;h.setMode(e,f,{resizable=true})function b.load()a={}function a.r()a[1]={x=.5,y=.9,.15}a[2]={x=.5,y=.8,.05}l=a[2]m=a[1]function l.c(o,l)return o.x+o[1]>l.x and o.x<l.x+l[1]and g.abs(o.y-l.y)<d end end;a.r()a.l=3;for p=0,9 do for q=0,1 do table.insert(a,{x=p*.1,y=q*(d+.01)+.05,.09})end end end;function b.draw()n=b.timer.getDelta()l.x=l.x+j*n*i;l.y=l.y+k*n*i;if l.x<0 then j=g.abs(j)elseif l.x>0.95 then j=-g.abs(j)end;if l.y<0 then k=1 end;if l.y>1 then a.l=a.l-1;if a.l<=0 then b.load()else a.r()end end;if c'a'then m.x=m.x-n/2 elseif c'd'then m.x=m.x+n/2 end;m.x=g.min(g.max(m.x,-.08),.92)if l:c(m)then n=(l.x+.02-(m.x+.07))/.17;j=n*2;k=-1 end;for r=#a,3,-1 do if l:c(a[r])then k=1;i=i+.01;table.remove(a,r)if#a==2 then b.load()end end end;e,f=h.getMode()for r=1,#a do love.graphics.rectangle('fill',a[r].x*e,a[r].y*f,a[r][1]*e,d*f)end end
Attachments
1KBreakout_WDR.love
(710 Bytes) Downloaded 88 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 50 guests