Skip to content

Instantly share code, notes, and snippets.

@iznax
Created March 7, 2012 06:54
Show Gist options
  • Save iznax/1991541 to your computer and use it in GitHub Desktop.
Save iznax/1991541 to your computer and use it in GitHub Desktop.
Mini Frogger

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 characters.

Play Game

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.

@aemkei
Copy link

aemkei commented Mar 7, 2012

Awesome! I love the concept of JS games that fit into a tweet!

@aemkei
Copy link

aemkei commented Mar 7, 2012

Btw: to make it a valid entry for http://140byt.es you should avoid leaking to the global scope, (such as H, t, V, ...).
But impressive anyway!

Update: This should fix the global scope:

function(g,i,H,t,i,g,V,p,x,M,B,y,r){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}

It's 144 bytes so how do we get rid of 4 chars here?

Update 2: Hmm, when passing the vars as arguments, we have no chance to override them outside.

@iznax
Copy link
Author

iznax commented Mar 7, 2012

You passed g,i, twice in your revision which is 4-chars, but would have to add a large statement to return the changes to p,x,y,r.

@iznax
Copy link
Author

iznax commented Mar 7, 2012

I had to adopt some alternate rules to make an "interactive" script that doesn't spend 30% of the budget on passing constants & state in/out of the function. The rules also don't allow my onkeydown() and draw() functions to exist... so I'm just trying to make some compromises that don't increase the overall size of the script, just to reduce the size of a single function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment