1K Breakout challenge

Show off your games, demos and other (playable) creations.
User avatar
ivan
Party member
Posts: 1457
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

1K Breakout challenge

Post by ivan » Mon May 09, 2016 5:43 pm

Goal:
Breakout clone in 1K or less - unzipped :)

Requirements:
1.reset when all blocks are cleared
2.handle the ball going off the screen
3.the paddle should not move off the screen
3.do NOT use "loadstring" (we want the focus to be on game logic)

Extra credit:
1.gameplay gets harder as you progress
2.should scale and be playable on different resolutions
3.dt
4.locals
Last edited by ivan on Tue May 10, 2016 4:11 am, edited 6 times in total.

User avatar
ivan
Party member
Posts: 1457
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Breakout challenge

Post by ivan » Mon May 09, 2016 5:43 pm

Revision 3, 1002 bytes:

Code: Select all

L=love;g,k=L.graphics,L.keyboard.isDown;p,n,a=pairs,tonumber,math.abs;x,y=g.getDimensions()W,H=x/13,y/32;w,h=W/2,H/2;s=W/8;t={}t.t={x=x/2,y=0,w=x,h=h}t.l={x=0,y=y/2,w=w,h=y}t.r={x=x,y=y/2,w=w,h=y}t.z={x=x/2,y=y-h,w=W,h=h}z=t.z;e,u,q=0,0,0;i=1/60;function j()b={x=z.x,y=z.y-H,w=h,h=h}U,V=0,0;u=u+1;for x=1,12 do for y=6,10+u do t[x+y*12]={x=x*W,y=y*H,w=w,h=h}e=e+1 end end end;function L.update(c)if e<1 then j()end;q=q+c;while q>i do q=q-i;b.x=b.x+U;b.y=b.y+V;if b.y>y then b.x,b.y=z.x,z.y-H;U,V=0,0 end;for k,d in p(t)do dx,dy=b.x-d.x,b.y-d.y;if a(dx)<d.w+b.w and a(dy)<d.h+b.h then if d==z then U=dx/(b.w+d.w)*s;V=-s elseif d==t.l then U=a(U)elseif d==t.r then U=-a(U)elseif d==t.t then V=a(V)elseif n(k)then t[k]=nil;e=e-1;V=-V end;break end end;m=k('left')and z.x>z.w and-s or 0;m=k('right')and z.x<x-z.w and s or m;V=k('space')and-s or V;z.x=z.x+m;if V==0 then b.x=b.x+m end end end;function o(d)g.rectangle('fill',d.x-d.w,d.y-d.h,d.w*2,d.h*2)end;function L.draw()for f,d in p(t)do o(d)end;o(b)end
Note: does not use locals! :(
License: MIT (go ahead, add it as an Easter egg in your real projects)
Attachments
b1k.love
revision 3
(679 Bytes) Downloaded 173 times
Last edited by ivan on Tue May 10, 2016 5:48 am, edited 3 times in total.

User avatar
Doctory
Party member
Posts: 439
Joined: Fri Dec 27, 2013 4:53 pm

Re: 1K Breakout challenge

Post by Doctory » Mon May 09, 2016 6:19 pm

This is some seriously cool stuff. I sadly don't know how to make anything this small, but kudos to you for this.
"This is what videogames should be like; they should have meaning and triple machine guns"
- Jan Willem Nijman, 2013
github

User avatar
ivan
Party member
Posts: 1457
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Breakout challenge

Post by ivan » Mon May 09, 2016 6:27 pm

Thanks! There are Lua minification tools out there, so you could probably pull something off if you get your usual code down to 2-3k.

User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: 1K Breakout challenge

Post by Beelz » Mon May 09, 2016 11:24 pm

I messed something up somewhere, typical me... But if someone wants to steal this and make it work(good luck), have at it. Its at 959 bytes right now.

Code: Select all

local lg,lk=love.graphics,love.keyboard.isDown local lgw,lgh=lg.getDimensions()local b={lgw/2,lgh-60}local v={0,0}local p={lgw/2-75,lgh-50}local w={{0,0,lgw,10},{0,0,10,lgh},{0,lgh-10,lgw,10},{lgw-10,0,10,lgh}}local blk={}for i=0,9 do for j=2,6 do blk[#blk+1]={i*80,j*30,true}end end function love.update(dt)if lk(' ')and v[1]==0 and v[2]==0 then v[2]=-100 end b[1]=b[1]+v[1]*dt b[2]=b[2]+v[2]*dt if lk('a')then p[1]=p[1]-100*dt end if lk('d')then p[1]=p[1]+100*dt end for f,z in ipairs(blk)do local dx,dy=b[1]-z[1]-15,b[2]-z[2]-25 if dx<=20 and dy<=10 then blk[f][3]=false v[2]=-v[2]end end if b[2]<=p[2]and b[2]>p[2]-10 then if b[1]>=p[1]and b[1]<=p[1]+150 then v[2]=-v[2]end end end function love.draw()lg.setColor(255,255,255)for _,w in ipairs(w)do lg.rectangle("fill",w[1],w[2],w[3],w[4])end for q=1,#blk do if blk[q][3]then lg.rectangle("fill",blk[q][1]+1,blk[q][2]+1,78,28)end end lg.circle("fill",b[1],b[2],10) lg.rectangle("fill",p[1],p[2],150,10)end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website

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 » Tue May 10, 2016 4:06 am

My method is a bit similar to ivan's with only globals, I got mine down to 939 and I could probably get it lower if a bit better whitespace removal, etc.

Code: Select all

l,m,u,z=love,math,unpack,table.remove;g,k=l.graphics,l.keyboard.isDown;
d,G={},{};for i=0,7 do d[i]=(i*2+1)/8*m.pi end;
r=function()for x=0,13 do for y=0,7 do G[y*14+x+1]={x,1+y,1,1} end end end
p=function()P,B,D = {6,22,2,1},{6.75,20,.5,.5},6 end;r()p()
c=function(X,Y,W,H)return(B[1]<X+W)and(B[2]<Y+H)and(B[1]+.5>X)and(B[2]+.5>Y)end
h=function(w) for i=1,#G do if c(u(G[i])) then z(G,i)D=(w and 3-D or 7-D)%8 break end end end
l.update=function(A)A=A*6
if k('a')and P[1]>0 then P[1]=m.max(0,P[1]-A)elseif k('d')and P[1]<12 then P[1]=m.min(12,P[1]+A) end
B[2]=B[2]+m.sin(d[D])*A;if c(u(P)) then D=4+m.floor((B[1]+.5-P[1])*1.6) end;h()
B[1]=B[1]+m.cos(d[D])*A;h(1);if #G<1 then r()p() end
if B[1]<=0 or B[1]>=13.5 then D=(3-D)%8 end;if B[2]<=0 then D=7-D end;if B[2]>=24 then p() end end
s=function(T)g.rectangle('fill',u(T))end
l.draw=function()X,Y=g.getDimensions()g.scale(X/14,Y/24)s(P)s(B) for i=1,#G do s(G[i]) end end
A couple of notes (or perhaps cheats)
- I'm not including a conf.lua, but if I did I'd enable resizable windows.
- I use a and w instead of left and right to save 7 characters. :P
- If anyone actually plays it, they may notice the ball can only travel in 8 possible directions.
- love.update(A) for the dt extra credit.
- i like semicolons instead of spaces where lua's syntax requires a token to separate two symbols.

User avatar
ivan
Party member
Posts: 1457
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Breakout challenge

Post by ivan » Tue May 10, 2016 4:41 am

Beelz, lk(" ") didn't work on my version of love, had to change it to lk("space"). Nice first try though.

Inny, good job on making the code so compact and clever use of table.remove in this case.
- If anyone actually plays it, they may notice the ball can only travel in 8 possible directions.
It works pretty well, and I managed to clear all the blocks.
could probably get it lower if a bit better whitespace removal, etc
Note that new lines often take two bytes "\r\n" instead of one. You can safely replace those with space or ";".

Also, what do you think should be the next 1k challenge?

User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: 1K Breakout challenge

Post by undef » Tue May 10, 2016 10:22 am

Inny wrote:snip
Nice!

I took your solution and shaved it down to 905 bytes, I think <900 should be possible.

Code: Select all

l,m,u,z=love,math,unpack,table.remove
g,k=l.graphics,l.keyboard.isDown
d,G={},{}for i=0,7 do d[i]=(i*2+1)/8*m.pi end
r=function()W,H=g.getDimensions()for x=0,13 do for y=0,7 do G[y*14+x+1]={x,1+y,1,1}end end end
p=function()D,P,B=6,{6,22,2,1},{7,20,.5,.5}end;r()p()c=function(X,Y,W,H)return(B[1]<X+W)and(B[2]<Y+H)and(B[1]+.5>X)and(B[2]+.5>Y)end
h=function(w)for i=1,#G do if c(u(G[i]))then z(G,i)D=(w and 3-D or 7-D)%8 break end end end
l.update=function(A)A=A*6
if k'a'and 0<P[1]then P[1]=m.max(0,P[1]-A)elseif k'd'and P[1]<12 then P[1]=m.min(12,P[1]+A)end
B[2]=B[2]+m.sin(d[D])*A;if c(u(P))then D=4+m.floor((B[1]+.5-P[1])*1.6)end;h()B[1]=B[1]+m.cos(d[D])*A;h(1);if #G<1 then r()p()end
if B[1]<=0 or B[1]>=13.5 then D=(3-D)%8 end;if B[2]<=0 then D=7-D end;if B[2]>=24 then p()end end
s=function(...)for _,v in pairs{...}do g.rectangle('fill',u(v))end end
l.draw=function()g.scale(W/14,H/24)s(P,B,u(G))end
One thing you didn't do is omitting the parentheses when calling a function with only a string as a parameter.
You probably know that already, but for those who don't - you can omit them when you only have a single string, or a single table as parameter.
twitter | steam | indieDB

Check out quadrant on Steam!

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 » Tue May 10, 2016 4:33 pm

Once more, building off of undef's tweaks.
- I fixed a bug where the ball gets stuck in the left wall
- Three lives!
- The ball speeds up as you knock out more of the wall
- 952 characters

Code: Select all

l,m,u,z=love,math,unpack,table.remove;g,k=l.graphics,l.keyboard.isDown
d,G={},{}for i=0,7 do d[i]=(i*2+1)/8*m.pi end;r=function()L=4;for x=0,13 do for y=0,7 do G[y*14+x+1]={x,1+y,1,1}end end end
p=function()L,S,D,P,B=L-1,1,6,{6,22,2,1},{7,20,.5,.5}end
r()p()c=function(X,Y,W,H)return(B[1]<X+W)and(B[2]<Y+H)and(B[1]+.5>X)and(B[2]+.5>Y)end
h=function(w)for i=1,#G do if c(u(G[i]))then z(G,i)D=(w and 3-D or 7-D)%8;S=S*1.01 break end end end
l.update=function(A)A=A*7;P[1]=k'a'and m.max(0,P[1]-A)or k'd'and m.min(12,P[1]+A)or P[1]
B[2]=B[2]+m.sin(d[D])*A*S;if c(u(P))then D=4+m.floor((B[1]+.5-P[1])*1.6)end;h()B[1]=B[1]+m.cos(d[D])*A*S;h(1);
if B[1]<0 or B[1]>13.5 then D=(3-D)%8;B[1]=m.max(0,m.min(13.5,B[1]))end;if B[2]<0 then D,B[2]=7-D,0 end;if B[2]>24 then p()end
if #G<1 or L<1 then r()p()end end;s=function(...)for _,v in pairs{...}do g.rectangle('fill',u(v))end end
l.draw=function()W,H=g.getDimensions()g.scale(W/14,H/24)s(P,B,u(G))end
And for the kill shot, conf.lua is 50 characters

Code: Select all

function love.conf(t)t.window.resizable=true end
This was really fun. I hope this becomes a thing.
Attachments
breakout_1k_challenge_inny_undef_ivan.love
(907 Bytes) Downloaded 86 times

User avatar
zorg
Party member
Posts: 2620
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: 1K Breakout challenge

Post by zorg » Wed May 11, 2016 1:10 am

940 characters built off the last one by Inny: (950 if we count conf.lua)
- replaced whatever line breaking symbol(s) with semicolons, now it's a one-liner
- defined N and U for m.min and m.max (totally unnecessary, since both with and without this, the length is unchanged)

Code: Select all

l,m,u,z=love,math,unpack,table.remove;g,k=l.graphics,l.keyboard.isDown;N,U=m.min,m.max;d,G={},{}for i=0,7 do d[i]=(i*2+1)/8*m.pi end;r=function()L=4;for x=0,13 do for y=0,7 do G[y*14+x+1]={x,1+y,1,1}end end end;p=function()L,S,D,P,B=L-1,1,6,{6,22,2,1},{7,20,.5,.5}end;r()p()c=function(X,Y,W,H)return(B[1]<X+W)and(B[2]<Y+H)and(B[1]+.5>X)and(B[2]+.5>Y)end;h=function(w)for i=1,#G do if c(u(G[i]))then z(G,i)D=(w and 3-D or 7-D)%8;S=S*1.01 break end end end;l.update=function(A)A=A*7;P[1]=k'a'and U(0,P[1]-A)or k'd'and N(12,P[1]+A)or P[1];B[2]=B[2]+m.sin(d[D])*A*S;if c(u(P))then D=4+m.floor((B[1]+.5-P[1])*1.6)end;h()B[1]=B[1]+m.cos(d[D])*A*S;h(1);if B[1]<0 or B[1]>13.5 then D=(3-D)%8;B[1]=U(0,N(13.5,B[1]))end;if B[2]<0 then D,B[2]=7-D,0 end;if B[2]>24 then p()end;if #G<1 or L<1 then r()p()end end;s=function(...)for _,v in pairs{...}do g.rectangle('fill',u(v))end end;l.draw=function()W,H=g.getDimensions()g.scale(W/14,H/24)s(P,B,u(G))end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests