Skip to content

Instantly share code, notes, and snippets.

@bsawyer
Last active June 5, 2020 08:35
Show Gist options
  • Save bsawyer/bc0cb3ec3a86b29e76a23104fc44f6d1 to your computer and use it in GitHub Desktop.
Save bsawyer/bc0cb3ec3a86b29e76a23104fc44f6d1 to your computer and use it in GitHub Desktop.
Blob Jump is a side scrolling game you can play in your browsers URL bar. Add as a bookmarklet to play on any page.
(function(){
let running = false;
const levelLength = 42;
const startLevel = new Array(levelLength + 1).join('_');
let level = startLevel;
const hurdle = '‗';
const air = '■';
let jump = false;
let frame = 0;
let score = 0;
let i;
function tick(){
if(!running){
if(i){
clearInterval(i);
i = null;
}
return;
}
const h = nextFrame();
window.location.hash = h;
}
function nextFrame(){
if(random(1,3) === 1 && level.slice(-1 * frames.length).indexOf(hurdle) === -1){
level += hurdle;
}else{
level += '_';
}
level = level.substr(1);
const c = character();
if(c !== air && level[0] === hurdle){
level = startLevel
running = false;
frame = 0;
jump = false;
const msg = formatMsg('[SCORE:_' + score + ']_(p:restart)');
score = 0;
return msg;
}else if(level[0] === hurdle){
score += 1;
}
return '_' + c + level;
}
function random(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
const frames = [
'▅',
'▅',
'▄',
'▃',
'▁',
'▁',
'▃',
'▄',
'▆',
'■',
'■',
'■',
'■',
'▅',
'▄',
'▃',
'▁',
'▃',
'▃',
'▄',
'▅'
];
function character(){
if(jump){
frame++;
if(frame >= frames.length){
frame = 0;
jump = false;
}
}
return frames[frame];
}
function start(){
i = setInterval(tick, 1000 / 12);
}
function formatMsg(msg){
const padding = startLevel.substr(levelLength / 2 + Math.floor(msg.length / 2));
return padding + msg + padding;
}
window.addEventListener('keydown', evt => {
if(evt.key === 'p'){
evt.preventDefault();
running = !running;
if(running){
start();
}else{
window.location.hash = formatMsg('[PAUSED]_(p:resume)');
}
}else if(evt.key === ' '){
jump = true;
evt.preventDefault();
}
});
window.location.hash = formatMsg('[BLOB_JUMP!]_(p:play/pause)_(space:jump)');
})();
@bsawyer
Copy link
Author

bsawyer commented Jun 4, 2020

javascript:!function(){let n=!1;const t=42,e=new Array(t+1).join("");let o=e;const r="‗",a="■";let s,i=!1,l=0,u=0;function c(){if(!n)return void(s&&(clearInterval(s),s=null));const t=function(){1===(t=1,s=3,Math.round(Math.random()(s-t)+t))&&-1===o.slice(-1h.length).indexOf(r)?o+=r:o+="";var t,s;o=o.substr(1);const c=function(){i&&++l>=h.length&&(l=0,i=!1);return h[l]}();if(c!==a&&o[0]===r){o=e,n=!1,l=0,i=!1;const t=d("[SCORE:"+u+"](p:restart)");return u=0,t}o[0]===r&&(u+=1);return""+c+o}();window.location.hash=t}const h=["▅","▅","▄","▃","▁","▁","▃","▄","▆","■","■","■","■","▅","▄","▃","▁","▃","▃","▄","▅"];function d(n){const o=e.substr(t/2+Math.floor(n.length/2));return o+n+o}window.addEventListener("keydown",t=>{"p"===t.key?(t.preventDefault(),(n=!n)?s=setInterval(c,1e3/12):window.location.hash=d("[PAUSED](p:resume)")):" "===t.key&&(i=!0,t.preventDefault())}),window.location.hash=d("[BLOB_JUMP!](p:play/pause)(space:jump)")}();

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