Skip to content

Instantly share code, notes, and snippets.

@iznax
Last active January 17, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iznax/d2e9b1f5659eac14d29717ebb76e6860 to your computer and use it in GitHub Desktop.
Save iznax/d2e9b1f5659eac14d29717ebb76e6860 to your computer and use it in GitHub Desktop.
Mini-Pede Game Package

Mini-Pede Package

Repackage javascript that can be run directly from github

Play Game

<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2018 by anonymous (http://jsbin.com/elupif/25/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>body{ font-family: monospace; font-size: 20px; text-align: center; }</style>
</head>
<body>
<b>Mini-Pede</b>
<br><br>
<div id="out"></div>​
<br><a href="https://gist.github.com">Source</a>
<script id="jsbin-javascript">
var W=16,R=W-1,M=W/2;
var H=12,B=H-1;
var x=M,y=B; // Player position x,y
var g=[]; // Grid of mushrooms
var w=[]; // List of worms
var b=-1; // Bullet position array index
var o=1; // Number of mushroom columns to drop
var t=0;
// Clear mushrooms with left side blocked.
for (var i=W*H+1; i--;) g[i]=i%W?0:2;
var F=[];F[H-1]=+1;F[H-3]=-1; // mark bounds for centipede bottom box
for (var i=W*H; i--;)
{
var j=(i/W)&63; F[i]=(j==H-1)?W:(j==H-3)?-W:0;
}
var notes = "*";
function Input(k)
{
// Ignore key if player is dead.
//k *= !p;
// Move left/right?
x -= k==37 & x>1;
x += k==39 & x<R;
// Move up/down?
y -= k==38 & y>(B-2);
y += k==40 & y<B;
// Fire bullet?
if (k==32 & b<0) b=x+y*W-W;
}
var drop = [2,1,2,2,1,3,1,2]
var Z = parseInt(101101011101001110110,2);
var Update=function(k,m,n)
{
if (!w[0])
{
// Drop a column of mushrooms
m = new Date&7;
var i = 0;
for (k=0; k<7; k++)
{
i += (Z>>(m+k)*2)&3;
//i += drop[(m+k)&7]
n = W+(i&1)*W;
//var i = (0x34862CA9>>(k+m))&15;
var pp=255;
for (;pp>>=1;n+=W)
{
//notes += "n="+n+" ";
g[n+i]|=pp&2;
}
}
//for(;k>>=1;n+=W) g[n]|=2*(k&1);
// Launch centipede after all mushrooms spawned
//if (!--o)
for (k=6;k--;)
w[k]=[k+1,1,W]; // position, left/right, up/down
}
// Check if bullet hits centipede?
for (k in w) //=0; k<w.length; ++k)
{
m = w[k];
if (b==m[0]) // bullet position == worm position?
{
g[b] = 2;
b=-1;
w.splice(k,1);
//o=6*!w[0];//.length;
}
// Kill player upon touching centipede.
y=m[0]==x+y*W?H:y;
}
// Check if bullet hit a mushroom?
// Decrement or clear mushroom?
// Move down or (-1) if bullet hit.
g[b]?g[b]+=b=-1:b-=W;
// Risk: always subtracting could wrap around for dead bullets?
if (++t % 3 < 1)
for (k in w) //=0; k<w.length; ++k)
{
m = w[k];
//m[0] = worm position
//m[1] = direction
//m[2] = up/down
// if (InBounds)MoveLeftRight; else MoveUpDown;
g[n=m[0]+m[1]]
?
(n=m[2],n=F[m[0]]==n?-n:n,w[k]=[m[0]+n,-m[1],n])
:
m[0]=n
}
//notes += "<br>";
}
function Draw()
{
var view = "";
var d=[];
for (var k in w)//=0; k<w.length; ++k)
{
var m=w[k];
d[ m[0] ] = 3; // Worm
}
for (var j=0; j<H; j++)
{
for (var i=0; i<W; i++)
{
var n = i+j*W;
var c = g[n]; // Bullet?
if (b==i+j*W) c=g[n]?6:5; // Explosion?
c = i==x&j==y?4:c; // Player?
if (d[n]) c=d[n];
c=y>B?3:c; // Game Over?
view += ".tTQ^|@"[c];
}
view += "<br>";
}
document.getElementById('out').innerHTML = view+"<br>"+notes;
}
document.onkeydown = function()
{
Input(event.keyCode);
Draw();
}
function Main()
{
Draw();
Update(0,0,0);
setTimeout(Main, 100);
}
Main();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment