How to teach yourself JavaScript, with video games
By: Travis Weston
Tags: Learn To Code, Learning To Code, JavaScript, Self Taught Developer, Screeps

Photo by Alex Knight on Unsplash

I’m a child of the 90s, born in the late 80s, who spent as much time with dirty on his knees as he did with a controller in his hand.

I am a gamer. In the classic sense of the word. I enjoy all kinds of games, beyond the common favorites of today.

I’m also, professionally, a web developer. I know JavaScript well enough, but I spend the majority of my day in PHP.

But here’s the thing: I was a programmer long before I was a professional. I fell in love with code when I was a kid, so it’s hard to turn it off. So how does one continue to love what they do, without burning out?

Tie it into a hobby.

I know how to write JavaScript, but I don’t know JavaScript. I can’t see the code in my head the way I can with PHP. I can’t imagine what it would be like to type that arrow function I just thought up. Or how to layout my exports so I can architect my project just right.

All of that comes with the amount of time that I just don’t have to devote strictly to JavaScript right now. So I have to fit that time in when I’m able. And that comes in the form of games.

Enter, Screeps

Screeps is about as niche of a video game as they come. Their subtitle is “MMO strategy sandbox game for programmers” and it is that, and more.

Without making this entire article an advertisement for Screeps, I’ll summarize it like this: Screeps provides you everything that your game requires, except the players.

That’s where you come in.

To play Screeps is to build Screeps. Your entire interaction with the game is by writing legitimate JavaScript code. Unlike some similar games like Gladiabots, that gives you a visual coding environment, Screeps forces you to write, and learn, actual JavaScript.

It’s easier if you already have some understanding of JavaScript to begin with, but if not, you can use it as a way to learn.

Which is what I’m doing.

What I’m teaching myself

With Screeps I have two distinct goals:

Learn ES6 syntax, including JavaScript classes Design a modular state machine

See, with Screeps you’re building the actual artificial intelligence for your Creeps — that’s what they call the characters that float around the screen. I’ve decided to learn a simple form of AI right now by designing a state machine, and I want to do so using JS Classes which I haven’t used in my day to day job yet.

I could learn this, and more, in other ways, I’m sure. I could go out and come up with an idea for a side hustle, and launch a SASS. I could take on projects at work that force me to dive more into the front end.

The problem is, all of those involves struggle without the little highs along the way that games give us.

Learning through play is actually something that I learned from my wife, who works with kids of all ages with development disabilities. She’s explained to me on numerous occasions how much she prefers teaching through play, and how much better it seems to work for her.

Given she’s smarter than I am, I figure there must be something to it.

Some of the benefits I’ve seen so far are:

Less Burnout

I have a lot of side projects already. I consult. I build affiliate websites. I design apps, and I build games. I even have an idea for a SASS, like I mentioned above.

The problem is, all of those use my work brain. Including the games. I’m not an expert in how the brain works, but I am the foremost expert in how my brain works. And my brain will always go into work mode if there is a dollar figure attached to my code.

With Screeps, I don’t have that. The code I write for Screeps is for me, and me alone. My code is my controller, and I’m not working, I’m playing.

As much as you would think it would burn me out, it actually has the opposite effect. I tend to only write my Screeps code over work vacations, simply because of the amount of time it requires. But whenever I come back from work, even if I’ve spent every day writing code, just like I do at my job, I am reinvigorated.

The combination of writing code for pleasure, like I used to, and spending so much time playing games, like I always want to, allows my work brain to relax. For however long I’m playing Screeps, I’m a kid again.

I’m not worried about bills. I’m not worried about the next release, or if that server is going down. I’m just worried about why this Creep has decided it wants to mine all of the energy from that source, and then just sit there doing nothing.

Experience with Build Tools I’ve never used

Before Screeps I had never used Grunt before. Now? I have a custom workflow that processes my development directory, flattens it into a single directory, and rewrites all of my requires. It then handles adding a version file, and uploads to my private Screeps server, committing to a Screeps branch with the same name as the Git branch that I’m developing on.

That seems like a lot, but it’s all due to the nature of how I prefer to develop, and how Screeps requires files to be laid out. Screeps doesn’t let you use folders, so if I have files laid out as Folder/Filename.js when I require it, I need to be able to rewrite both the filename and the require to use Folder_Filename.js otherwise things will break.

Sounds a heck of a lot like what we used to do in PHP before we had proper namespaces.

JavaScript Unit Testing

This is one that I haven’t really dived into much yet, but had never touched before now. I test my PHP code extensively. In fact, I wrote a whole article about test driven development, and why I think it separates the computer scientists from the random devs.

The problem is, I don’t write JavaScript code. So I don’t have all the tools figured out like I do in PHP.

That’s where Jist comes in. Right now I have it installed in my repository, and I’m prepared to use it. For now, though, I’m simply taking my time to learn the basics of Screeps, while learning some of the more advanced aspects of ES6.

Once I feel comfortable with both of those, I’ll likely wipe my repo, and start from scratch using TDD and Jist.

Am I going to become an expert?

No. Without hesitation no.

But I can surround myself with experts, and that’s good enough for me. The Screeps community is unique in that it is a self selected group of people who both love programming, and gaming.

Consider this: Where else can you talk about the intricacies of state pattern? What about custom building an operating system in JavaScript? How about event driven development?

All of those conversations can be had on the Screeps Slack, with other players who are as invested in gaming and Screeps as you are. Given some of the more advanced bots that you can see running around in the online worlds, you are definitely among experts there.