Slashdot Mirror


Javascript Game of Tron In 226 Bytes

Have you upgraded your hardware to play something beefier than 140-byte Tetris? New submitter alokmenghrajani writes with "a detailed view of how we size-optimized a game of Tron to just 226 bytes." It's also optimized for Chrome, and very fast.

24 of 115 comments (clear)

  1. 1KB Chess For The Sinclair by TheGoodNamesWereGone · · Score: 5, Informative

    1KB Chess for the Sinclair still has that beat.

    1. Re:1KB Chess For The Sinclair by lobiusmoop · · Score: 5, Informative

      Actually it was only 672 bytes - the 1024 byte memory had to include the screen memory also, much like shared video memory today - could take up to 768 of the 1024 bytes for a full 32x24 screen! (the chess game only used an 11-line screen for the board etc)

      And it's even considered by some to be the greatest program ever written.

      --
      "I bless every day that I continue to live, for every day is pure profit."
    2. Re:1KB Chess For The Sinclair by Anonymous Coward · · Score: 3, Interesting

      And what about the 20 MB browser and all other cram below it? I can do a 3D FPS in 0 lines of code by just inserting a DVD into my PS3! Yay for me!

    3. Re:1KB Chess For The Sinclair by hairyfeet · · Score: 2, Insightful

      Not to mention this is NOT "Tron" this is just a variation on slither. These don't even look like light cycles they are just dots. Lame. And since when do we not count all the overhead of the browser? I'm sure you could get a 5Kb checkers game by using all the existing code in ubuntu or Win 7 but we wouldn't say that it was really a 5Kb checkers game would we?

      --
      ACs don't waste your time replying, your posts are never seen by me.
  2. Re:portability by hobarrera · · Score: 4, Interesting

    It works on chrome and firefox. This isn't a portability issue: normally developers add huge amounts of code to support IE. If it needs tweaking for IE, it would probably be 50% larger.

  3. Re:Damn. by TheRaven64 · · Score: 4, Informative
    Works fine in Safari. That said, if you press a key other than ijkl it does something random. I assumed wasd would work, and was very confused until I read the instructions. It's also single player, so you basically drive around until you hit your own wall - there's no way of winning. It's more a drawing program with a crappy UI than a game. Making it only 226 bytes of source isn't that impressive, it's basically:
    • Change direction if key is pressed
    • Test if pixel next in that direction is white, if so exit
    • If not, set it to white, set current location to that address
    • Repeat.

    I wouldn't be surprised if you could write the same game in under 226 bytes of Z80 or 6502 assembly, so doing it in a high-level language seems much less impressive.

    --
    I am TheRaven on Soylent News
  4. 2 player? by Ambitwistor · · Score: 5, Informative

    It's not really a game of Tron without a competing lightcycle. (Without fruit, it's not really a game of Snake either.)

    1. Re:2 player? by Lorkki · · Score: 3, Interesting

      Nor is it really Tetris without tetraminoes or scoring. Not to berate the effort, but it's a lot easier to downsize something when you drop half of the main features.

  5. Calling this a game of "Tron" is a stretch by readandburn · · Score: 5, Insightful
    It should be called "Drawing-A-Line-In-A-Tiny-Black-Square-With-Terrible-Controls".

    Come to think of it, that might make a better movie than that last Tron.

  6. This again... by zAPPzAPP · · Score: 4, Funny

    226 byte which I am sure include library calls, only runs on top of the multi megabyte of browser code, may use various other resources as well.

    By this standard, I can write Tron in 1 bit.

    1

    There.
    Call it the "Run Tron Bit". It runs on top of a full impletation of Tron.

    1. Re:This again... by sco08y · · Score: 3, Interesting

      226 byte which I am sure include library calls, only runs on top of the multi megabyte of browser code, may use various other resources as well.

      By this standard, I can write Tron in 1 bit.

      1

      There.
      Call it the "Run Tron Bit". It runs on top of a full impletation of Tron.

      Anything that has a display of some sort has library calls to control that display. If you were writing this on an old C-64, it had a comprehensive sprite library that performed collision detection and perfectly synchronized updates, without you doing more than setting a few registers carefully. If you were playing 6502 machine code golf, you could probably write this in half the bytes. Switching to a graphics mode was (IIRC) a single assignment, clearing the screen was a function provided by the BASIC interpreter, and polling for key events was a simple call. Updating the display, naturally, involved writing to a predesignated area of memory. You could even switch back to text mode to print out GAME OVER when you were done.

      In this example, the library calls, which are deliberately designed to be fairly verbose in order to promote readability, chew up much of their byte budget with verbose keywords like onkeyup, getContext, fillRect, etc.

      Also, since you're dealing with complex libraries that provide lots features, code has to turn off some of those features. In this case, they actually generate javascript errors and ignore them. They then have to go through handling key events, handling movement and such, and all with some very cleverly compressed techniques. For instance, they grab the mod of the keyvalue and use that as an index to an array of values to update the player's direction.

    2. Re:This again... by Ksevio · · Score: 2

      The challenge was to write the game in standard javascript - and he did that. It wasn't to include the graphics display drivers (not all that interesting a problem these days). It's not like he included a jquery library (or anything really). Just 221 bytes (in the latest version) that you can post into an html file and run on your computer (no need for internet access either!)

    3. Re:This again... by zAPPzAPP · · Score: 2

      That's the source code before I run it through my self made, super optimized compiler.

  7. This is going to make me sound old but by SDrag0n · · Score: 2

    Our old low-bit graphics games used to do something or have some goal...

    --
    I don't have time to make a sig
  8. Even Shorter by BrokenCube · · Score: 3, Interesting

    The current version of the code (on the linked site) is only 221 bytes long - and I've had a quick play with it and it can be reduced further to 219 bytes by swapping out the boolean ANDs "&&" for bitwise ANDs "&" twice.

    1. Re:Even Shorter by alokmenghrajani · · Score: 2

      your are right, thanks! I'll update the code & give you credits.

  9. Re:Controls? by Anonymous Coward · · Score: 3, Informative

    IJKL instead of WASD or arrow keys. It saves space because IJKL is actually in alphabetical order, which makes it a neat modulus away from being the four directions.

  10. Same thing in x86 asm by Juippi · · Score: 4, Informative

    This was fun, because I got pretty much the same thing down to 56 bytes in x86 assembly some 15 years ago. I remember the best entry in the competition I wrote it for being around 48 bytes or so; I missed at least one trick for setting the graphics segment more efficiently, and also something related to either collision handling or keyboard input, don't remember which.
    In any case, this is possibly the right version of the code. Should compile with NASM, and is even playable in Dosbox with arrow keys if you turn the emulation speed as low as it can go.

    1. Re:Same thing in x86 asm by Mr+Z · · Score: 2

      There's only 56 bytes that the computer has to read from somewhere to have a runnable program. In the case of Javascript, since there's no compiled bytecode, you have to download and run from the source. For assembly language, I think it's fair to measure the binary that results from assembling, since it's pretty much a 1:1 translation at that level.

      Under DOS, I believe you could actually key the whole program's machine code in with Alt-Keypad sequences using "copy con: foo.com" as long as your program didn't have a ^Z in it. Would you count the source code length then as the number of keystrokes on the keyboard then, or the number of bytes produced? (And if so, are we going to count all the shift keypresses for typing curly braces and parentheses in the Javascript program? Ha.)

      Take a look at that 672 byte chess program someone mentioned earlier. You don't hear anyone talking about the size of the source code or even the fact that to create the program image requires a machine with 3K. The resulting program image that the machine has to load (mixture of BASIC and machine code) takes 672 bytes of RAM.

  11. And how much supporting code? by davidbrit2 · · Score: 4, Insightful

    How many megabytes of supporting code are necessary to run those 226 bytes?

  12. not tron by eyenot · · Score: 5, Funny

    I think there's a difference between Tron and Suicidal Etch-A-Sketch

    --
    "Stratigraphically the origin of agriculture and thermonuclear destruction will appear essentially simultaneous" -- Lee
  13. Etch-A-Sketch by tepples · · Score: 4, Funny

    Suicidal Etch-A-Sketch

    Please leave Governor Romney out of this.

  14. I set the interval to 50... by bartoku · · Score: 2

    Now it is like I am playing in Matrix time and I rock!

  15. Re:Damn. by chrismcb · · Score: 2

    Apparently they don't consider "how to play the game" as part of the game. Hit i,j,k,l (well any key really)