On utilisera la barre d’espacement pour générer une nouvelle structure, la souris sur l’axe horizontal pour taire tourner de dessin, et toutes les autres touches pour faire apparaitre un "sol".
Le code
Nous utilisons randomSeed() pour afficher constamment la même structure générée au hasard.
// cabanes à la Franck
int nb_minimum=6; // nombre d'element minimum
int nb_maximum=20; // nombre maximal d'element
long lerandom;
boolean sol=true;
void setup() {
size(1200, 800, P3D);
smooth();
newseed();
}
void draw() {
background(255);
stroke(1);
fill(255);
directionalLight(210, 210, 210, -1, 1, 0);
ambientLight(120, 120, 120);
randomSeed(lerandom);
translate(width/2, 0, -250);
rotateY(map(mouseX, 0, width, 0, TWO_PI));
int nbc=int(random(nb_minimum, nb_maximum)); // nombre d'elements
for (int i=0;i<nbc;i++) {
pushMatrix();
float largeur, hauteur;
float type=random(10);
// pilier ou plateau ?
if (type>5) {
// plateau
largeur=random(250, 400);
hauteur=random(2, 10);
translate(random(-300, 300), height-random(50, 500), random(-30, 30));
}
else {
// pilier
largeur=random(10, 30);
hauteur=random(200, 400);
translate(random(-300, 300), height-hauteur, random(-30, 30));
}
box(largeur, hauteur, largeur);
popMatrix();
}
// sol
if(sol==true){
fill(200);
noStroke();
translate(0, height, 0);
box(width*2, 2, width*2);
}
}
void keyPressed() {
if(key==' '){
newseed();
} else {
if(sol==true){
sol=false;
} else {
sol=true;
}
}
}
void newseed() {
lerandom=int(random(1000));
}