Two level to my game

Hello everyone I urgently need your help I must make my game tomorrow and I just miss it to do 2 level to my game but I do not know how to do it. Thank you for your help and here is my code:

PImage img;//image boule
PImage ile;//fond
bird b = new bird();//nouvelle partie
pillar[] p = new pillar[3];//batons

int score=0;//score

int state = 0; 

void setup() {
  img = loadImage("boule.png");//boule 
  ile = loadImage("Fond.png");//fond
  size(1000, 800);//ecran
  for (int i = 0; i<3; i++) {
    p[i]=new pillar(i);//batons
  }
}

void draw() {
  background(ile);//ile/ fond



  if (state==0) {

    // intro 

    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(48);
    } else {
      fill(0);
    }
    rect(340, 200, 220, 50);//encadrement
    rect(340, 300, 220, 50);//encadrement
    rect(340, 400, 220, 50);//encadrement

    textSize(35); //taille de l’écriture
    fill(#FFA500); //couleur Ă©criture

    fill(#FF0000); //couleur cadre
    rect(310, 50, 300, 100);//encadrement

    fill(255); //couleur

    text("Flappy Rabbit", 350, 115);//titre debut
    text("niveau1 ", 370, 240);//debut
    text("Règle ", 385, 340);//règle
    text("niveau2", 370, 440);
  } else if (state==1) {

    // GAME 

    background(ile);//ile/ fond
    rect(20, 20, 100, 50);//cadre score
    fill(230);//texte score
    text(score, 30, 58); //texte score
    b.move(); //mouvement de la balle

    b.drawBird(); //vision de balle

    b.drag(); //redessente

    b.checkCollisions(); //collision

    for (int i = 0; i<3; i++) {
      p[i].drawPillar();//batons
      p[i].checkPosition();//batons
    }

    fill(0);//couleur fond encadrement
    textSize(32); //taille de l’écriture
    //
  } else if (state==2) {
    //
    //si perdu
    // 
    textSize(32); //taille de l’écriture
    fill(#FF0000 );
    rect(340, 100, 220, 50);//encadrement
    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(48);
    } else {
      fill(0);
    }
    rect(340, 200, 220, 50);//encadrement
    fill(255); //couleur ecriture

    text("Game Over", 350, 140);//fin
    text("Accueil", 390, 235);//score
  } else if (state==3) {// else if
    background(#1601FD);// fond
    textSize(32); //taille de l’écriture

    fill(#21F904); //couleur texte
    text("REGLE DU JEU", 350, 75); 
    fill(0); //couleur
    text("Le but du jeu est de:", 15, 150); 
    fill(#FF0000); //couleur
    text("-passer entre les poteaux sans les toucher ", 15, 225); 
    text("-marquer le plus de points", 15, 300); 
    fill(0); //couleur
    text("Comment jouer?", 15, 375);
    fill(#FF0000); //couleur
    text("-appuyer sur la barre espace pour sauter ", 15, 450); 
    text("-appuyer sur toutes les touches", 15, 525);
    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(48);
    } else {
      fill(0);
    }
    rect(400, 600, 220, 50);//encadrement
    fill(255); //couleur
    text("RETOUR", 440, 640);
  } else if (state==4) {
  
  
  
  }//func
}
void reset() { //recomencer
  score=0;
  b.yPos=400;
  b.ySpeed=0; 
  for (int i = 0; i<3; i++) {
    p[i].xPos+=550;
    p[i].cashed = false;
  }
}

void mousePressed() {
  //sauter avec la souris

  switch(state) {

  case 0:

    float x1=340; //emplacement cadre  //clique to play
    float y1=200;
    float w1=220;
    float h1=50;

    if (mouseX > x1 &&  //selection cadre
      mouseX < x1 + w1 && 
      mouseY > y1 &&
      mouseY < y1 + h1) { 


      state=1;
      reset();
    } //recomence
  }
  //switch
  switch(state) {

  case 0:
    float x2=340; //emplacement cadre //menu
    float y2=300;
    float w2=220;
    float h2=50;

    if (mouseX > x2 &&  //selection cadre
      mouseX < x2 + w2 && 
      mouseY > y2 &&
      mouseY < y2 + h2) { 


      state=3;
      reset();
    }
  } 
  switch(state) {
  case 3:
    float x3=400; //emplacement cadre //retour
    float y3=600;
    float w3=220;
    float h3=50;

    if (mouseX > x3 &&  //selection cadre
      mouseX < x3 + w3 && 
      mouseY > y3 &&
      mouseY < y3 + h3) { 


      state=0;
      reset();
    }
  }
  switch(state) {

  case 2:
    float x4=340; //emplacement cadre // accueil
    float y4=200;
    float w4=220;
    float h4=50;

    if (mouseX > x4 &&  //selection cadre
      mouseX < x4 + w4 && 
      mouseY > y4 &&
      mouseY < y4 + h4) { 


      state=0;
      reset();
    }
  }
  switch(state) {

  case 0:
    float x5=340; //emplacement cadre // accueil
    float y5=200;
    float w5=220;
    float h5=50;

    if (mouseX > x5 &&  //selection cadre
      mouseX < x5 + w5 && 
      mouseY > y5 &&
      mouseY < y5 + h5) { 


      state=4;
      reset();
    }
  }
}

void keyPressed() {
  //sauter avec la barre d’espace
  switch(state) {
  case 0: 
    state=1; 
    reset();
    break; 

  case 1: 
    b.jump();
    break; 

  case 2: 
    reset();
    state=0; 
    break; 
    //
  case 3:
    state=3;
    reset();
    break;
    //switch

  case 4:
    state=4;
    reset();
    break;
  }
}
// ============================================================================================
// from now on only classes 
class bird {

  float xPos, yPos, ySpeed; //position de la balle
  bird() {
    xPos = 250;//position de la balle au début
    yPos = 400;//position de la balle au début
  }
  void drawBird() { //balle
    stroke(#FFA500);
    noFill();
    strokeWeight(5);
    if (yPos<10) 
      yPos=10; 
    image(img, b.xPos, b.yPos);  // balle
  }
  void jump() { //monter
    ySpeed=-10;
  }
  void drag() { //avancer
    ySpeed+=0.4;
  }
  void move() { //bouger
    yPos+=ySpeed;
    for (int i = 0; i<3; i++) {
      p[i].xPos-=3;
    }
  }
  void checkCollisions() { //collision avec poteau
    if (yPos>800) {
      state=2;
    }
    for (int i = 0; i<3; i++) {
      if ((xPos<p[i].xPos+10&&xPos>p[i].xPos-10)&&(yPos<p[i].opening-100||yPos>p[i].opening+100)) { //position des poteau
        state=2;
      }
    }
  }
}//class

class pillar { //poteau
  float xPos, opening;
  boolean cashed = false;

  pillar(int i) {
    xPos = 100+(i*200);
    opening = random(600)+100;
  }

  void drawPillar() { //poteau
    line(xPos, 0, xPos, opening-100);
    line(xPos, opening+100, xPos, 800);
  }

  void checkPosition() { //position
    if (xPos<0) {
      xPos+=(200*3);
      opening = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false) {
      cashed=true;
      score++;
    }
  }
}//class
//

Whoa! that a lot of code that isn’t formatted. You should try and format it first so it can be read easily and efficiently

I am not sure exactly what you want from your code but I think this template could help you out just fill in each class add the objects you require anything you don’t understand let me know !

GUI button1;
GUI button2;
Levels l1;
void setup(){
  size( 600,600);
  button1 = new GUI( width/2 - 100 , height/2 , 150 , 30);
  button2 = new GUI( width/2 - 100 , height/4 , 150 , 30);
}
void draw(){
  background(255);
  button1.show(255 , 0 , 0 ,10,"Level2");
  button2.show(0 , 255 , 0 ,10,"Level1");
  
}
void mouseClicked(){
 if( button1.onTheButton() ) background(0,255,0);
 if( button2.onTheButton() ) background(0,0,255); 
}
public class GUI{
  
  PVector pos;
  int h , w;
  GUI( float x , float y , int w , int h){
     pos = new PVector(x,y); 
     this.w = w;
     this.h = h;
  }
  void show(int r , int g , int b , int size , String buttonDes){
      textSize( size );
      fill( 0 );
      text( buttonDes , pos.x , pos.y  );
      fill( r, g , b );
      rect(pos.x,pos.y , w , h);
  }
  boolean onTheButton(){
    if( mouseX > pos.x && mouseX< pos.x+w && mouseY > pos.y && mouseY < pos.y+h ) return true;
    return false;
  }
}
public class Levels{
   PVector pos;
   int w,h;
   Levels( float x , float y , int w , int h ){
     pos = new PVector( x , y );
     this.w = w;
     this.h = h;
   }
   void show( int r , int g , int b ){
     fill( r, g , b);
     rect( pos.x , pos.y , w , h );
   }
   boolean collision( float x , float y , int sizeW , int sizeH){
      if( x > pos.x && x + sizeW < pos.x + w && y > pos.y && y+sizeH < pos.y + h ) return true;
      return false;
   }
}
class Player{
   PVector pos;
   int size;
   Player( float x , float y , int size){
      pos = new PVector(x,y); 
      this.size = size;
   }
   void show(){
      fill(255,0,0);
      rect(pos.x,pos.y,size,size);
   }
}
1 Like

In fact I would like to know how to have 2 level this to say a level for example or the ball is not going fast (level1) and a level where the ball goes fast (level2) if you could send me the scrip would be perfect because after this message i could not be written before 2 am

both levels use the same state.

The difference is that the new variable level is 1 or 2.

This gives another speed for the walls in the bird class (3 or 6)

  void move() { //bouger
    yPos+=ySpeed;
    for (int i = 0; i<3; i++) {

      if (level==1) 
        p[i].xPos-=3; //!!!!!!!!!!!!!!!!!!!!!
      else if (level==2)
        p[i].xPos-=6; //!!!!!!!!!!!!!!!!!!

    }
  }

Chrisir

Entire Sketch

PImage img;//image boule
PImage ile;//fond

bird b = new bird();//nouvelle partie

pillar[] p = new pillar[3];//batons

int score=0;//score

int state = 0;

int level=1; 

void setup() {
  //
  img = loadImage("boule.png");//boule
  ile = loadImage("Fond.png");//fond
  size(1000, 800);//ecran
  for (int i = 0; i<3; i++) {
    p[i]=new pillar(i);//batons
  }
}

void draw() {
  background(ile); // fond

  if (state==0) {

    // intro 

    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(48);
    } else {
      fill(33);
    }
    stroke(#FFA500);
    strokeWeight(5);
    rect(340, 200, 220, 50);//encadrement
    rect(340, 300, 220, 50);//encadrement
    rect(340, 400, 220, 50);//encadrement

    textSize(35); //taille de l’écriture
    fill(#FFA500); //couleur Ă©criture

    fill(#FF0000); //couleur cadre
    rect(310, 50, 300, 100);//encadrement

    fill(255); //couleur

    text("Flappy Rabbit", 350, 115);//titre debut
    text("niveau1 ", 370, 240);//debut
    text("niveau2", 370, 340);//règle
    text("Règle ", 370, 440);
  } 
  //------
  else if (state==1) {

    // GAME 

    background(ile); // fond
    rect(20, 20, 100, 50);//cadre score
    fill(230);//texte score and level 
    text(score, 30, 58); //texte score
    text("Level "+level, width-130, 58); //texte level

    b.move(); //mouvement de la balle

    b.drawBird(); //vision de balle

    b.drag(); //redessente

    b.checkCollisions(); //collision

    for (int i = 0; i<3; i++) {
      p[i].drawPillar();//batons
      p[i].checkPosition();//batons
    }

    fill(0);//couleur fond encadrement
    textSize(32); //taille de l’écriture
    //
  } 
  //---------------
  else if (state==2) {
    //
    //si perdu
    //
    textSize(32); //taille de l’écriture
    fill(#FF0000 );
    rect(340, 100, 220, 50);//encadrement
    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(48);
    } else {
      fill(0);
    }
    rect(340, 200, 220, 50);//encadrement
    fill(255); //couleur ecriture

    text("Game Over", 350, 140);//fin
    text("Accueil", 390, 235);//score
  } 

  // ------------------------

  else if (state==3) {

    // RULES 

    background(#1601FD);// fond
    textSize(32); //taille de l’écriture

    fill(#21F904); //couleur texte
    text("REGLE DU JEU", 350, 75); 
    fill(0); //couleur
    text("Le but du jeu est de:", 15, 150); 
    fill(#FF0000); //couleur
    text("-passer entre les poteaux sans les toucher ", 15, 225); 
    text("-marquer le plus de points", 15, 300); 
    fill(0); //couleur
    text("Comment jouer?", 15, 375);
    fill(#FF0000); //couleur
    text("-appuyer sur la barre espace pour sauter ", 15, 450); 
    text("-appuyer sur toutes les touches", 15, 525);
    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(48);
    } else {
      fill(0);
    }
    rect(400, 600, 220, 50);//encadrement
    fill(255); //couleur
    text("RETOUR", 440, 640);
  } 
  // ------------------------
}

void reset() { //recomencer
  score=0;
  b.yPos=400;
  b.ySpeed=0;
  for (int i = 0; i<3; i++) {
    p[i].xPos+=550;
    p[i].cashed = false;
  }
}

void mousePressed() {
  //sauter avec la souris

  switch(state) {

  case 0:

    float x1=340; //emplacement cadre  //clique to play / Level 1
    float y1=200;
    float w1=220;
    float h1=50;

    if (mouseX > x1 &&  //selection cadre
      mouseX < x1 + w1 && 
      mouseY > y1 &&
      mouseY < y1 + h1) { 
      level=1; 
      state=1;
      reset();
    } //recomence

    float x2=340; //emplacement cadre  / Level 2
    float y2=300;
    float w2=220;
    float h2=50;

    if (mouseX > x2 &&  //selection cadre
      mouseX < x2 + w2 && 
      mouseY > y2 &&
      mouseY < y2 + h2) { 
      println("3");
      level=2; 
      state=1;
      reset();
    }    

    float x5=340; //emplacement cadre / Rules 
    float y5=400;
    float w5=220;
    float h5=50;

    if (mouseX > x5 &&  //selection cadre
      mouseX < x5 + w5 && 
      mouseY > y5 &&
      mouseY < y5 + h5) {        
      state=3;
      reset();
    }
    break; 

  case 3:
    // Rules 
    float x3=400; //emplacement cadre //retour
    float y3=600;
    float w3=220;
    float h3=50;

    if (mouseX > x3 &&  //selection cadre
      mouseX < x3 + w3 && 
      mouseY > y3 &&
      mouseY < y3 + h3) { 
      state=0;
      reset();
    }
    break; 

  case 2:
    float x4=340; //emplacement cadre // accueil
    float y4=200;
    float w4=220;
    float h4=50;

    if (mouseX > x4 &&  //selection cadre
      mouseX < x4 + w4 && 
      mouseY > y4 &&
      mouseY < y4 + h4) {
      state=0;
      reset();
    }
    break; 
    //
  } // switch
}//func 

void keyPressed() {
  //sauter avec la barre d’espace
  switch(state) {
  case 0:
    //ignore
    break;

  case 1:
    b.jump();
    break;

  case 2:
    reset();
    state=0;
    break;
    //
  case 3:
    state=0;
    reset();
    break;
  } //switch
}

// ============================================================================================
// from now on only classes

class bird {

  float xPos, yPos, ySpeed; //position de la balle

  bird() {
    xPos = 250;//position de la balle au début
    yPos = 400;//position de la balle au début
  }

  void drawBird() { //balle
    stroke(#FFA500);
    noFill();
    strokeWeight(5);
    if (yPos<10)
      yPos=10;
    image(img, b.xPos, b.yPos); // balle
    // ellipse (b.xPos, b.yPos, 5, 5); // balle
  }

  void jump() { //monter
    ySpeed=-10;
  }

  void drag() { //avancer
    ySpeed+=0.4;
  }

  void move() { //bouger
    yPos+=ySpeed;
    for (int i = 0; i<3; i++) {
      if (level==1) 
        p[i].xPos-=3;
      else if (level==2)
        p[i].xPos-=6;
    }
  }

  void checkCollisions() { //collision avec poteau
    if (yPos>800) {
      state=2;
    }
    for (int i = 0; i<3; i++) {
      if ((xPos<p[i].xPos+10&&
        xPos>p[i].xPos-10)&&
        (yPos<p[i].opening-100||yPos>p[i].opening+100)) { //position des poteau
        state=2;
      }
    }
  }
}//class

class pillar {
  //poteau
  float xPos, opening;
  boolean cashed = false;

  pillar(int i) {
    xPos = 100+(i*200);
    opening = random(600)+100;
  }

  void drawPillar() { //poteau
    line(xPos, 0, xPos, opening-100);
    line(xPos, opening+100, xPos, 800);
  }

  void checkPosition() { //position
    if (xPos<0) {
      xPos+=(200*3);
      opening = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false) {
      cashed=true;
      score++;
    }
  }
}//class
//
1 Like

Thank you very much and if I wanted to add a timer so that for example if I play for 30 seconds it shows win and if not lost

Yes, that’s possible.

Just search timer here in the forum

I did not find a timer I found that clock

Search

https://discourse.processing.org/search?q=timer

Why didn’t you find those?

Here is a timer



int timer; 
int i;

void setup() {
  size(555, 555);
  background(0);
  timer = millis();
}

void draw() {
  background(0);

  if (millis()-timer > 1000) {
    timer = millis();
    i++;
  }
  text(i, 33, 33);
}//
//

Timer in your sketch

When you enter the state for Game say before this: timer = millis();

During the game say

  if (millis()-timer > 1000) {
    state = 0; // or whatever 
  }

Chrisir

1 Like

if (millis()-timer > 1000) {
state = 0; // or whatever
}

ou je dois mettre ça?
and why the timer starts before I start the game

Where? English please.

In draw(), in the section where the state is for the game

For 30 seconds

> 30000

and why he does not show up during the game (the timer)

It depends on what you mean by “show”.

To show the value say

text(millis()-timer, 33,33);

When you want me to find an error in your code, post the code. Press ctrl-t prior to posting and format in correctly.

PImage img;//image boule
PImage ile;//fond

bird b = new bird();//nouvelle partie

pillar[] p = new pillar[3];//batons

int score=0;//score
int timer; //temps
int i; //temps
int state = 0;//inserer des switch pour inserer des "case"
//pour simplifier if et else lorsqu'il y a plusieur alternative

int level=1;  //nombre decimaux

void setup() {
  //
  timer = millis(); //temps /milieme de seconde
  img = loadImage("boule.png");//boule
  ile = loadImage("Fond.png");//fond
  size(1000, 800);//ecran
  for (int i = 0; i<3; i++) {
    p[i]=new pillar(i);//batons
  }
}

void draw() {
  background(ile); // fond

  if (millis()-timer > 1000) { //vitesse des seconde
    timer = millis();
    i++; //1+1
  }
  text(i, 33, 33);

  if (state==0) {

    // intro 

    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(200);
    } else {
      fill(0);
    }
    stroke(#FFA500);
    strokeWeight(5);
    rect(340, 200, 220, 50);//encadrement
    rect(340, 300, 220, 50);//encadrement
    rect(340, 400, 220, 50);//encadrement

    textSize(35); //taille de l’écriture
    fill(#FFA500); //couleur Ă©criture

    fill(#FF0000); //couleur cadre
    rect(310, 50, 300, 100);//encadrement

    fill(255); //couleur

    text("Flappy Rabbit", 350, 115);//titre debut
    text("niveau1 ", 370, 240);//debut
    text("niveau2", 370, 340);//règle
    text("Règle ", 370, 440);
  } 



  //------

  else if (state==1) {

    // GAME 


    background(ile); // fond
    rect(700, 20, 100, 50);//cadre score
    fill(230);//texte score and level 
    text(score, 710, 58); //texte score
    text("Level "+level, width-130, 58); //texte level

    b.move(); //mouvement de la balle

    b.drawBird(); //vision de balle

    b.drag(); //redessente

    b.checkCollisions(); //collision

    for (int i = 0; i<3; i++) {
      p[i].drawPillar();//batons
      p[i].checkPosition();//batons
    }

    fill(0);//couleur fond encadrement
    textSize(32); //taille de l’écriture
    //
    if (millis()-timer > 30000) {
      state = 0; // or whatever
    }
  } 
  //---------------
  else if (state==2) {
    //
    //si perdu
    //
    textSize(32); //taille de l’écriture
    fill(#FF0000 );
    rect(340, 100, 220, 50);//encadrement
    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(200);
    } else {
      fill(0);
    }
    rect(340, 200, 220, 50);//encadrement
    fill(255); //couleur ecriture

    text("Game Over", 350, 140);//fin
    text("Accueil", 390, 235);//score
  } 

  // ------------------------

  else if (state==3) {

    // RULES 

    background(#1601FD);// fond
    textSize(32); //taille de l’écriture

    fill(#21F904); //couleur texte
    text("REGLE DU JEU", 350, 75); 
    fill(0); //couleur
    text("Le but du jeu est de:", 15, 150); 
    fill(#FF0000); //couleur
    text("-passer entre les poteaux sans les toucher ", 15, 225); 
    text("-marquer le plus de points", 15, 300); 
    fill(0); //couleur
    text("Comment jouer?", 15, 375);
    fill(#FF0000); //couleur
    text("-appuyer sur la barre espace pour sauter ", 15, 450); 
    text("-choisir niveau 1 ou 2", 15, 525);
    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(0);
    } else {
      fill(0);
    }
    rect(400, 600, 220, 50);//encadrement
    fill(255); //couleur
    text("RETOUR", 440, 640);
  } 
  // ------------------------
}

void reset() { //recomencer
  score=0;
  b.yPos=400;
  b.ySpeed=0;
  for (int i = 0; i<3; i++) {
    p[i].xPos+=550;
    p[i].cashed = false;
  }
}

void mousePressed() {
  //sauter avec la souris

  switch(state) {

  case 0:

    float x1=340; //emplacement cadre  //clique to play / Level 1
    float y1=200;
    float w1=220;
    float h1=50;

    if (mouseX > x1 &&  //selection cadre
      mouseX < x1 + w1 && 
      mouseY > y1 &&
      mouseY < y1 + h1) { 
      level=1; 
      state=1;
      reset();
    } //recommence

    float x2=340; //emplacement cadre  / Level 2
    float y2=300;
    float w2=220;
    float h2=50;

    if (mouseX > x2 &&  //selection cadre
      mouseX < x2 + w2 && 
      mouseY > y2 &&
      mouseY < y2 + h2) { 
      println("3"); //examiner les données produites par un programme
      level=2; 
      state=1;
      reset();
    }    

    float x5=340; //emplacement cadre / Rules 
    float y5=400;
    float w5=220;
    float h5=50;

    if (mouseX > x5 &&  //selection cadre
      mouseX < x5 + w5 && 
      mouseY > y5 &&
      mouseY < y5 + h5) {        
      state=3;
      reset();
    }
    break; 

  case 3:
    // Rules 
    float x3=400; //emplacement cadre //retour
    float y3=600;
    float w3=220;
    float h3=50;

    if (mouseX > x3 &&  //selection cadre
      mouseX < x3 + w3 && 
      mouseY > y3 &&
      mouseY < y3 + h3) { 
      state=0;
      reset();
    }
    break; 

  case 2:
    float x4=340; //emplacement cadre // accueil
    float y4=200;
    float w4=220;
    float h4=50;

    if (mouseX > x4 &&  //selection cadre
      mouseX < x4 + w4 && 
      mouseY > y4 &&
      mouseY < y4 + h4) {
      state=0;
      reset();
    }
    break; 
    //
  } // switch
}//func 

void keyPressed() {
  //sauter avec la barre d’espace
  switch(state) {
  case 0:
    //ignore
    break;

  case 1:
    b.jump();
    break;

  case 2:
    reset();
    state=0;
    break;
    //
  case 3:
    state=0;
    reset();
    break;
  } //switch
}

// ============================================================================================
// from now on only classes

class bird {

  float xPos, yPos, ySpeed; //position de la balle

  bird() {
    xPos = 250;//position de la balle au début
    yPos = 400;//position de la balle au début
  }

  void drawBird() { //balle
    stroke(#FFA500);
    noFill();
    strokeWeight(5);
    if (yPos<10)
      yPos=10;
    image(img, b.xPos, b.yPos); // balle
    // ellipse (b.xPos, b.yPos, 5, 5); // balle
  }

  void jump() { //monter
    ySpeed=-10;
  }

  void drag() { //avancer
    ySpeed+=0.4;
  }

  void move() { //bouger
    yPos+=ySpeed;
    for (int i = 0; i<3; i++) {
      if (level==1) 
        p[i].xPos-=3;
      else if (level==2)
        p[i].xPos-=6;
    }
  }

  void checkCollisions() { //collision avec poteau
    if (yPos>800) {
      state=2;
    }
    for (int i = 0; i<3; i++) {
      if ((xPos<p[i].xPos+10&&
        xPos>p[i].xPos-10)&&
        (yPos<p[i].opening-100||yPos>p[i].opening+100)) { //position des poteau
        state=2;
      }
    }
  }
}//class

class pillar {
  //poteau
  float xPos, opening;
  boolean cashed = false;

  pillar(int i) {
    xPos = 100+(i*200);
    opening = random(600)+100;
  }

  void drawPillar() { //poteau
    line(xPos, 0, xPos, opening-100);
    line(xPos, opening+100, xPos, 800);
  }

  void checkPosition() { //position
    if (xPos<0) {
      xPos+=(200*3);
      opening = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false) {
      cashed=true;
      score++;
    }

> Blockquote

}
}//class
//

Please ask a question and do not post code without a comment.
What do you mean by “show”? You did not explain…

This is what you wrote about the timer:

if I play for 30 seconds it shows win and if not lost

This is not understandable.

it’s good I managed but when it shows win and I want to replay the time do not come back to 0

PImage img;//image boule
PImage ile;//fond

bird b = new bird();//nouvelle partie

pillar[] p = new pillar[3];//batons

int score=0;//score
int timer; //temps
int i; //temps
int state = 0;//inserer des switch pour inserer des "case"
//pour simplifier if et else lorsqu'il y a plusieur alternative

int level=1;  //nombre decimaux

void setup() {
  //
  timer = millis(); //temps /milieme de seconde
  img = loadImage("boule.png");//boule
  ile = loadImage("Fond.png");//fond
  size(1000, 800);//ecran
  for (int i = 0; i<3; i++) {
    p[i]=new pillar(i);//batons
  }
}

void draw() {
  background(ile); // fond

  if (millis()-timer > 30000) { //vitesse des seconde
    timer = millis();
    i++; //1+1
  }


  if (state==0) {

    // intro 

    if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
      fill(200);
    } else {
      fill(0);
    }
    stroke(#FFA500);
    strokeWeight(5);
    rect(340, 200, 220, 50);//encadrement
    rect(340, 300, 220, 50);//encadrement
    rect(340, 400, 220, 50);//encadrement

    textSize(35); //taille de l’écriture
    fill(#FFA500); //couleur Ă©criture

    fill(#FF0000); //couleur cadre
    rect(310, 50, 300, 100);//encadrement

    fill(255); //couleur

    text("Flappy Rabbit", 350, 115);//titre debut
    text("niveau1 ", 370, 240);//debut
    text("niveau2", 370, 340);//règle
    text("Règle ", 370, 440);
  } 



  //------

  else if (state==1) {

    // GAME 


    background(ile); // fond
    rect(700, 20, 100, 50);//cadre score
    fill(230);//texte score and level 
    text(score, 710, 58); //texte score
    text("Level "+level, width-130, 58); //texte level

    b.move(); //mouvement de la balle

    b.drawBird(); //vision de balle

    b.drag(); //redessente

    b.checkCollisions(); //collision

    for (int i = 0; i<3; i++) {
      p[i].drawPillar();//batons
      p[i].checkPosition();//batons
    }

    fill(0);//couleur fond encadrement
    textSize(32); //taille de l’écriture
    //
    if (millis()-timer > 5000) {
      state = 4; // si depasser 5000 alors aller page 4
      
  }
  
  text(millis()-timer, 33, 33);
} 
//---------------
else if (state==2) {
  //
  //si perdu
  //
  textSize(32); //taille de l’écriture
  fill(#FF0000 );
  rect(340, 100, 220, 50);//encadrement
  if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
    fill(200);
  } else {
    fill(0);
  }
  rect(340, 200, 220, 50);//encadrement
  fill(255); //couleur ecriture

  text("Game Over", 350, 140);//fin
  text("Accueil", 390, 235);//score
} 

// ------------------------

else if (state==3) {

  // RULES 

  background(#1601FD);// fond
  textSize(32); //taille de l’écriture

  fill(#21F904); //couleur texte
  text("REGLE DU JEU", 350, 75); 
  fill(0); //couleur
  text("Le but du jeu est de:", 15, 150); 
  fill(#FF0000); //couleur
  text("-passer entre les poteaux sans les toucher ", 15, 225); 
  text("-marquer le plus de points", 15, 300); 
  fill(0); //couleur
  text("Comment jouer?", 15, 375);
  fill(#FF0000); //couleur
  text("-appuyer sur la barre espace pour sauter ", 15, 450); 
  text("-choisir niveau 1 ou 2", 15, 525);
  if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
    fill(0);
  } else {
    fill(0);
  }
  rect(400, 600, 220, 50);//encadrement
  fill(255); //couleur
  text("RETOUR", 440, 640);
} 
// ------------------------
else if (state==4) {
  background(#1601FD);// fond
  textSize(32); //taille de l’écriture
  if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
    fill(0);
  } else {
    fill(0);
  }
  rect(400, 600, 220, 50);//encadrement
  fill(255); //couleur
  text("MENU", 440, 640);

  textSize(90); //taille de l’écriture
  if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
    fill(0);
  } else {
    fill(0);
  }

  fill(#FF0000); //couleur
  text("GAGNER", 300, 280);
}  

// ------------------------
}

void reset() { //recomencer
  score=0;
  b.yPos=400;
  b.ySpeed=0;
  for (int i = 0; i<3; i++) {
    p[i].xPos+=550;
    p[i].cashed = false;
  }
}

void mousePressed() {
  //sauter avec la souris

  switch(state) {

  case 0:

    float x1=340; //emplacement cadre  //clique to play / Level 1
    float y1=200;
    float w1=220;
    float h1=50;

    if (mouseX > x1 &&  //selection cadre
      mouseX < x1 + w1 && 
      mouseY > y1 &&
      mouseY < y1 + h1) { 
      level=1; 
      state=1;
      reset();
    } //recommence

    float x2=340; //emplacement cadre  / Level 2
    float y2=300;
    float w2=220;
    float h2=50;

    if (mouseX > x2 &&  //selection cadre
      mouseX < x2 + w2 && 
      mouseY > y2 &&
      mouseY < y2 + h2) { 
      println("3"); //examiner les données produites par un programme
      level=2; 
      state=1;
      reset();
    }    

    float x5=340; //emplacement cadre / Rules 
    float y5=400;
    float w5=220;
    float h5=50;

    if (mouseX > x5 &&  //selection cadre
      mouseX < x5 + w5 && 
      mouseY > y5 &&
      mouseY < y5 + h5) {        
      state=3;
      reset();
    }
    break; 

  case 3:
    // Rules 
    float x3=400; //emplacement cadre //retour
    float y3=600;
    float w3=220;
    float h3=50;

    if (mouseX > x3 &&  //selection cadre
      mouseX < x3 + w3 && 
      mouseY > y3 &&
      mouseY < y3 + h3) { 
      state=0;
      reset();
    }
    break; 

  case 2:
    float x4=340; //emplacement cadre // accueil
    float y4=200;
    float w4=220;
    float h4=50;

    if (mouseX > x4 &&  //selection cadre
      mouseX < x4 + w4 && 
      mouseY > y4 &&
      mouseY < y4 + h4) {
      state=0;
      reset();
    }
    break; 

  case 4:
    float x6=400; //emplacement cadre // accueil
    float y6=600;
    float w6=220;
    float h6=50;

    if (mouseX > x6 &&  //selection cadre //menu (gagner)
      mouseX < x6 + w6 && 
      mouseY > y6 &&
      mouseY < y6 + h6) {
      state=0;
      reset();
    }
    break;

    //
  } // switch
}//func 

void keyPressed() {
  //sauter avec la barre d’espace
  switch(state) {
  case 0:
    //ignore
    break;

  case 1:
    b.jump();
    break;

  case 2:
    reset();
    state=0;
    break;
    //
  case 3:
    state=0;
    reset();
    break;
    //switch

  case 4:
    state=4;
    reset();
    break;
  }
}

// ============================================================================================
// from now on only classes

class bird {

  float xPos, yPos, ySpeed; //position de la balle

  bird() {
    xPos = 250;//position de la balle au début
    yPos = 400;//position de la balle au début
  }

  void drawBird() { //balle
    stroke(#FFA500);
    noFill();
    strokeWeight(5);
    if (yPos<10)
      yPos=10;
    image(img, b.xPos, b.yPos); // balle
    // ellipse (b.xPos, b.yPos, 5, 5); // balle
  }

  void jump() { //monter
    ySpeed=-10;
  }

  void drag() { //avancer
    ySpeed+=0.4;
  }

  void move() { //bouger
    yPos+=ySpeed;
    for (int i = 0; i<3; i++) {
      if (level==1) 
        p[i].xPos-=3;
      else if (level==2)
        p[i].xPos-=6;
    }
  }

  void checkCollisions() { //collision avec poteau
    if (yPos>800) {
      state=2;
    }
    for (int i = 0; i<3; i++) {
      if ((xPos<p[i].xPos+10&&
        xPos>p[i].xPos-10)&&
        (yPos<p[i].opening-100||yPos>p[i].opening+100)) { //position des poteau
        state=2;
      }
    }
  }
}//class

class pillar {
  //poteau
  float xPos, opening;
  boolean cashed = false;

  pillar(int i) {
    xPos = 100+(i*200);
    opening = random(600)+100;
  }

  void drawPillar() { //poteau
    line(xPos, 0, xPos, opening-100);
    line(xPos, opening+100, xPos, 800);
  }

  void checkPosition() { //position
    if (xPos<0) {
      xPos+=(200*3);
      opening = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false) {
      cashed=true;
      score++;
    }
  }
}//class
//

Repeat this

timer = millis();

at the end of setup and when starting and restarting the game.

These lines at the beginning of draw() are nonsense:

  if (millis()-timer > 30000) { //vitesse des seconde
    timer = millis();
    i++; //1+1
  }

They presumably belong in the if (state==1) -section? As I said nothing is allowed outside the long if...else if....-section in draw().

I have a question if I want to do that when my mouse is above a frame then it changes color

here is an example from the other thread (sketch not by me)

As you can see, there is a button class. It makes the handling of the buttons easier.

In the class you can see the function over that checks if the mouse is over a button (without clicking).

Depending on that the color is set

Chrisir


Button button1, button2, button3, button4;

int x1 = 185;
int x2 = 495;
int x3 = 805;
int x4 = 15;

int y = 500;
int y4 = 20;

boolean flag1=false;

void setup() {
  size(1250, 800);
  background(#D5ECF9);

  button1 = new Button(x1, y, "a", "1");
  button2 = new Button(x2, y, "b", "2"); 
  button3 = new Button(x3, y, "c", "3");
  button4 = new Button(x4, y4, "=", "4");
}

void draw () {
  background(#D5ECF9);

  button1.display();
  button2.display();
  button3.display();
  button4.display();

  fill(0);
  textAlign(LEFT);
  textSize(40);
  text("TEST...", 60, 390);

  if (flag1) 
    text("Yes", 360, 239);
} 

// -------------------------------------------------------------------------------------------

void mousePressed() {
  if (button1.over()) 
    flag1=true; 
  else flag1=false;
}

// ===========================================================================================

class Button {
  float x, y; // position
  float w = 215, h = 110;  // size 

  String name;
  String caption;

  boolean hover = false;
  boolean enabled = true;
  boolean clicked = false;

  color baseColor;
  color hoverColor;

  //constr 
  Button(float x, float y, 
    String caption, 
    String name) {
    this.x = x; // position 
    this.y = y;
    this.caption = caption; // text 
    this.name = name; // navnet pĂĄ knappen, navnet pĂĄ det mode knappen skifter til

    this.baseColor = color(255); // knappens normalfarve // white 
    this.hoverColor = color(204); // nĂĄr mouse er over
  }//constr 

  void display() {
    // draw rect 
    strokeWeight(3);
    stroke(255, 0, 0); // RED 
    if (over())  // choose color depending on mouse over button 
      fill(hoverColor);
    else fill(baseColor);
    rect(x, y, w, h);

    // draw text 
    strokeWeight(9);
    fill(0);    
    textSize(50);
    textAlign(CENTER, CENTER);
    text(caption, x, y, w, h);
  }// method

  boolean over() {
    // mouse over button?  
    boolean result = false; 
    if (mouseX > x && 
      mouseX < x + w && 
      mouseY > y &&
      mouseY < y+h) 
      result=true; // is over the rect of the button 
    else
      result=false; // is not over 

    // give the result value to the calling function 
    return result;
  }// method
  //
}//class
//
1 Like