Hello,
after multiple help and time I have this code:
int wait = 1000;
int lastTime = 0;
int score = 0;
int bonus = 1;
int square_size = 50;
float square_x;
float square_y;
void setup() {
fullScreen();
}
void draw() {
background(0);
fill(255);
if (lastTime + wait < millis()) {
randomize_square_position();
}
rect(square_x, square_y, square_size, square_size);
text("Score = ", 20, 1050);
textSize(32);
text(score, 150, 1050);
}
void mousePressed() {
if ( over_square() ) {
score += bonus;
bonus++;
} else {
score = 0;
bonus = 1;
}
randomize_square_position();
}
void randomize_square_position() {
square_x = random(width - square_size);
square_y = random(height - square_size);
lastTime = millis();
}
boolean over_square() {
return(
mouseX > square_x &&
mouseX < square_x + square_size &&
mouseY > square_y &&
mouseY < square_y + square_size
);
}
and I would like to add before starting the game directly a menu with the Start button which, when I press it displays the game for the moment I have this (I think I have mixed everything) :
int wait = 1000;
int lastTime = 0;
int score = 0;
int bonus = 1;
int square_size = 50;
float square_x;
float square_y;
final int menu = 0;
final int jeux = 1;
int state = menu;
PFont font;
void setup() {
fullScreen(); //fullscreen
smooth();
font = createFont("ARCARTER-78.vlw", 14);
textFont(font);
}
void draw()
{
switch (state) {
case menu:
showMenu();
break;
case jeux:
handlejeux();
break;
default:
println ("Unknown state (in draw) "
+ state
+ " ++++++++++++++++++++++");
exit();
break;
} // switch
}
{
background(0); //couleur du fond
fill(255);
if (lastTime + wait < millis()) { //si j'ai pas click au bout de X temps
randomize_square_position(); //alors nouveau carré
}
rect(square_x, square_y, square_size, square_size); //dessin du carré
text("Score = ", 20, 1050); //Afficher "Score =" a certaine coo
textSize(32); //taille du texte
text(score, 150, 1050); //coordonnée du score
}
void mousePressed() {
if ( over_square() ) {
score += bonus; //plus j'appuye plus je gagne du score
bonus++;
} else {
score = 0; //score de base
bonus = 1; //score +1 par combo
}
randomize_square_position();
}
void randomize_square_position() {
square_x = random(width - square_size); //Spawn random de carré sur les coo X
square_y = random(height - square_size); //Spawn random de carré sur les coo Y
lastTime = millis(); //depop si trop long
}
boolean over_square() {
return(
mouseX > square_x && //si je click sur le carré, alors disparition
mouseX < square_x + square_size &&
mouseY > square_y &&
mouseY < square_y + square_size
);
}
void keyPressed() {
switch (state) {
case menu:
keyPressedForMenu();
break;
case jeux:
keyPressedForjeux();
break;
default:
println ("Unknown state (in keypressed) "
+ state
+ " ++++++++++++++++++++++");
exit();
break;
}
}
void keyPressedForMenu() {
switch(key) {
case '1':
state = jeux;
break;
case '2':
case 'x':
case 'X':
exit();
break;
default:
break;
}
}
void keyPressedForjeux() {
switch(key) {
default:
state = menu;
break;
} // switch
//
} // func
void showMenu() {
background(255, 204, 0);
fill(0);
textSize(32);
text(" OSU ", 150, 100, 3);
textSize(14);
text("Press 1 Jouer ", 100, 200);
text("Press 2 Stats Parties ", 100, 220);
text("Press x Pour Quitter ", 100, 260);
}
void handlejeux() {
background(255, 204, 0);
fill(0);
textSize(32);
text(" Jouer ", 150, 100, 3);
textSize(14);
text("..... some text ", 100, 200);
}