Game Development Reference
In-Depth Information
With the help of the web messaging API, we can set up a snake, where the snake is
not constrained to a single window. Imagine the possibilities when we combine this
clever API with another very powerful HTML5 feature, which just happens to lend it-
self incredibly well to games - web sockets. By combining web messaging with web
sockets, we could play a game of snake, not only across multiple windows, but also
with multiple players at the same time. Perhaps each player would control the snake
when it got inside a given window, and all players could see all windows at the same
time, even though they are each using a separate computer. The possibilities are
endless, really.
Surprisingly, the code used to set up a multi-window port of snake is incredibly
simple. The basic setup is the same, we have a snake that only moves in one dir-
ection at a time. We also have one or more windows where the snake can go. If we
store each window in an array, we can calculate which screen the snake needs to
be rendered in, given its current position. Finding out which screen the snake is sup-
posed to be in, given its world position, is the trickiest part.
For example, imagine that each window is 200 pixels wide. Now, suppose there are
three windows opened. Each window's canvas is only 200 pixels wide as well, so
when the snake is at position 350, it would be printed too far to the right in all of
Search WWH ::




Custom Search