r/learnprogramming • • Dec 10 '17

Learn JavaScript By Coding Flappy Bird And Snake Games.

1.7k Upvotes

58 comments sorted by

179

u/Wilfred-kun Dec 10 '17

Learn JavaScript .....

It was not quite what I was expecting. I expected a multi-video tutorial for both games, starting from the very basics of JS. But if you have no background experience with JS this is difficult to follow and although you might understand what is happening in a general sense, I don't think you will learn a lot of JS by watching this.

I am not saying the videos are bad, but the title gives (well, me at least) the wrong impression of what's in the box.

31

u/th3originals Dec 10 '17

If someone had learn a thing or two from the videos that's enough. people can't learn everything in one video, and I assumed that people who just started JavaScript, this will make them to keep up learning the language. and I'm so sorry if title was a bit "misleading".

6

u/ungulateCase Dec 11 '17

I like your attitude. Very cool of you to want to help people learn.

2

u/th3originals Dec 11 '17

thank you.

3

u/k4kuz0 Dec 11 '17

I would be personally not recommend you putting "Best explanation ever" in the title of your video if you want your videos to be taken seriously. That sort of thing makes me click away very fast.

2

u/th3originals Dec 11 '17

thanks for the advice.

21

u/Wilfred-kun Dec 10 '17

Can someone explain to me why this comment got so many dislikes? I think he actually made a fair point.

31

u/[deleted] Dec 10 '17

Maybe the "I'm so sorry" that just sounds sarcastic and dismissive of any concerns over whether it's misleading.

I didn't downvote, but I can see why someone might.

13

u/Askee123 Dec 10 '17

Reddit is a fickle bitch sometimes vOv

0

u/[deleted] Dec 10 '17

[deleted]

18

u/MRH2 Dec 10 '17

This is an excellent tutorial and I love your explanations! Thanks so much.

You cover a lot, so hopefully people have some background ahead of time. The event stuff could be confusing. Also you could explain how to find out the keycodes for the keys (e.g write something that prints the number of the key on the screen when you press it).

I had no idea that JS had array.pop and array.unshift. In Java you have to write these things yourself. Except that you can't change the size of an array -- it's fixed. So what we have to do is add another variable to record the size of the snake and only display that many elements (or use an array list which can change size).

Calling a variable "d" is not really as nice as calling it "dir".

Eventually, I would use constants for the board size, so that you can change them without having to rewrite the whole code (ie. no magic numbers). Oh. but then you have to change your background image. How about just adding some code to draw the background yourself with different shades of green squares? The only time I use a background image for a game like this is if the board is complicated like snakes and ladders or monopoly.

The audio in JS is so easy. I'm jealous!

3

u/Wulfys Dec 10 '17 edited Dec 10 '17

I'm really new to programming but couldn't you use ArrayList for Java?

Edit: oops I'm blind you mentioned it

2

u/MRH2 Dec 10 '17

or use an array list which can change size.

yes.

1

u/th3originals Dec 10 '17

thanks a lot for this comment, it gives me a lot of positive energy.

and I agree with all you mentioned above, and It will be considered in my next videos.

I've never tried to code in JAVA, it sounds very complicated, and yes you must be jealous, JavaScript is so easy. but still it has a weird part. a very weird one.

1

u/MRH2 Dec 10 '17

I'll look at your other ones in the next week. This one is clear and concise and has a lot of content. It is great for people who can learn quickly.

1

u/th3originals Dec 11 '17

yep and thanks a lot

1

u/FriesWithThat Dec 10 '17 edited Dec 10 '17

write something that prints the number of the key on the screen when you press it

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
  <div>
    <p class="keycode" style="text-align:center;line-height:200px;font-size:3em"></p>
  </div>
  <script>
    window.addEventListener('keydown', function(event) {
      // console.log(event);
      const key = event.key;
      const el = document.querySelector('.keycode');
      el.innerHTML = key;
    });
  </script>
</body>
</html>

1

u/monsto Dec 11 '17

alright here's a little personal self test, but I want to see how right I am, if you don't mind...

<script>
  window.addEventListener('keydown', (event) => {
    document.querySelector('.keycode').innerHTML = event.key;
  });
</script>

Is about as distilled as this could get since the 2nd argument really couldn't be anything other than a function. So we're left with this.

Yeah?

1

u/FriesWithThat Dec 12 '17 edited Dec 12 '17

That works! Though in most scenarios you're going to want a reference to that selector to do something with later; such as to add or remove a class from that element. Not sure how familiar you are with the event that gets captured, but if not do take a look at what gets returned in Chrome Dev Tools by that console log. A whole huge object full of properties. What is more likely to be useful is to kind of do the opposite, and capture they keyCode, to help you map things out. For instance if you were making a game you could type WASD and look it up right there (= 87|65|83|68). So for that, just switch to event.keyCode. Here's a great quick walkthrough that covers just this sort of thing.

1

u/monsto Dec 12 '17

Excellent, thanks.

1

u/drboyfriend Dec 10 '17

Also you could explain how to find out the keycodes for the keys (e.g write something that prints the number of the key on the screen when you press it).

http://keycode.info/

3

u/looserloser Dec 11 '17

I watched the snake game video and thought it was great the way you explained it, but one thing I noticed is that a couple of times you made a mistake in the code and then fixed it without saying anything, like using = instead of == in conditional statements or putting a variable assignment statement after a statement where the variable is referenced, things like that. If someone just learning JS is following along and typing the code out, they'd have no idea why it isn't working if they didn't happen to catch the half second of video where 4 ='s magically become 4 =='s. If you have to fix stuff like that,it would be good to point it out just in case someone is following along. Good job though.

1

u/th3originals Dec 11 '17

I agree. and thanks a lot

8

u/sarevok9 Dec 10 '17

As someone who knows Javascript --

I find a lot of your variables to be named a bit too confusingly. bg and fg for background and foreground may seem like immutable concepts to you -- but what if you were creating a football (American football) where fg could be field goal? or BG could mean backgammon, battleground or any number of other things given the context of the game that's being made. Making variable names descriptive and using the content-filling of your IDE to offset the keystrokes of longer variable names is always worth it if someone else is going to be looking at your code.

Furthermore, I'm not entirely sure who your audience is -- but ternary operators are generally not "beginner friendly" as the syntax is a bit weird for people just getting started. These are definitely a fine intermediate tutorial series, but I'm not sure that these are going to be of much use to people attempting to begin programming due to the speed at which you switch between concepts, the fact that you provide a great deal of the resources rather than show how they were generated and the fact that you assume that the user have a base of knowledge that will allow them to understand some fairly difficult topics.

6

u/LittleAccountOfCalm Dec 10 '17

i don't think any beginner would dive into canvas, so ternary are just fine in this context..

3

u/papivebipi Dec 10 '17

there is no logical reason to omit ternary operator, if you understand "if else" you understand them. shielding beginners from them only gives the illusion that they are advanced and confuses them in the long run.

1

u/th3originals Dec 10 '17

all you mentioned above, will be considered, thanks a lot.

1

u/MRH2 Dec 10 '17

No!

fg and bg are just fine. We always infer the meaning of words from their context.

Ternary operators are not for beginners but neither is this video. If beginners use this, then what they need to do is watch the video and then do research on any command that they do not understand, and then try and write the program from scratch without looking at video or source code, to see if they really understand it.

2

u/OrionLightning Dec 10 '17

Damnnn seems really good tutorial. Do you know any other videos just like that but about Java?

1

u/th3originals Dec 10 '17

no sorry I'm not familiar with JAVA. and I don't know any.

2

u/Jonno_FTW Dec 11 '17

Java and JavaScript are worlds apart.

1

u/Gofnutz Dec 11 '17

Java is to JavaScript like Car is to Carpet.

1

u/MRH2 Dec 10 '17

I'm sure that there are some. I watched one last year about making sudoku or something. I made one to show Java animations - a ball bouncing on a screen. Maybe I'll do a series about that. But it was using a library called HSA2 that handled all of the messy Swing stuff that takes a long time to learn.

2

u/muuuggg Dec 11 '17

This looks promising

1

u/michaellai Dec 13 '17

Thanks mate :) https://snaky-hipz.glitch.me/ this will be the first project in my portfolio. You are a legend! BTW are you african? (from your accent).

1

u/th3originals Dec 14 '17

Yes I am :) glad the tutorial helped you.

-9

u/Seankps Dec 10 '17

But js is usually not used for stuff like that. So why learn that? I'm seriously curious

39

u/th3originals Dec 10 '17

the best way to learn something is learning while having fun.

3

u/tvi016 Dec 10 '17

Agree so much, i made a game with javascript and i learned the basics of javascript really well. And after that it's easier to learn other things

2

u/th3originals Dec 10 '17

yeah somethings could be weird at the beginning, but once you understand the thing, you really do.

16

u/k4kuz0 Dec 10 '17

Programming skills are very transferable, so you don't need to learn Javascript within its typical problem domain in order to gain useful skills.

14

u/SemanticRomantic Dec 10 '17

Why wouldn't JS be used for something like that? Browser games are a huge market, and JS is a wildly dynamic language to say the least.

11

u/sportif11 Dec 10 '17

Because "learn javascript by creating a boring line of business CRUD app" doesn't have the same pizzaz

7

u/LovingThatPlaid Dec 10 '17

What else is commonly used for web based games?

0

u/Wilfred-kun Dec 10 '17

AFAIK Java, Flash, Unity

0

u/Seankps Dec 11 '17

How commonly are web based games used for employment?

6

u/MRH2 Dec 10 '17

why do you say that JS is not used for web based games?

0

u/Seankps Dec 11 '17

it's used for all sorts of things. Probably less than 1% of which is web based games. I just feel like newcomers could benefit from more marketable js skills.

1

u/filleduchaos Dec 11 '17
  1. Not everything is about 'marketability'

  2. Obviously the skills you pick up through game dev are applicable in lots of other contexts - working with canvases, HTML5 audio, animations, etc

4

u/Wilfred-kun Dec 10 '17

Mozilla disagrees :^ )

7

u/sarevok9 Dec 10 '17

Amusingly, javascript is used for pretty much EVERYTHING right now. Front end / backend development can be done in js /css / html now with very little need for other languages to process anything. The MEAN (Mongo, Express, Angular, Node) stack is new and sexy and used by lots of startups. Meteor is gaining a lot of traction since it leverages react / angular and ties in node / meteor automatically.

Doing any kind of screen rendering in javascript isn't the most natural thing in the world, but with html5 canvas becoming the new norm and flash being pushed out, we're going to see a lot more of it.

1

u/polargus Dec 10 '17

Actually only half of MEAN is still popular. Angular and Mongo fell out of favour a while ago, replaced with React/Redux and pretty much any other DB solution. I’d learn MySQL/Postgres over Mongo any day of the week.

1

u/sarevok9 Dec 10 '17

I don't disagree with you, though it really depends on the use case. Angular has been mostly replaced by react.... But I hear Angular 4 isn't a total shit show

4

u/Plazmatic Dec 10 '17

JS is the best thing for web based games... and I'm a C, C++ and Python developer...

0

u/[deleted] Dec 10 '17

[deleted]

2

u/WhatAHaskell Dec 10 '17

No need to be rude about it

0

u/[deleted] Dec 11 '17

[deleted]

1

u/meh6927 Dec 11 '17

amazing