JavaScript Code Tetris Game
I have long heard that someone on the internet just wrote a Tetris game with 60 lines of JavaScript code, and recently looked at it, and today in this article I have done my analysis (mainly in the form of annotations).
I use C to write a basic full-featured Tetris, it takes about 1000 lines of code to look like. So the 60 line at first glance is still very surprising.
But after reading the code, it turns out that the entire program does not use any arcane technology, but rather simplifies the code by using some tools or the techniques of JavaScript itself.
summed up mainly the following three points
1. Use eval to generate JavaScript code, reducing the volume of code
2. Use a string as the game scene data, using regular expressions to find and match, eliminating the usual should be manually written find validation code
3. Manage Tetris data and scene data in binary mode, simplifying comparisons and validations with bitwise operations
In addition, the original author code line is very small, the code is relatively compact, which is the result of this program only 60 lines of one reason.
Here's the code after my typeset comment.
1 <!doctype html> 2 The original author code is given below, 60 lines
1 <!doctype html>Category: C/Tetris game JavaScript code