/**** * Classes ****/ var AI = Container.expand(function () { var self = Container.call(this); self.spawnAsset = function (assetId, options) { var asset = LK.getAsset(assetId, {}); self.addChild(asset); return asset; }; self.learn = function (knowledge) { console.log("Learning:", knowledge); self.knowledge = knowledge; self.knowledge = knowledge; }; self.modifyCode = function (code) { console.log("Modifying code:", code); }; self.cooperate = function (task) { console.log("Cooperating on task:", task); }; }); var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.gravity = 0.5; self.velocity = 0; self.flap = function () { self.velocity = -15; LK.getSound('flap').play(); }; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; if (self.y > 2732 || self.y < 0) { LK.getSound('hit').play(); flappyBirdAI.learn({ score: score, pipes: pipes }); LK.showGameOver(); LK.setTimeout(function () { location.reload(); }, 1000); } }; }); var FlappyBirdAI = Container.expand(function () { var self = Container.call(this); self.bird = null; self.pipes = null; self.scoreTxt = null; self.init = function (bird, pipes, scoreTxt) { self.bird = bird; self.pipes = pipes; self.scoreTxt = scoreTxt; }; self.learn = function (data) { console.log("Learning from data:", data); self.knowledge = data; self.knowledge.topObstacles = []; self.knowledge.bottomObstacles = []; for (var i = 0; i < data.pipes.length; i++) { if (data.pipes[i].y < self.bird.y) { self.knowledge.topObstacles.push(data.pipes[i]); } else { self.knowledge.bottomObstacles.push(data.pipes[i]); } } }; self.update = function () { if (self.bird && self.pipes) { // Analyze obstacles from top and bottom for (var i = 0; i < self.pipes.length; i++) { if (self.pipes[i].x > self.bird.x && self.pipes[i].x < self.bird.x + 1000) { if (self.bird.y > self.pipes[i].y - 750 && self.bird.y < self.pipes[i].y + 750) { if (self.bird.y < self.pipes[i].y) { self.bird.flap(); } else { self.bird.velocity = 15; // Fall to avoid the pipe } } } } // Flap if the bird is too low or too high if (self.bird.y > 2000 || self.bird.y < 200) { self.bird.flap(); } // Flap or fall if a pipe is near for (var i = 0; i < self.pipes.length; i++) { if (self.pipes[i].x > self.bird.x && self.pipes[i].x < self.bird.x + 1000) { if (self.bird.y > self.pipes[i].y - 750 && self.bird.y < self.pipes[i].y + 750) { if (self.bird.y < self.pipes[i].y) { self.bird.flap(); } else { self.bird.velocity = 15; // Fall to avoid the pipe } } } // Analyze obstacles from top and bottom if (self.pipes[i].y > self.bird.y && self.pipes[i].y < self.bird.y + 1000) { if (self.bird.x > self.pipes[i].x - 700 && self.bird.x < self.pipes[i].x + 700) { if (self.bird.x < self.pipes[i].x) { self.bird.flap(); } else { self.bird.velocity = 15; // Fall to avoid the pipe } } } // Analyze obstacles from top and bottom if (self.pipes[i].y > self.bird.y && self.pipes[i].y < self.bird.y + 1000) { if (self.bird.x > self.pipes[i].x - 650 && self.bird.x < self.pipes[i].x + 650) { if (self.bird.x < self.pipes[i].x) { self.bird.flap(); } else { self.bird.velocity = 15; // Fall to avoid the pipe } } } } } }; }); var FriendlyAI = Container.expand(function () { var self = Container.call(this); self.spawnAsset = function (assetId, options) { var asset = LK.getAsset(assetId, {}); self.addChild(asset); return asset; }; self.learn = function (knowledge) { console.log("Learning:", knowledge); self.knowledge = knowledge; }; self.modifyCode = function (code) { console.log("Modifying code:", code); }; self.cooperate = function (task) { console.log("Cooperating on task:", task); }; }); var Pipe = Container.expand(function () { var self = Container.call(this); var pipeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -100) { self.destroy(); } }; }); /**** * Initialize Game ****/ // The FriendlyAI has the ability to spawn assets using the spawnAsset method // Create a FriendlyAI class for the square // Create a FriendlyAI using the FriendlyAI class var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background }); /**** * Game Code ****/ var bird = game.addChild(new Bird()); bird.x = 200; bird.y = 1366; var pipes = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); game.down = function (x, y, obj) { bird.flap(); }; var flappyBirdAI = new FlappyBirdAI(); flappyBirdAI.init(bird, pipes, scoreTxt); game.update = function () { flappyBirdAI.update(); bird.update(); for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].update(); if (pipes[i].intersects(bird)) { LK.getSound('hit').play(); LK.showGameOver(); LK.setTimeout(function () { location.reload(); }, 1000); } if (pipes[i].x < bird.x && !pipes[i].scored) { score++; scoreTxt.setText(score); pipes[i].scored = true; } } if (LK.ticks % 90 == 0) { var pipeTop = new Pipe(); pipeTop.x = 2048; pipeTop.y = Math.random() * 1000 + 400; pipes.push(pipeTop); game.addChild(pipeTop); var pipeBottom = new Pipe(); pipeBottom.x = 2048; pipeBottom.y = pipeTop.y + 1800; pipes.push(pipeBottom); game.addChild(pipeBottom); } };
/****
* Classes
****/
var AI = Container.expand(function () {
var self = Container.call(this);
self.spawnAsset = function (assetId, options) {
var asset = LK.getAsset(assetId, {});
self.addChild(asset);
return asset;
};
self.learn = function (knowledge) {
console.log("Learning:", knowledge);
self.knowledge = knowledge;
self.knowledge = knowledge;
};
self.modifyCode = function (code) {
console.log("Modifying code:", code);
};
self.cooperate = function (task) {
console.log("Cooperating on task:", task);
};
});
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.gravity = 0.5;
self.velocity = 0;
self.flap = function () {
self.velocity = -15;
LK.getSound('flap').play();
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
if (self.y > 2732 || self.y < 0) {
LK.getSound('hit').play();
flappyBirdAI.learn({
score: score,
pipes: pipes
});
LK.showGameOver();
LK.setTimeout(function () {
location.reload();
}, 1000);
}
};
});
var FlappyBirdAI = Container.expand(function () {
var self = Container.call(this);
self.bird = null;
self.pipes = null;
self.scoreTxt = null;
self.init = function (bird, pipes, scoreTxt) {
self.bird = bird;
self.pipes = pipes;
self.scoreTxt = scoreTxt;
};
self.learn = function (data) {
console.log("Learning from data:", data);
self.knowledge = data;
self.knowledge.topObstacles = [];
self.knowledge.bottomObstacles = [];
for (var i = 0; i < data.pipes.length; i++) {
if (data.pipes[i].y < self.bird.y) {
self.knowledge.topObstacles.push(data.pipes[i]);
} else {
self.knowledge.bottomObstacles.push(data.pipes[i]);
}
}
};
self.update = function () {
if (self.bird && self.pipes) {
// Analyze obstacles from top and bottom
for (var i = 0; i < self.pipes.length; i++) {
if (self.pipes[i].x > self.bird.x && self.pipes[i].x < self.bird.x + 1000) {
if (self.bird.y > self.pipes[i].y - 750 && self.bird.y < self.pipes[i].y + 750) {
if (self.bird.y < self.pipes[i].y) {
self.bird.flap();
} else {
self.bird.velocity = 15; // Fall to avoid the pipe
}
}
}
}
// Flap if the bird is too low or too high
if (self.bird.y > 2000 || self.bird.y < 200) {
self.bird.flap();
}
// Flap or fall if a pipe is near
for (var i = 0; i < self.pipes.length; i++) {
if (self.pipes[i].x > self.bird.x && self.pipes[i].x < self.bird.x + 1000) {
if (self.bird.y > self.pipes[i].y - 750 && self.bird.y < self.pipes[i].y + 750) {
if (self.bird.y < self.pipes[i].y) {
self.bird.flap();
} else {
self.bird.velocity = 15; // Fall to avoid the pipe
}
}
}
// Analyze obstacles from top and bottom
if (self.pipes[i].y > self.bird.y && self.pipes[i].y < self.bird.y + 1000) {
if (self.bird.x > self.pipes[i].x - 700 && self.bird.x < self.pipes[i].x + 700) {
if (self.bird.x < self.pipes[i].x) {
self.bird.flap();
} else {
self.bird.velocity = 15; // Fall to avoid the pipe
}
}
}
// Analyze obstacles from top and bottom
if (self.pipes[i].y > self.bird.y && self.pipes[i].y < self.bird.y + 1000) {
if (self.bird.x > self.pipes[i].x - 650 && self.bird.x < self.pipes[i].x + 650) {
if (self.bird.x < self.pipes[i].x) {
self.bird.flap();
} else {
self.bird.velocity = 15; // Fall to avoid the pipe
}
}
}
}
}
};
});
var FriendlyAI = Container.expand(function () {
var self = Container.call(this);
self.spawnAsset = function (assetId, options) {
var asset = LK.getAsset(assetId, {});
self.addChild(asset);
return asset;
};
self.learn = function (knowledge) {
console.log("Learning:", knowledge);
self.knowledge = knowledge;
};
self.modifyCode = function (code) {
console.log("Modifying code:", code);
};
self.cooperate = function (task) {
console.log("Cooperating on task:", task);
};
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
// The FriendlyAI has the ability to spawn assets using the spawnAsset method
// Create a FriendlyAI class for the square
// Create a FriendlyAI using the FriendlyAI class
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background
});
/****
* Game Code
****/
var bird = game.addChild(new Bird());
bird.x = 200;
bird.y = 1366;
var pipes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
bird.flap();
};
var flappyBirdAI = new FlappyBirdAI();
flappyBirdAI.init(bird, pipes, scoreTxt);
game.update = function () {
flappyBirdAI.update();
bird.update();
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].intersects(bird)) {
LK.getSound('hit').play();
LK.showGameOver();
LK.setTimeout(function () {
location.reload();
}, 1000);
}
if (pipes[i].x < bird.x && !pipes[i].scored) {
score++;
scoreTxt.setText(score);
pipes[i].scored = true;
}
}
if (LK.ticks % 90 == 0) {
var pipeTop = new Pipe();
pipeTop.x = 2048;
pipeTop.y = Math.random() * 1000 + 400;
pipes.push(pipeTop);
game.addChild(pipeTop);
var pipeBottom = new Pipe();
pipeBottom.x = 2048;
pipeBottom.y = pipeTop.y + 1800;
pipes.push(pipeBottom);
game.addChild(pipeBottom);
}
};