Jane Wang

A dive into the world of startups, science, and technology

The Saga of Writing A Node.js Game

In my eager quest to learn to code better at Hacker School this summer, I’ve began working on a multiplayer game in Node.js, with websockets. For those who are new to programming, Node.js is a blazing fast event-based platform built on top of the Google’s V8 engine. It originally was intended for server side scripting, but became prevalent on the client. I must confess my growing love for Node.js, not only for its ability to actualize the original vision set out by the creators of JavaScript, but also for its lightweight and fast performance. As a side note, I believe that it is important for developers to consider performance and make a conscious decision to write code that is fast and clear.

Thursday: In the ideation stage, the concept of the multiplayer game came to me as something that would be fun to implement. After speaking to more advanced developers and getting rounds of thumbs up, I decided to make the game. Designing a game is exciting.

Friday: Enjoyed coding the universe with vectors. The universe component came together nicely, with gravity modelled after Newtonian mechanics.

Monday: With some mucking around in JavaScript, Icarus arises.

Tuesday: Major breakthrough. Forty lines of JavaScript was replaced with a single line of CSS. It is a moment of triumph and regret. Then, I encountered problem on performance while writing a collision detection algorithm. The chrome tab is using over 100% of CPU which means it is maxing out on one core. This leads to a due diligence on performance related docs online, specifically on how not to use if-statement and evals inside of computationally intensive loops. Redesign decisions to come. Tuesday evening was thus spent talking to seasoned game makers and going over some of the difficulties. The conversations were illuminating and I now have some idea as to how to proceed. Among the  conversations, the discussion with Mary Rose Cook is most illustrative. Mary has made a 2D canvas game and had run into similar performance-related issues.

Wednesday: Simmering and reading other people’s codes. Then, I took a break to read books and to play with Haskell. Haskell seems like a great language for those coming from pure math. In an act of brazen braggadocio, I showed my Instafun app to a designer friend visiting from abroad who suggested to put the app up and offered to re-skin it. How cool is that?!

Thursday: Some small incremental changes and a crude form of collision detection is working. CPU usage is still beyond the acceptable range. If Mozilla’s BrowserQuest can stay under 70% of a CPU, so should Icarus. That is a generous benchmark to use going forward.

Friday: A preliminary version of websocket is working and is broadcasting each player’s position all connected sockets, except the one on which it is being called. This is important, since in human-machine interaction, to avoid movement lags, the player should see his/her Icarus move with mouse movements, whereas other players positions are broadcasted by the server to the client.

Saturday: Since universe is instantiated on client side, in a multiplayer situation, each player is looking at a different screen and the collision detection algorithm is detecting collision in each instance of the universe. Time to rewrite the game logic of the universe on the server side. I decided to break universe and Icarus (player) into two models. Server side would have both models, so does client side. Socket.io and Backbone.js are going to be my closest companions in the coming week.

Monday: Spent the morning modeling out the new structure of the application on napkins. In the afternoon, I spend some time pairing with the Dave Albert, a founder at Hacker School. Dave is a great teacher. Together, we dug deeply into the finer details of Backbone.js, which also led us to read some of source code of Underscore.js. Reading blame and tracing through source code brought us to Jeremy Ashkenas’s code commits that were pithy and beautiful to me, and that handled corner cases. I found that reading source code with Dave was an eye-opening experience, partly because the code itself is well-written (beautiful JavaScript) and partly because my pairing partner Dave is both intensely curious and persistent. We almost forgot to eat dinner until Sonali texted us. Then we stayed in the dining room till 9:30 to figure out prototypical inheritance, execution context, currying, and binding. To note, I learned a lot from Dave, particular a good way to read and to quickly understand source codes. 

Tuesday: I paired more with Dave on Icarus’s server side logic. We’ve enhanced code structure, played with snazzy CoffeeScript, and worked through debugging. Dave took a break in the afternoon to become familiar with Backbone.js. I spent the afternoon wrestling with websockets. To note, non-blocking code is so much better than blocking codee. How could programmers live without it for so long? Also, out of curiosity, how does WoW, or any other large scaled multi-player games structure their server side and client side models?

[To continue…]

You can find the repository for this project here.This project, Icarus, is generous supported by the awesome folks at Hacker School and Etsy, who are changing the ratio of women in technology.

  • 26 June 2012
  • 1