Processing, button, void

yes i find thank you

1 Like

why “click to play” does not work anymore

PImage img;
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”);
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

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("Click to Play ", 350, 240);//debut
text("Menu ", 385, 340);//menu

} 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
rect(340, 200, 220, 50);//encadrement
fill(255); //couleur

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);
rect(400, 600, 220, 50);//encadrement
fill(0); //couleur
text("RETOUR", 440, 640);

}
}//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
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

float x2=340; //emplacement cadre
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();
}

case 3:
float x3=400; //emplacement cadre
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

case 2:
float x4=340; //emplacement cadre
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();
}

}
}

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
}

// ============================================================================================
// 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)) {
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() { //poisition
if (xPos<0) {
xPos+=(2003);
opening = random(600)+100;
cashed=false;
}
if (xPos<250&&cashed==false) {
cashed=true;
score++;
}
}
}//class
//

Did you fix it?

Chrisir

1 Like

no unfortunately I do not understand it’s weird and you?

Did you understand the principles?

Look at function mousePressed.

Hit ctrl-t to get correct indents!

It has one switch starting with
switch (state) {
and ending with
} // switch

Then it has case 0: ... etc. for the different states. Each case section must end with break; before the next case... begins.

Check this please.

You now have errors

You still need to learn how to post code correctly. I described it above.

This is quite at the beginning of draw() and it makes no sense:

    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

it makes no sense because both buttons light up (not one) when you click somewhere on the screen. What you really want to achieve is that the button you hover over with the mouse (without clicking) lights up.

I deleted this part.

Chrisir

entire sketch


PImage img;
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");
  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(0);//ile/ fond

  if (state==0) {

    // intro 

    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("Click to Play ", 350, 240);//debut
    text("Instructions ", 355, 340);//menu
  } 
  // ---------------------------
  else if (state==1) {

    // GAME 

    background(0);//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
    rect(340, 200, 220, 50);//encadrement
    fill(255); //couleur

    text("Game Over", 350, 140);//fin
    text("Accueil", 390, 235);//score
  } 
  // ---------------------------
  else if (state==3) {
    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);
    rect(400, 600, 220, 50);//encadrement
    fill(0); //couleur
    text("RETOUR", 440, 640);
  }
}//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
    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
    }

    float x2=340; //emplacement cadre
    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();
    }
    break; 

    // -------

  case 3:
    float x3=400; //emplacement cadre
    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
    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();
    }//if
    break;

    // -------
  }//switch
}//func 

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
}//func 

// ============================================================================================
// 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, 7, 7); // 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)) {
        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() { //poisition
    if (xPos<0) {
      xPos+=(2003);
      opening = random(600)+100;
      cashed=false;
    }
    if (xPos<250&&cashed==false) {
      cashed=true;
      score++;
    }
  }
}//class
//

continued elsewhere by same user








I just miss 2 level in my game but I do not know how to do it. Thank you for your help

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; //inserer des switch pour inserer des “case”
//pour simplifier if et else lorsqu’il y a plusieur alternative
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) {

// début 

if (mousePressed && (mouseButton == LEFT)) { //appuyer sur le bouton
  fill(#2EFEF7);
} 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) {

// jeu 

background(ile);//ile/ fond

fill(255);
int s = second();  // Values from 0 - 59 #timer

line(s, 0, s, 33); 
fill(0);
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(#2EFEF7);
} 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((color) random(#000000));// fond multicolor
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(#2EFEF7);
} else {
  fill(0);
}
rect(400, 600, 220, 50);//encadrement
fill(255); //couleur
text("RETOUR", 440, 640);

}

/////////////////////////////////////////////////////////////////////////////////////////////////debut de niveau2 
  else if (state==4) {
  }
}//func

//////////////////////////////////////////////////////////////////////////////////fin de niv2

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) { //simplifi if et else lorsqu’il y a plusieur alternative

case 0:

float x1=340; //emplacement cadre  //niv1 //nombre decimaux
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) { //simplifi if et else lorsqu’il y a plusieur alternative

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 // niv2
float y5=400;
float w5=220;
float h5=50;

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


  state=4;
  reset();
}

}
switch(state) {

case 4:
float x6=400; //emplacement cadre // retour niv2
float y6=600;
float w6=220; //largeur
float h6=50; //hauteur

if (mouseX > x6 &&  //selection cadre
  mouseX < x6 + w6 && 
  mouseY > y6 &&
  mouseY < y6 + h6) { 


  state=0;
  reset();
}

}
}

void keyPressed() {
//sauter avec la barre d’espace
switch(state) {
case 0: //different cas possibles
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 { //class --) definit un objet

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
//

Wait.

  • I posted your entire code with 2 levels in your other thread yesterday. This thread here should be closed.

  • Also, you said, you had to hand it in. Did you?

  • Also, you still don’t know how to post code.

  • And read the reference on switch: https://www.processing.org/reference/switch.html

Chrisir

See: Two level to my game

excuse me I had not seen

1 Like

@macabol Please learn how to post code properly. Looking at your code makes my eyes bleed, and only one thing could fix that: image
And it’s a literal single button click to do it.
Please, for everyone’s sake just press the damn button.

2 Likes