____ __ __ ____ ____
/ __| | | ___/ ___|
| __| | |__ | __)
|_| `_____|____/____/ JS1K
Spread the love by absorbing all the positive energy around you.
In this simple game you control an energy ball through seven levels of love.
The goal is to fuse with the warm glowing blobs. But beware: Others might grow to big and turn into negative energy.
Play it here: http://js1k.com/2012-love/demo/1254
Created by Martin Kleppe for the JS1K 2012 competition. The object of this competition is to create a cool JavaScript “application” no larger than 1024 bytes. Feel free to follow me on Twitter (@aemkei).
The final script: ./crushed.js
_="for(i in a)a[3]+i[6]]=aZjL=S=c.height=c.wid=60n=f=j[],k'WiDa7LLB`@`^?
1WA@YAh8;rL1YKCEAxUCE?+AJuA{AI1W7@5C+K_CBmi=}>1MLB8UPMAV7CHFUUu9?1WKFu/i
UI1uM?VKCABTAW-DWA1V_C2A9iCmAuiErAx7?rA<7?'plit(1)[L] ii<zleng;)fzcharCo
deAt(i)-32 ifZP=B[0])B.push({x*6y*6s*1X-3,Y-3})},setInterval(if(!B){k(i-
27/3$2,font='3em Impact','+O%360'+(e-)+',lx(L<8?L?'ιQvQι '+L:' fυsQ':'τ
hQ eηd',24i-=L<8),e<0&&n();return}$4 f=ib=BZ)wi(b){x<0^x>SXy<0^y>SYxX,yY
el=B[eZ)l!=b&s>0&#>00|Maqrt(G*G+N*N)-s-#)<0#>s?(#,s-=):(s,#-=));s>0kaR(x
,y,x,y,s),c=s>P?200:3404'1+')5.1*Main(321)97'),f)}0>P?(j:f==1L,j},3,onmo
usedown=P.X(P.xX)/OP.Y(P.yY)/O0},'+c,zaddColorStop(.,lc(S,S):f]function(
e){0,0%, ;for(fillStylk&&(.si=32new Date/+',O5+(b==P)*+==0;-l.-zpage++0)
*=-1),'hsla([ith)'a..3#l$.G(xx)N(yy)O10QεZ];jB=ke=ze.";for(Y=0;$="zkjZQO
NG$# "[Y++];)with(_.split($))_=join(pop());eval(_)
Last year I gave some talks about JS Golfing Lessons and Binary Love with many tricks how to save bytes in JavaScript.
But for this project I skipped most of the rules and did some code optimization on a “meta” level.
To dramatically reduce the file size, I’ve used JSCrunsh where the main rule is: Repeat yourself! That genius tool replaces multiple occourences in your code with a singe character, so no need to assign Math
to a variable and reuse this across the project.
You’ll see eg. f[i++]
, 240
or function(e){
many times around the code. And even if there is no argument “e” for the function statement, let’s add it, because it will be swapped with the same placeholder as all other functions.
To keep track of my manual optimisations I created a little Node.js script that can be executed from the command line:
$ node zip.js
The longest statements were the method calls to the canvas object. To shrink them down to a two letter shortcut I used a modified version of Marijn Haverbeke’s mechanized abbreviation:
for(i in a)a[i[3]+i[6]]=a[i]
Another heavy part was the level data. I had to store the position, size and movement for each blob in each level. Initially it wasted about 300 characters but I managed to write a simple tool that encoded the data into a custom format where two integers are replaces by a single character. The decoder itself is only about 50 bytes:
f="",e="WiDa7LLB`@`^?1WA@YAh8;rL".split(1)[L]
for(i=0;i<e.length;)f+=e.charCodeAt(i++)-32
See the list of my entries for 140byt.es including Binary Tetris, the JS Community Logo, Game of Life, Rubik’s Cube Solver and Minesweeper.
Thanks to the 140byt.es community and it’s Wiki, Uglify.js for saving the first bytes, Marijn Haverbeke for his canvas shortcut tricks, Aivo Paas for his insane JSCrush Tool, my company Ubilabs for the great feedback, JSBin for the hacking environment and Astrid for her patience.