Skip to content

Instantly share code, notes, and snippets.

@seanmtracey
Last active December 29, 2015 20:28
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 seanmtracey/7723609 to your computer and use it in GitHub Desktop.
Save seanmtracey/7723609 to your computer and use it in GitHub Desktop.
A Javascript version of Space Invaders that deletes a random Facebook friend whenever you shoot an enemy.
var invaders=function(){function m(e){(new AsyncRequest).setURI("/ajax/profile/removefriendconfirm.php").setData({uid:e,norefresh:true,unref:"profile_gear"}).send()}function g(e){$.ajax({type:"GET",url:"https://www.facebook.com/ajax/typeahead/first_degree.php?viewer="+e+"&__user="+e+"&__a=1",success:function(e){var t=e;t=t.substring(9,t.length-1);t+="}";t=JSON.parse(t);var n=[];for(var r=0;r<t.payload.entries.length;r+=1){if(t.payload.entries[r].type=="user"){n.push(t.payload.entries[r].uid)}}v=n.length;p=n.length/10;for(var i=0;i<p;i+=1){for(var r=0;r<d;r+=1){w(window.innerWidth/d*r+50,window.innerHeight/2-(i*50+20),n[10*i+r])}}},dataType:"text"});counter.innerHTML=n.length+" friends";y()}function y(){t.clearRect(0,0,e.width,e.height);t.save();t.fillStyle="rgb(0,255,0)";var i=0;furthest.left=0;furthest.right=0;furthest.down=0;while(i<n.length){if(n[i].x<n[furthest.left].x){furthest.left=i}else if(n[i].x>n[furthest.right].x){furthest.right=i}else if(n[i].y>n[furthest.down].y){furthest.down=i}i+=1}var s=0;while(s<lasers.length){lasers[s].y-=20;t.fillRect(lasers[s].x-5,lasers[s].y,10,50);var a=n.length-1;while(a>=0){var f=n[a];if(lasers[s].x>f.x-10&&lasers[s].x<f.x+10){if(lasers[s].y<f.y+10&&lasers[s].y>f.y-10){if(f.y>-10){m(f.uid);counter.innerHTML=n.length+" friends";lasers[s].y=-55;n.splice(a,1);if(a==furthest.left){furthest.left=0}else if(a==furthest.right){furthest.right=0}else if(a==furthest.down){furthest.down=0}a=0}}}a-=1}if(lasers[s].y<-50){lasers.splice(s,1)}s+=1}t.restore();t.save();t.fillStyle="rgb(255,255,255)";var p=0;while(p<n.length){t.fillRect(n[p].x-10,n[p].y-10,20,20);if(l){if(h==="left"){n[p].x-=20}else if(h==="right"){n[p].x+=20}else if(h==="down"){n[p].y+=20}if(p==n.length-1){(function(){l=false;if(n[furthest.left].x<50&&h!=="down"||n[furthest.right].x>window.innerWidth-50&&h!=="down"){h="down";for(var e=0;e<d;e+=1){}}else{if(n[furthest.left].x<50){h="right"}else if(n[furthest.right].x>window.innerWidth-50){h="left"}}if(n[furthest.down].y>window.innerHeight-50){alert("You lose");window.location="https://www.facebook.com/me/friends"}if(n.length/v<.8&&n.length/v>.6){o=2}else if(n.length/v<.6&&n.length/v>.4){o=2.5}else if(n.length/v<.4&&n.length/v>.2){o=3}else if(n.length/v<.2){o=16}setTimeout(function(){l=true},1e3/o)})();(function(){if(c){var e=Math.random().toFixed(1);if(e<=.4){c=false;var t=n[Math.round(Math.random()*20)];r.push({x:t.x,y:t.y})}setTimeout(function(){c=true},1e3)}})()}}p+=1}t.restore();t.save();t.fillStyle="rgb(255,0,0)";t.fillRect(ship.x-20,ship.y-10,40,20);t.fillStyle="rgb(255,255,255)";var g=0;while(g<r.length){r[g].y+=20;t.fillRect(r[g].x-5,r[g].y,5,25);if(r[g].x>ship.x-20&&r[g].x<ship.x+20&&r[g].y>ship.y-10&&r[g].y<ship.y+10){if(ship.shields>0){ship.shields-=1;r[g].y=window.innerHeight+10;t.save();t.fillStyle="rgb(0,0,255)";t.fillRect(ship.x-30,ship.y-20,58,40);t.restore()}else{alert("Yo Dead");c=false;l=false;return}}if(r[g].y>window.innerHeight){r.splice(g,1)}g+=1}t.restore();u(y)}function b(){lasers.push({x:ship.x,y:ship.y-20})}function w(e,t,r){n.push({x:e,y:t,uid:r})}function E(){window.addEventListener("mousemove",function(e){ship.x=e.pageX},false);e.addEventListener("click",function(e){e.preventDefault();e.stopPropagation();b()},false);window.addEventListener("keyup",function(e){if(e.keyCode===39||e.keyCode===37){a=false;s=.1}},false);window.addEventListener("resize",function(){e.width=window.innerWidth;e.height=window.innerHeight;ship.y=window.innerHeight-20},false)}function S(){var n=document.createElement("canvas"),r=document.createElement("div"),i="canvas"+Date.now()*1;n.setAttribute("id",i);r.setAttribute("id","friendCount"+Date.now()*1);n.style.position="fixed";n.style.top="0";n.style.left="0";n.style.padding="0";n.style.margin="0";n.style.backgroundColor="black";n.style.zIndex="2000000";document.body.appendChild(n);document.body.appendChild(r);e=n;t=e.getContext("2d");counter=r;e.width=window.innerWidth;e.height=window.innerHeight;var s=document.createElement("script");s.src="https://code.jquery.com/jquery-1.10.2.min.js";document.body.appendChild(s);E();ship.x=window.innerWidth/2-20;ship.y=window.innerHeight-20;var o=document.head.innerHTML,u=o.indexOf('"id"'),a=o.substring(u,u+20);a=parseInt(a.replace(/^\D+/g,""));(function(){setTimeout(function(){g(a)},3e3)})()}var e,t;var n=[],r=[];ship={x:undefined,y:undefined,shields:3},lasers=[],furthest={left:1,right:1,down:1},counter=undefined;var i=1,s=.1,o=1;var u=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame;window.requestAnimationFrame=u;var a=false,f="left",l=true,c=true,h="right";var p=5,d=10,v;return{init:S}}();(function(){invaders.init()})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment