Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 8 additions & 38 deletions ants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function AntFarm() {

ctx = can.getContext('2d');

generateDirt();

ctx.fillRect(0,0,realWidth, realHeight);

initAnts();
loop();

Expand All @@ -57,7 +59,6 @@ function AntFarm() {
function Ant(){
this.x = Math.floor(Math.random()*width);
this.y = 0;
// this.speed = 1;
var thisAnt = this;

this.actions = [
Expand Down Expand Up @@ -96,6 +97,9 @@ function AntFarm() {

for (i = 0; i < l; i++) {
a = ants[i];

x = a.x;
y = a.y;


// Check if dirt is not under ant
Expand All @@ -105,7 +109,7 @@ function AntFarm() {
else {
al = a.actions.length;
// Cause more left to right
r = Math.floor(Math.random()*al*0.9);
r = Math.floor(Math.random()*al*0.85);
a.actions[r]();

}
Expand All @@ -114,56 +118,22 @@ function AntFarm() {
dirt[a.x][a.y] = 0;
}


drawPixel(x,y, 'white');
drawPixel(a.x, a.y, 'red');
}
}

function loop() {
render();
antLoop();
frames ++;
setTimeout(loop, 0);
}

function generateDirt() {
var x;
var y;

for (x = 0; x < width; x ++) {
for (y = surfaceBase; y >= 0; y --) {
dirt[x][y] = 0;
}
}
}

function drawPixel(x, y, color) {
var realX = x * zoom;
var realY = y * zoom;

ctx.fillStyle = color;
ctx.fillRect(realX, realY, zoom, zoom);
}

function clear() {
ctx.clearRect(0, 0, realWidth, realHeight);
}

function renderDirt() {
var x;
var y;

for (x = 0; x < width; x ++) {
for (y = 0; y < height; y ++) {
if (dirt[x][y]) {
drawPixel(x, y, 'black');
}
}
}
}

function render() {
clear();
renderDirt();
}
}