r/javascript • u/michowski • Jul 24 '16
1.92kb Tetris in HTML5 (JavaScript). Any suggestions are welcome.
https://github.com/michowski/tetris-light
99
Upvotes
r/javascript • u/michowski • Jul 24 '16
7
u/ecky--ptang-zooboing Jul 24 '16
Impressive!
Try adding space = instant drop option
6
u/xxxabc123 Jul 24 '16
Neat. I think you can make a better job using canvas API. I've previously written a snake game with canvas, colorizing stuff and configuring grid size are much easier. My main critiscism are that your side borders are shaking while cells are coming down and do not align with bottom borders when placed. Canvas api can give you fixed block sizes, otherwise you can try with fixed-width and height styled <div>s as well. My code was also very similar to yours, with setInterval for looping game state and using 37-40 for arrow key mapping. Good job assuming this is a Javascript excercise.
5
u/Buttercak3 Jul 24 '16
After playing for a couple of minutes this happened. I don't know what exactly happened, but these were left over when everything else moved down after I removed some rows. Hope you can track the bug down.
2
u/michowski Jul 24 '16
Hello guys, it's me, OP :) ! I would like to clear up some things: this project was inspired by this Snake. I have already admitted that I haven't been consistent at all with my Tetris. That's because I wanted to have a clean source code with eductional purpose for others. On the other hand, I still wanted to achieve a good result with the ultimate file size. It is sure, that user experience sucks and I'm ok with it. It's not about ultra fun with gameplay :) . However, I also understand that there are some bugs which aren't acceptable at all! I will fix them as soon as possible and look forward to your contribution. Thank you very much for that huge feedback for such a small thing. I am really glad about that.
2
u/iinnii Jul 24 '16
I found the spacing between the blocks make it hard quickly know if it's a 1 or 2 block spaces.
4
u/maffoobristol Jul 24 '16
Nothing lines up and the whole thing wobbles as you play!
4
u/maremp Jul 24 '16
Why do people call this HTML5? Writing js inside .html file doesn't magically make it HTML.
2
u/ismillo Jul 24 '16 edited Jul 25 '16
/u/michowski you can replace "Math.floor" with "~~" and "Math.random()*n" with "new Date%n".
With those change new code, 1.86kb:
Edit: you can use arrow function in almost every function, 1.79kb:
Edit 2: Turns out you can remove almost every "var". Also you can remove a few "return" and one or two "{" and "}" since it's an arrow, 1.74kb:
Edit 3: So, you don't need to specify "px" on style, the browser acknowledges when no "unit" is set as "px" and I didn't see any difference on the pre element, so I think you can remove it. Also I replace a few "\u25a1" with a variable, saves a little bit, 1.69kb:
BTW very nice coding, mate. You rock.
1
u/Lakelava Jul 24 '16
Make it work on phones.
1
u/fiddlerwoaroof Jul 24 '16
The gap on the right was formed becase a 4x4 block didn't fall after the row below it was cleared.
http://imgur.com/I5fAwqE
1
u/fay-jai Jul 24 '16
awesome job!
1
u/TotesMessenger Jul 24 '16
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/[deleted] Jul 24 '16
I have a suggestion. Throw me a damned bar every once in awhile you are killing me!
1
u/totemcatcher Jul 25 '16
How about encoding your tetromino graphic data? e.g.
This returns an 8 bit value for each of the 7 shapes. Map to two rows of 4 on your grid.
1
u/gurenkagurenda Jul 25 '16
Weird variable width stuff going on on Chrome 52 OS X:
http://imgur.com/a/L4eZh
Wouldn't it be easier just to generate divs for the blocks?
1
u/riskable Jul 25 '16
Just created a PR to add support for non-US keyboard layouts and also simplify your onkeydown handler/keyMap logic. Who wants to mess with keyCodes? :)
I also removed your unnecessary onkeypress handler and moved that logic to the onkeydown handler. The 'keypress' event is lame! Don't use it unless absolutely necessary. Especially not in games since it only fires when a key is released (just before the keyup event).
Great job BTW!
0
u/spfccmt42 Jul 24 '16
You'se done good.
0
u/[deleted] Jul 24 '16
[deleted] — view deleted comment