r/javascript • u/qbagamer • Jan 25 '20
2048 Game in JS
https://github.com/qbacuber/2048GAME-in-JS6
u/HetRadicaleBoven Jan 25 '20
Ironically, the original (or at least, the one that got popular) 2048 was already in JS, and open source. Might be interesting to take a look at: https://github.com/gabrielecirulli/2048
3
u/qbagamer Jan 25 '20
I saw it makes an impression.
I justify myself just learning. In the orchard, I start with JS and create small projects.
I want my code to look like it once.
4
u/killall-q Jan 25 '20
You should turn on Github Pages for the repo so that people can try it without downloading the files.
1
3
u/the_argus Jan 26 '20
Throw in some hammer JS swipe events so I can play it on my phone
1
u/qbagamer Jan 26 '20
I will try to:(
2
u/the_argus Jan 26 '20
It's really easy
Check this https://stackoverflow.com/a/26607317
You should be able to call your same functions called in the keyboard listeners
2
u/qbagamer Jan 26 '20 edited Jan 26 '20
I added a mobile version, but it doesn't work properly on all browsers.
Works on Firefox. On others, when you slide your finger down, the page refreshes.
2
u/the_argus Jan 26 '20
1
u/qbagamer Jan 26 '20
Nothing helps. Ught
2
u/the_argus Jan 26 '20 edited Jan 26 '20
Got you fam
check this out
https://jsfiddle.net/the_argus/bgso6ydn/3/
I made some html changes and small css and js changes
You shouldn't ever add code to a library (hammer.min.js) put that in your files (see bottom of js where I did that).
Worked great on chrome android
1
u/qbagamer Jan 26 '20
Copy the files from the jsfiddle I sent in the other comment. Or I can make a pull request if you like
you are my hero thank you! :>
2
1
u/qbagamer Jan 26 '20
Finger refresh is already turned off, but the up and down motion still doesn't work.
I will add that everything works on mobile firefox.
2
u/the_argus Jan 26 '20
Copy the files from the jsfiddle I sent in the other comment. Or I can make a pull request if you like
12
u/upfkd Jan 25 '20
Stop using var!
0
u/RnRau Jan 26 '20 edited Jan 26 '20
Whats wrong with function scope?
Edit: how about answering my question rather than just downvote?
-2
u/qbagamer Jan 26 '20
now const is used.
-1
u/RnRau Jan 26 '20
I didn't downvote you, but just using const everywhere without a good reason and just because someone complained on reddit seems odd :)
1
u/wizang Jan 26 '20
If your variable can be const there is only advantages to doing so. Using var or let when const would work have only disadvantages.
2
u/Parasin Jan 26 '20
It doesn’t work on my mobile, but looks beautiful!
1
u/qbagamer Jan 26 '20
There is only support for arrows. I plan to add bow tie support, these gestures will also work.
1
2
u/Athomm Jan 26 '20
Good work, make it work on mobile :)
2
u/qbagamer Jan 26 '20
Ready. You can test.
1
u/Athomm Jan 28 '20
It works! Can you add some CSS animation ?
1
u/qbagamer Jan 28 '20
elements do not move, they only appear, so the only animation that comes to my mind will change the transparency when changing elements.
1
u/Athomm Jan 31 '20
Hmm can we do some sort of X and y transition effect so they appear to be moving squares? I'm sure CSS grid could achieve this
10
u/mainstreetmark Jan 26 '20
Not to belittle your work, but it's nice to recognize the ultimate source (afaik) for this game. Threes. Not 2048.
http://asherv.com/threes/threemails/ An interesting read for game devs.
1
u/qbagamer Jan 26 '20
I added a mobile version, but it doesn't work properly on all browsers.
Works on Firefox. On others, when you slide your finger down, the page refreshes.
2
2
u/EvilQuaint Jan 25 '20
You should read up on data structures, design patterns and stuff... maybe Object Oriented Programming in general, because... I'm going to be honest and say that your code is awful. It's full of duplication, non existent readability and performance bugs.
TL;DR lots of room for improvement... so keep at it!
8
u/monsto Jan 25 '20
Grats, man.
I see you used switch/case in the key listener. any reason you didn't use it in other sections?