MLH LHD 2018 - Twitch Plays Tetris

An in progress board from a game of Tetris. A line piece is about to fall into a well on the left side of the board, only clearing the bottom two lines. The next piece shown will be a T piece, and there is no piece being held in reserve.

For the 2018 MLH Local Hack Day, I decided to try to make a “Twitch Plays” style of game. I began by working on the Twitch side of the system, which comprises of a Python script that connects via IRC to the Twitch channel’s chatroom, and a second Python script that runs a websocket server. Whenever the websocket server receives a message, it broadcasts it to everything that has connected to it - which under normal circumstances will be the IRC script and the browser running the game. (Incidentally, the university’s network blocked the ports required for IRC, so I ran these two scripts on a Microsoft Azure virtual machine.)

The game itself was written in HTML, CSS and Javascript. It’s not especially well put together, but serves the job well enough - if I was to rewrite it, then I would try to take a more object-oriented approach as opposed to the vast number of hard-coded switch/case statements and direct array accesses to the board. Regardless, when the IRC script detects a chat message that fits one of the valid commands (such as l, r, etc.) it sends a message to the websocket server, which in turn sends a message to the game. When the game receives a message, it will try to carry out an action, such as swapping the piece that’s dropping into the hold section, or moving the piece from side to side, or rotating the piece. The piece will drop automatically every second, assuming that there hasn’t been a drop command sent.

It’s also possible to play the game locally, as the JS also contains a key handling function which will imitate messages being sent from the server when the arrow keys or the c key are pressed. This came in useful during the writing of the game, as it meant I didn’t need to spam the chat window every time. A limitation of the project is that Twitch won’t let you send the same message in a 30 second window, or let you send too many messages in too short an amount of time. This can be overcome by giving the desired user a “mod” or “vip” role in the channel.

You can see the project in action here:

The hack was submitted for the both the “Best Open Source Hack on GitHub” and “Best Microsoft Azure Hack” categories, and of the 53 submissions for the Azure category, it was selected to be one of the three winners.

code GitHub publicWebsite

Written by Profile photo for James Cordon James Cordon | First published: | Last updated:
Link to this page: https://mrjamesco.uk/projects/twitch_plays_tetris/