}

}

function keyPressed() {

if (key == ' ') {

bird.up();

//console.log("SPACE");

}

}


tab3


function DNA(genes) {

// Recieves genes and create a dna object

if (genes) {

this.genes = genes;

}

// If no genes just create random dna

else {

this.genes = [];

for (var i = 0; i < lifespan; i++) {

// Gives random vectors

this.genes[i] = p5.Vector.random2D();

// Sets maximum force of vector to be applied to a rocket

this.genes[i].setMag(maxforce);

}

}

// Performs a crossover with another member of the species

this.crossover = function(partner) {

var newgenes = [];

// Picks random midpoint

var mid = floor(random(this.genes.length));

for (var i = 0; i < this.genes.length; i++) {

// If i is greater than mid the new gene should come from this partner

if (i > mid) {

newgenes[i] = this.genes[i];

}

// If i < mid new gene should come from other partners gene's

else {

newgenes[i] = partner.genes[i];

}

}

// Gives DNA object an array

return new DNA(newgenes);

}

// Adds random mutation to the genes to add variance.

this.mutation = function() {

for (var i = 0; i < this.genes.length; i++) {

// if random number less than 0.01, new gene is then random vector

if (random(1) < 0.01) {

this.genes[i] = p5.Vector.random2D();

this.genes[i].setMag(maxforce);

}

}

}

}


tab4


function Bird() {

this.y = height/2;

this.x = 64;

this.gravity = 0.7;

this.lift = -12;

this.velocity = 0;

this.show = function() {

fill(255);

ellipse(this.x, this.y, 32, 32);

}

this.up = function() {

this.velocity += this.lift;

}

this.update = function() {

this.velocity += this.gravity;

// this.velocity *= 0.9;

this.y += this.velocity;

if (this.y > height) {

this.y = height;

this.velocity = 0;

}

if (this.y < 0) {

this.y = 0;

this.velocity = 0;

}

}

}

JavaScript CodingWhere stories live. Discover now