Skip to content

Instantly share code, notes, and snippets.

@iznax
Created March 4, 2012 00:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iznax/1969402 to your computer and use it in GitHub Desktop.
Save iznax/1969402 to your computer and use it in GitHub Desktop.
Mini Invaders

Mini Invaders

This is a miniature version of the classic Space Invaders 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 bytes.

Play Game

Display

.........
..#.#.#..    
..#.#.#..    #  Invader
.........
....i....    i  Bullet
.........
....^....    ^   You
---------

Controls

Left  Arrow = Move left
Right Arrow = Move right
Up    Arrow = Fire

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 key 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 ( 126 characters )

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

// Assign to a variable to measure function body.
var Update=
function(k,h)
{
// Check for bullets hitting enemies
k=1<<b-x; // positional bit pattern of bullet
t-=h=e==y ?t&k:0; // remove any enemy hits from top line
w-=k=e==y+1?w&k:0; // remove any enemy hits from bottom line
// Move bullet up (or hide off-screen)
e -= h|k ? H:1;
// Reset enemy wave when all destroyed?
t|w ? 0 :(x=4,y=0,t=w=Z);
// Check if enemy lines are blocked on left/right sides?
(t|w) & 1<<(d>0)*R-x ?
(y++,d=-d) : // move down and change directions
x+=d // move left/right
}
function(k,h){k=1<<b-x;t-=h=e==y?t&k:0;w-=k=e==y+1?w&k:0;e-=h|k?H:1;t|w?0:(x=4,y=0,t=w=Z);(t|w)&1<<(d>0)*R-x?(y++,d=-d):x+=d}
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.
{
"name": "MiniInvaders",
"description": "Mini version of Space Invaders in less than 140 bytes",
"keywords": [
"game",
"javascript",
"space",
"invaders"
]
}
<html>
<style>body{ font-family: monospace; font-size: 20px}</style>
<div id="out"></div>
</html>
<script>
var W=16,R=W-1; // Screen Width
var H=10,B=H-1; // Screen Height
var p=W/2; // Player position X
var x=4,y=0; // Enemy top/left position
var d=1; // Enemy direction -1,+1
var t=w=Z=85 // Enemy top/bot bit pattern
var b=0,e=-1; // Bullet position X,Y
var Update=function(k,h)
{
// Check for bullets hitting enemies
k=1<<b-x;
t-=h=e==y ?t&k:0;
w-=k=e==y+1?w&k:0;
// Move bullet up or hide
e-=h|k?H:1;
// Reset when all enemies destroyed?
t|w?0:(x=4,y=0,t=w=Z);
// Check if enemy lines are blocked on left/right sides?
(t|w) & 1<<(d>0)*R-x ?
(y++,d=-d) : // move down and change directions
x+=d // move left/right
}
function Draw()
{
var view = "";
for (var j=0; j<H; j++)
{
for(var i=0;i<W;i++)
{
var c = (i==p & j==B)?3:0; // Player?
if (j==y) c=t&(1<<(i-x))?1:c; // Enemy?
if (j==y+1) c=w&(1<<(i-x))?1:c; // Enemy?
c = (i==b & j==e)?(c==1)?4:2:c; // Bullet?
// Has bottom of wave touched ground?
c = y-!w+1<B?c:5; // Game Over?
view += ".#i^@*"[c];
}
view += "<br>";
}
document.getElementById('out').innerHTML = view+"----------------";
}
// IE requires implicit 'event' parameter?
document.onkeydown = function()
{
var k=event.keyCode;
p -= k==37 & p>0; // Move left?
p += k==39 & p<R; // Move right?
if (k==38 & e<0) b=p, e=H-2; // Fire bullet?
Draw();
}
function Main()
{
Update(0,0);
Draw();
setTimeout(Main, 300);
}
Main();
</script>
@JavaScript-Packer
Copy link

<h1><pre id=x><script>function a(a,b,f,j){for(j="",a=0;10>a;a++){for(b=0;16>b;b++)f=b==c&9==a?3:0,a==e&&(f=g&1<<b-d?1:f),a==e+1&&(f=w&1<<b-d?1:f),f=b==h&a==i?1==f?4:2:f,f=9>e-!w+1?f:5,j+=".#i^@*"[f];j+="\n"}x.innerHTML=j}function b(){var c=0,j=0,c=1<<h-d;g-=j=i==e?g&c:0,w-=c=i==e+1?w&c:0,i-=j|c?10:1,g|w?0:(d=4,e=0,g=w=Z),(g|w)&1<<15*(f>0)-d?(e++,f=-f):d+=f,a(),setTimeout(b,300)}var c=8,d=4,e=0,f=1,g=w=Z=85,h=0,i=-1;this.onkeydown=function(b){b=event.keyCode,c-=37==b&c>0,c+=39==b&15>c,38==b&0>i&&(h=c,i=8),a()},b()</script>
<!-- 527 bytes complete game -->

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