Show HN: Simple JavaScript Tetris
github.com
github.com
I had fun building this and experiencing the joyful side of JS (or at least the browser ecosystem). Boarding a plane I'd had the idea of finishing a simple tetris before arrival. The game was in a working state after ~2 hours, even though I had made a few bad decisions. (it was subsequently polished, investing another few hours).
It was made with a "get things done" mindset, and the experience from dabbling with purely functional programming in Haskell was very helpful (mostly the "pure", as in "non-destructive", part).
It's nice that in Javascript we have an environment where we can code a fun game in <400 lines, without any library dependencies (not even internet connection needed) or build systems. On the downside, if it had stronger dynamic types and errors (like Python) that would have saved maybe 25% of my debugging time, probably without coding overhead.
Maybe checkout Bucklescript, Purescript, or Elm for the next one :)
One of my rituals of taking a long trip is to finish a small coding project before I land. Sudoku, minesweeper, tetris, forward-propagating constraint solvers... it's a nice practice. Thanks for sharing yours!
Data representation is pretty universally one of the most important things (basically the "design" part to me). Four hours is pretty quick I guess. Snake might be more difficult (but an array or llist + snake direction + board map should be straightforward). I'd done chess that week and the representation is also doable unless thinking about AI or history or... which is when I quickly go bikeshedding. Multiple different representations for different tasks (and transforming in between) might be a way to go.
My subsequent port to C and Arduino was very natural: http://youtu.be/t3QOeQbEHVs
f=[];p=[1];g=h=2;s=0;t=500;for(i=0;23>i;i++)f[i]=j=-16380;f[23]=-1;onkeydown=e;e(e);function e(b,a){(k=b.which)?k-38?k&-3^37||(g+=a=k^37?-1:1):(z=p,v=p[2],c=p[1],p=[2*v&2|4*c&4,p[3]/2&1|v&2|2*c&4|4*p[0]&8,p[3]/4&1|v/2&2|c&4|2*p[0]&8,v/4&2|c/2&4]):s-j&&setTimeout(e,100+t,h++);m=p[0]<<g;l=p[1]<<g;_=p[2]<<g;o=p[3]<<g;f[h+=32==k]&m|f[1+h]&l|f[2+h]&_|f[3+h]&o?a?g-=a:k^38?(f[h]|=l,f[--h]|=m,f[2+h]|=_,f[3+h]|=o,h?(g=6,x=new Date%7*4,p=[h=0,2908739>>x&15,266758006>>x&15],t*=.97):s+=" Game Over!"):p=z:k^32||e(b);for(y=_=o="";24>++y;o+="\n")for(f[y]+4||(f.splice(y,1),f.unshift(j),s+=++_,o="",y=2),x=14;x;q.textContent=o+s)o+=" X"[(f[y]|p[y-h]<<g)>>x--&1]}I can walk through the submitter's code and understand what's happening clearly. It's fairly well structured and easy enough to read. This is 20x more valuable than any code that is produced using uncommented edge features for a perceived 5% gain. (Note that I'm making up these numbers to illustrate a point.)
Note that it's not logically equivalent to `for (var i = 5; i > 0; i--)` because in this case the `i--` operation is applied after the body of the loop.
Try it in your browser: for (var i = 5; i-- > 0;) { console.log(i); } //4,3,2,1 for (var i = 5; i > 0; i--) { console.log(i); } //5,4,3,2,1
- Can use the same limits as in ascending iteration
- Can do decrementing in for loop (no extra line)
- The arrow unambiguously signals that the iteration is correct :-)This is a test to see how quickly someone brings up the stackoverflow link ;-)
funny thing is i also represented shapes like you did as 0 or 1 on plane, but then changed to x,y list of blocks.
here it is. https://gist.github.com/spaceexperiment/4d6b116ef577a2971259...
[0] http://brainfisheatfishbrain.com/2015/03/writing-a-game-with...
Not all Tetris versions I've seen do that. I'm not sure what official behavior, though.
Either way, cool game! Pretty neat you could do that in sub 400 lines of code.
If there is any need for optimization, then it would probably be to avoid DOM inefficiencies and opting for canvas or another environment.
This one generates a wide array of pieces upon start. And has buttons for use on mobile
Can I use it as a teaching tool? :)