Skip to content

Instantly share code, notes, and snippets.

@philippeantoine
Created October 27, 2010 19:19
Show Gist options
  • Save philippeantoine/649748 to your computer and use it in GitHub Desktop.
Save philippeantoine/649748 to your computer and use it in GitHub Desktop.
2010-10-27 codingdojo boids
<!doctype html>
<html>
<head>
<script src="raphael.js"></script>
<style>body{width:400px;margin:auto;font-family:monospace}</style>
</head>
<body id="test">
<div id="boids"></div>
<script>
var b={x:0,y:0};
var Boid = function (x,y) {
this.x=x;
this.y=y;
this.vx=1;
this.vy=1;
this.circle = paper.circle(x, y, 10).attr({fill:'black'}); // rajouté apres dojo
}
Boid.prototype.step=function(){
this.x += this.vx;
this.y += this.vy;
this.circle.translate(this.vx, this.vy); // rajouté apres dojo
}
function createboids(nb,rand){
boids=[];
for (var i=0; i<nb;i++){
boids.push(new Boid(rand(),rand()));
}
return boids;
}
</script>
<script>
window.onload=function init(){
setup();
//tdd();
}
var rnd =function(){
return Math.ceil(Math.random() * 400);
}
//setup rajouté apres dojo
function setup(){
stop = true, w=400, h=400, numboids = 30;
paper = Raphael("boids", w, h);
createboids(numboids,rnd);
function play(){
for(var i = 0; i < numboids; i++) {
boids[i].step();
}
if(!stop) setTimeout(arguments.callee, 50);
};
document.onclick = function() {
stop = !stop;
if(!stop) play();
}
}
r=[1,2,0,0];
var rand = function(){
return r.pop();
}
function tdd(){
ok('Warming up',true,true);
ok('Boid x',b.x,0);
ok('Boid y',b.y,0);
ok('Nouveau boid',(new Boid).prototype,this.prototype);
b=new Boid(0,1);
ok('Un boid a un x',b.x,0);
b=new Boid(1,1);
ok('Un boid a un x a 1',b.x,1);
b=new Boid(1,1);
ok('Un boid a un y a 1',b.y,1);
var twoboids = createboids(2,rand);
ok('Function qui renvoie un tableau',twoboids.length,2);
ok('premier elem boids est un boid',twoboids[0].x,0);
ok('Deuxieme element de boids est un elemen avec2 ',twoboids[1].x,2);
ok('Le premier boid a une velocite',twoboids[1].vx,1);
c=new Boid(0,0);
c.step();
ok('Le premier boid bouge de 1 en x',c.x,1);
ok('Le premier boid bouge de 1 en y',c.y,1);
// ok('Deux boids trop proches',);
}
function ok(desc,a,b){
var tu = document.createElement('div');
if ( eval( a==b ) ) { tu.textContent='PASS '+desc; tu.style.color='green'; }
else { tu.textContent='FAIL '+desc+' '+a; tu.style.color='red'; }
$('test').appendChild(tu);
}
function $(id){return document.getElementById(id)}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment