Created
July 25, 2015 22:47
-
-
Save nategraf/a4f678c978d574fb02c9 to your computer and use it in GitHub Desktop.
The first attempt at a game where each player programs an AI to control a blob which goes around and eats all of the smaller blobs. After this I went on to complete the concept by building upon Blockly Games instead of starting from scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var updater; | |
var Ybound = 800; | |
var Xbound = 900; | |
passiveStarve = 5; | |
moveStarve = 1; | |
var blobs = []; | |
var foods = []; | |
$(document).ready( function() { | |
$('#bounding-box').css('height',Ybound).css('width',Xbound); | |
blobs.push(new blob(P(100,130))); | |
blobs[0].setSpeed(P(1.0,1.0)); | |
blobs.push(new blob(P(300,150))); | |
blobs[1].setSpeed(P(-0.5,0)); | |
blobs.push(new blob(P(200,300))); | |
blobs[2].setSpeed(P(-1.0,-1.0)); | |
blobs.push(new blob(P(50,300))); | |
blobs[3].setSpeed(P(-1.0,1.0)); | |
updater = setInterval(update, 50); | |
}); | |
function update(){ | |
for(i=0; i<blobs.length; i++){ | |
blobs[i].update(); | |
} | |
for(i=0; i<foods.length; i++){ | |
foods[i].update(); | |
} | |
resolveCollisions(); | |
for(i=0; i<blobs.length; i++){ | |
if(blobs[i].dead){ | |
blobs.splice(i,1); | |
} | |
} | |
for(i=0; i<foods.length; i++){ | |
if(foods[i].eaten){ | |
foods.splice(i,1); | |
} | |
} | |
if(blobs.length == 0){ | |
$('#bounding-box').after('<p>All is DEAD!</p>'); | |
clearInterval(updater); | |
} | |
while(foods.length < 5){ | |
foods.push(new food(P())); | |
} | |
} | |
function resolveCollisions(){ | |
for(i=0; i<blobs.length; i++){ | |
for(j=i+1; j<blobs.length; j++){ | |
var disX = Math.pow(blobs[i].pos.x-blobs[j].pos.x,2); | |
var disY = Math.pow(blobs[i].pos.y-blobs[j].pos.y,2); | |
var dis = Math.pow(disX+disY,0.5); | |
if(dis < blobs[i].radius()+blobs[j].radius()){ | |
$('#'+blobs[i].elemId).css('background-color','#000000'); | |
$('#'+blobs[j].elemId).css('background-color','#000000'); | |
if(blobs[i].size>blobs[j].size){ | |
transferSize(blobs[i],blobs[j]); | |
} | |
else if(blobs[i].size<blobs[j].size){ | |
transferSize(blobs[j],blobs[i]); | |
} | |
else if(Math.random()<0.5){ | |
transferSize(blobs[j],blobs[i]); | |
} | |
else{ | |
transferSize(blobs[i],blobs[j]); | |
} | |
} | |
} | |
for(j=0; j<foods.length; j++){ | |
var disX = Math.pow(blobs[i].pos.x-foods[j].pos.x,2); | |
var disY = Math.pow(blobs[i].pos.y-foods[j].pos.y,2); | |
var dis = Math.pow(disX+disY,0.5); | |
if(dis < blobs[i].radius()+foods[j].radius()){ | |
$('#'+blobs[i].elemId).css('background-color','#ff00ff'); | |
$('#'+foods[j].elemId).css('background-color','#ff00ff'); | |
transferSize(blobs[i],foods[j]); | |
} | |
} | |
} | |
} | |
function transferSize(b1, b2){ | |
/*a1 = 3.14 * Math.pow(b1.size,2); | |
a2 = 3.14 * Math.pow(b2.size,2); | |
a1 += a2/5; | |
a2 -= a2/5; | |
b1.size = Math.pow(a1 / 3.14,0.5); | |
b2.size = Math.pow(a2 / 3.14,0.5);*/ | |
b1.size += b2.size/5; | |
b2.size -= b2.size/5; | |
} | |
function Pair(X, Y){ | |
this.x = X; | |
this.y = Y; | |
this.mag = function(){ | |
return Math.pow(Math.pow(this.x,2)+Math.pow(this.y,2),0.5); | |
} | |
} | |
function P(X,Y){ | |
// Make a random value as default if no args are passed. | |
X = typeof X !== 'undefined' ? X : Math.random()*Xbound; | |
Y = typeof Y !== 'undefined' ? Y : Math.random()*Ybound; | |
return new Pair(X,Y); | |
} | |
function blob(point){ | |
this.size = 5000; | |
this.pos = point; | |
this.dead = false; | |
var speed = P(0,0); | |
// Blob and four random digits | |
this.elemId = "blob" + Math.floor(Math.random()*10000); | |
this.radius = function(){ | |
return Math.pow(this.size/3.14,0.5); | |
} | |
this.setSpeed = function(speedpair){ | |
speed = speedpair; | |
} | |
this.update = function(){ | |
if(!this.dead){ | |
this.pos.x = this.pos.x + (speed.x); | |
this.pos.y = this.pos.y + (speed.y); | |
this.size = this.size - passiveStarve - Math.pow(speed.mag(),2)*moveStarve; | |
if(this.size < 2){ | |
this.dead = true; | |
} | |
if(this.pos.x < this.radius()){ | |
this.pos.x = this.radius(); | |
speed.x = 0; | |
} | |
else if(this.pos.x > Xbound - this.radius()){ | |
this.pos.x = Xbound - this.radius(); | |
speed.x = 0; | |
} | |
if(this.pos.y < this.radius()){ | |
this.pos.y = this.radius(); | |
speed.y = 0; | |
} | |
else if(this.pos.y > Ybound - this.radius()){ | |
this.pos.y = Ybound - this.radius(); | |
speed.y = 0; | |
} | |
this.draw(); | |
} | |
} | |
this.initDraw = function(){ | |
$('#bounding-box').append('<div class="blob" id='+this.elemId+'/>'); | |
} | |
this.draw = function(){ | |
if(this.dead){ | |
$('#'+this.elemId).remove(); | |
} | |
else{ | |
$('#'+this.elemId).css('left',this.pos.x-this.radius()); | |
$('#'+this.elemId).css('top',this.pos.y-this.radius()); | |
$('#'+this.elemId).css('height',this.radius()*2); | |
$('#'+this.elemId).css('width',this.radius()*2); | |
$('#'+this.elemId).css('border-radius',this.radius()); | |
} | |
} | |
// When the blob is constructed, draw it. | |
this.initDraw(); | |
} | |
function food(point) { | |
this.pos = point; | |
this.size = 500; | |
this.elemId = "food" + Math.floor(Math.random()*10000); | |
this.eaten = false; | |
this.radius = function(){ | |
return Math.pow(this.size/3.14,0.5); | |
} | |
this.update = function(){ | |
if(this.size < 2){ | |
this.eaten = true; | |
} | |
this.draw(); | |
} | |
this.initDraw = function(){ | |
$('#bounding-box').append('<div class="food" id='+this.elemId+'/>'); } | |
this.draw = function(){ | |
if(this.eaten){ | |
$('#'+this.elemId).remove(); | |
} | |
$('#'+this.elemId).css('left',this.pos.x-this.radius()); | |
$('#'+this.elemId).css('top',this.pos.y-this.radius()); | |
$('#'+this.elemId).css('height',this.radius()*2); | |
$('#'+this.elemId).css('width',this.radius()*2); | |
$('#'+this.elemId).css('border-radius',this.radius()); | |
} | |
this.initDraw(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment