Skip to content

Instantly share code, notes, and snippets.

@iznax
Created March 7, 2012 06:54

Revisions

  1. iznax revised this gist Mar 7, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ This is a miniature version of the classic Frogger arcade game.
    It is written in the smallest amount of JavaScript that I could squeeze it.
    The core logic of the game is a single function that fits in less than [140](http://140byt.es/) characters.

    [**Play Game**](http://jsbin.com/ojojix/12)
    [**Play Game**](http://jsbin.com/ojojix/13)

    ### Display

  2. iznax revised this gist Mar 7, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -37,4 +37,5 @@ The update function can be stripped of white-space and formatted to in less than
    See my other games:

    [**Mini Tetris**](http://jsbin.com/ujewuk)

    [**Mini Invaders**](http://jsbin.com/ujovip/5)
  3. iznax revised this gist Mar 7, 2012. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -30,4 +30,11 @@ The update function is the core logic of the game. It processes all game data fo

    ### Compact Version ( 123 characters )

    The update function can be stripped of white-space and formatted to in less than 140 characters of text which is small enough for tweeting etc.
    The update function can be stripped of white-space and formatted to in less than 140 characters of text which is small enough for tweeting etc.

    ### Information

    See my other games:

    [**Mini Tetris**](http://jsbin.com/ujewuk)
    [**Mini Invaders**](http://jsbin.com/ujovip/5)
  4. iznax revised this gist Mar 7, 2012. 3 changed files with 95 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions License.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    The "WTH" Public License

    Copyright (C) 2012 Paul Isaac

    Everyone is permitted to copy and distribute verbatim or modified
    copies of this license document, and changing it is allowed as long
    as the name is changed.

    #. You can do whatever-the-heck you like with this code.
    12 changes: 12 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {
    "name": "MiniFrogger",

    "description": "Mini version of Frogger game in less than 140 bytes",

    "keywords": [
    "game",
    "javascript",
    "frogger",
    "arcade"
    ]
    }
    74 changes: 74 additions & 0 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    <html>
    <style>body{ font-family: monospace; font-size: 20px}</style>
    <div id="out"></div>
    </html>

    <script>

    var W=16,R=W-1,M=W/2;
    var H=12,B=H-1;
    var V=18; // View size bits to wrap-around?
    var Z = 0x6666; // Top line pattern
    var x=M,y=B; // Player position x,y
    var p=0; // Player animation
    var t=0; // Time counter

    // Water logs and car traffic
    var r=[0,0x0e38f,0x38f3c,0x383e0,0x39e3c,0,85,1048,31+(63<<9),85<<8,2048|9,0];

    function Input(k)
    {
    // Ignore key if player is dead.
    k *= !p;
    // Move left/right?
    x -= k==37 & x>0;
    x += k==39 & x<R;
    // Move up/down?
    y -= k==38 & ((y==1&Z>>x) | y>1);
    y += k==40 & y<B;
    // Check if you hop into danger?
    p+=r[y]>>x&!p;
    }

    var Update=function(g,i)
    {
    for (i=H; --i;)
    if (!(t%4) | i>5)
    //{
    g=r[i],
    r[i] = i&1?(g&1)<<V|g>>1:g>>V&1|g<<1;
    //}
    p<3?t++:(p=0,x=M,y=B);
    p+=p?1:r[y]>>x&1;
    }

    function Draw()
    {
    var view = "";
    for (var j=0; j<H; j++)
    {
    for (var i=0; i<W; i++)
    view += "#O~=:.#@X%%"[i==x&j==y?p+7:
    y?j?2+(j<5?!(r[j]&1<<i):2+!!r[j]+(j>5?(r[j]&1<<i)>0:0)):Z>>i&1:1];
    view += "<br>";
    }
    document.getElementById('out').innerHTML = view;
    }

    // IE requires implicit 'event' parameter?
    document.onkeydown = function()
    {
    Input(event.keyCode);
    Draw();
    }

    function Main()
    {
    Update(0,0);
    Draw();
    setTimeout(Main, 300);
    }

    Main();

    </script>
  5. iznax revised this gist Mar 7, 2012. 3 changed files with 23 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    This is a miniature version of the classic Frogger arcade game.
    It is written in the smallest amount of JavaScript that I could squeeze it.
    The core logic of the game is a single function that fits [140](http://140byt.es/) characters.
    The core logic of the game is a single function that fits in less than [140](http://140byt.es/) characters.

    [**Play Game**](http://jsbin.com/ojojix/12)

    @@ -28,6 +28,6 @@ The core logic of the game is a single function that fits [140](http://140byt.es

    The update function is the core logic of the game. It processes all game data for the next display frame. This is the function that I am trying to minimize. I realize it's cheating a bit to ignore the size of the outer script, but it's the benchmark I have chosen since the display mechanism is arbitrary and less important, for my purposes.

    ### Compact Version ( 140 characters )
    ### Compact Version ( 123 characters )

    The update function can be stripped of white-space and formatted to fit in 140 characters of text. This is small enough to satisfy the 140-byte limit for tweeting etc.
    The update function can be stripped of white-space and formatted to in less than 140 characters of text which is small enough for tweeting etc.
    19 changes: 19 additions & 0 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    var Update=function(g,i)
    {
    // For each line in the game board.
    for (i=H; --i;)
    // If time cycle is 1/4 or line is below middle sidewalk.
    if (!(t%4) | i>5)
    //{
    // Read the current bits for log/car traffic
    g=r[i],
    // Shift (odd) rows left with wrap-around
    // Shift (even) rows right with wrap-around
    r[i] = i&1?(g&1)<<V|g>>1:g>>V&1|g<<1;
    //}
    // If player has not reach (0,1,2,3) end of death animation advance timer.
    // otherwise reset the player to the bottom middle of the screen.
    p<3?t++:(p=0,x=M,y=B);
    // Advance the player animation if already dead or newly hit by traffic.
    p+=p?1:r[y]>>x&1;
    }
    1 change: 1 addition & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    function(g,i){for(i=H;--i;)if(!(t%4)|i>5)g=r[i],r[i]=i&1?(g&1)<<V|g>>1:g>>V&1|g<<1;p<3?t++:(p=0,x=M,y=B);p+=p?1:r[y]>>x&1}
  6. iznax revised this gist Mar 7, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Mini Invaders
    # Mini Frogger

    This is a miniature version of the classic Frogger arcade game.
    It is written in the smallest amount of JavaScript that I could squeeze it.
  7. iznax created this gist Mar 7, 2012.
    33 changes: 33 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    # Mini Invaders

    This is a miniature version of the classic Frogger arcade game.
    It is written in the smallest amount of JavaScript that I could squeeze it.
    The core logic of the game is a single function that fits [140](http://140byt.es/) characters.

    [**Play Game**](http://jsbin.com/ojojix/12)

    ### Display

    #OO##OO##OO# O Lilly pad
    ~~==~~~~==~~ ~ Water
    ~~~~====~~~~ = Log
    :::::::::::: : Grass
    ............ . Road
    ..##...##... # Truck
    .....@...... @ Frog You
    :::::::::::: : Sidewalk

    ### Controls

    Left Arrow = Move left
    Right Arrow = Move right
    Up Arrow = Move up
    Down Arrow = Move down

    ### The Update Function

    The update function is the core logic of the game. It processes all game data for the next display frame. This is the function that I am trying to minimize. I realize it's cheating a bit to ignore the size of the outer script, but it's the benchmark I have chosen since the display mechanism is arbitrary and less important, for my purposes.

    ### Compact Version ( 140 characters )

    The update function can be stripped of white-space and formatted to fit in 140 characters of text. This is small enough to satisfy the 140-byte limit for tweeting etc.