/*###########################################
CLASSE QUI CONSTRUIT LES AUTOMOBILES
#############################################*/
class Automobile {
//attribut
float x;
float y;
float largeur = 27;
float hauteur = 47;
//constructeur
Automobile(float x, float y){
this.x = x;
this.y = y;
//this.vitesse = vitesse;
}
void draw() {
fill(255);
rect(x,y,largeur,hauteur);
fill(0);
rect( x+largeur/2-14 , y+4 , 28 , 10 );
rect( x+largeur/2-14 , y+32 , 28 , 10 );
rect( x+largeur/2-13 , y+15 , 2 , 7 );
rect( x+largeur/2-13 , y+24 , 2 , 7 );
rect( x+largeur/2+11 , y+15 , 2 , 7 );
rect( x+largeur/2+11 , y+24 , 2 , 7 );
fill(255);
ellipse( x+13, y+47, 28, 6 );
ellipse( x+13, y, 28, 6 );
//x = constrain(x +90, 100, 350);
}
}
/*###########################################
Auteur: sébastien Belhumeur leith
TP3 - Création d'une simulation virtuelle
#############################################*/
int nombre_auto =4 ; //int pour determiner le nombre d'automobile
int nombre_grenouille = 1; //int pour determiner le nombre de grenouille
float vitesse; //float qui determine la vitesse des automobiles
float vitesseGrenouille; //float qui determine la vitesse des grenouille
PImage b; // images de fond
float x; // axe des x des autos
float y; // axe des y des autos
float xG; // axe des x des grenouilles
float yG; // axe des y des grenouille
ArrayList automobiles; //arraylist automobiles
ArrayList grenouilles; //arraylist grenouilles
void setup() {
frameRate( 60 );
size(400,400);
smooth();
noStroke();
// initialisation des variables
b = loadImage("route.jpg");
automobiles = new ArrayList();
grenouilles = new ArrayList();
for (int b = 0; b < nombre_auto; b++)
{
vitesse = random(1,2) ;
x =random(width);
y =random(height);
Automobile automobile = new Automobile(x, y);
automobiles.add(automobile);
}
for (int b = 0; b < nombre_grenouille; b++)
{
vitesseGrenouille = random(1,2);
xG =random(width);
yG =random(height);;
Grenouille grenouille = new Grenouille(xG, yG);
grenouilles.add(grenouille);
}
}
void draw()
{
//background(220);
image(b, 0, 0);
// Dessine et bouge les grenouilles selon le nombre de grenouille demandé,
for (int h=0; h < grenouilles.size() ; h++ ) {
Grenouille grenouille = (Grenouille) grenouilles.get(h);
grenouille.draw();
grenouille.xG += random(1.30);
// si la grenouille sort de la scène elle la remet au début dans un axe des y différents
if ( grenouille.xG > width )
{
grenouille.xG = -50;
grenouille.yG = random(height);
}
}
// Dessine et bouge les automobiles selon le nombre d'automobile demandé,
for (int i=0; i < automobiles.size() ; i++ ) {
Automobile automobile = (Automobile) automobiles.get(i);
automobile.draw();
automobile.y += vitesse;
//automobile.x = constrain(x +90, 100, 350);
// si l'automobile sort de la scène elle la remet au début dans un axe des x différents
if ( automobile.y > height )
{
automobile.y = -50;
automobile.x = random(width);
}
}
// detecte si il y a une colision entre les objets
for ( int q =0 ; q < automobiles.size() ; q++ ) {
Automobile automobile = (Automobile) automobiles.get(q);
for ( int g =0 ; g < grenouilles.size() ; g++ ) {
Grenouille grenouille = (Grenouille) grenouilles.get(g);
float distance = dist(automobile.x,automobile.y,grenouille.xG,grenouille.yG);
//enleve la grenouille si il y collision
if ( distance < 20) {
grenouilles.remove(g);
}
//rajoute une grenouille lorsqu'il n'y en a plus
if(grenouilles.size() <= 0) {
grenouilles.add(grenouille);
grenouille.xG= -50;
}
}
}
}
/*###########################################
CLASSE QUI CONSTRUIT LES GRENOUILLES
#############################################*/
class Grenouille {
//attributs
float xG;
float yG;
float largeurG = 20;
float hauteurG = 20;
//Constructeur
Grenouille(float xG, float yG){
this.xG = xG;
this.yG = yG;
//this.vitesse = vitesse;
}
void draw() {
fill(28,100,60);
ellipse( xG-7, yG+7, 8, 8 );
ellipse( xG-7, yG-7, 8, 8 );
fill(28,200,22);
ellipse( xG, yG, 20, 20 );
ellipse( xG, yG+11, 8, 8 );
ellipse( xG, yG-11, 8, 8 );
//ellipse( xG-13, yG+9, 6, 6 );
//ellipse( xG-13, yG-9, 6, 6 );
fill(255);
ellipse( xG+8, yG+6, 5, 8 );
ellipse( xG+8, yG-6, 5, 8 );
fill(0);
ellipse( xG+8, yG+5, 3, 3 );
ellipse( xG+8, yG-5, 3, 3 );
//x = constrain(x +90, 100, 350);
}
}
Simulation du jeu Frogger
Lorsque qu'une grenouille frappe un automobile, elle meurt!