Faster and faster squares (Click game)

Hello,
I would like in my game that every 15 seconds the speed of appearance of the edges increases by x time (example the first 15 seconds is 1 square every 1 second then the next 15 seconds will be 1 square every 0.7seconds … etc) except that when I had done it it had worked but in reverse.

int wait = 1000;
int lastTime = 0; 
int score = 0; 
int score2 = 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;
PImage img;
PFont police;

void setup() {
  size(900,600);         //feunetre 900x600
  smooth();              //dessine la geometrie avec des arretes lisses
  police = loadFont("Arial-Black-48.vlw");         //charger police d'écriture 
  img=loadImage("Sans titre-67.jpg");        //quel image je dois charger (au prealable mis dans data)
}

//**********Changement de fenetre*********\\

void draw() {
  switch (state) {
  case menu:
    showMenu();       //developpement plus bas
    break;             //passer a l'instruction suivante 
  case jeux:
    handlejeux();    //developpement plus bas
    break;            //passer a l'instruction suivante 
  default:
    println ("Unknown state (in draw) "
      + state
      + " ++++++++++++++++++++++");
    exit();      //quitter le programme
    break;            //passer a l'instruction suivante 
  } 
}

//**********Multiplicateur de score (score de base + bonus)*********\\

void mousePressed() {
  if (state!=jeux)
    return; 

 if ( over_square() ) {     
    score += bonus;         //plus j'appuye plus je gagne du score
    bonus++;

    score2 += 1;         
  } else {
    score = 0;              //score de base
    bonus = 1;              //score +1 par combo

    score2 = 0;        //autre score mais avance de 1
  }
  randomize_square_position();
}

void keyPressed() {
  switch (state) {
  case menu:
    keyPressedForMenu();   //developpement plus bas
    break;            //passer a l'instruction suivante 
  case jeux:
    keyPressedForjeux();   //developpement plus bas
    break;           //passer a l'instruction suivante 
  default:
    println ("Unknown state (in keypressed) "
      + state
      + " ++++++++++++++++++++++");
    exit();        //quitter le programme
    break;          //passer a l'instruction suivante 
  } 
} 

//**********Action si touche pressé (changement de background)*********\\

void keyPressedForMenu() {

  switch(key) {
  case '1':             //si j'appuye sur 1....
    state = jeux;        //changer ecran sur jeux
    break;         //passer a l'instruction suivante 
    
  case 'x':         //si j'appuye sur x....
  case 'X':         //ou X
    exit();         //quitter
    break;        //passer a l'instruction suivante 
  }
} 

void keyPressedForjeux() {
  switch(key) {
  default:
    key=0; 
    state = menu;
    break;       //passer a l'instruction suivante 
  }

} 

//**********Apparence visuel du menu (emplacement des textes...etc) **********\\

void showMenu() {             //Mon ecran menu
  image (img,0,0);           //joli photo fais pas mes soins ;)
} 

//**********Mon jeux**********\\

void handlejeux() {        //mon ecran jeux
  background(78, 61, 40);          //couleur du fond noir 
  fill(255);                       //couleur blanche
  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é
  textFont(police,32);        //mettre la police d'écriture
  text("Score = ", 680,50, height-25);            //Afficher "Score =" a certaine coo
  textFont(police,32);        //mettre la police d'écriture
  textSize(32);                                   //taille du texte
  textFont(police,32);        //mettre la police d'écriture
  text(score, 830,50, height-25);                //coordonnée du score
  textFont(police,32);        //mettre la police d'écriture
  text("X ", 30,570, height-25);                 //Afficher "X" a certaine coo
  textFont(police,32);        //mettre la police d'écriture
  text(score2, 60,570, height-25);               //coordonnée du score
}

void randomize_square_position() {                 //commande du random spawn
  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
    );
}

This is your variable wait (see this line: if (lastTime + wait < millis()) {).

wait is 1000 (milliseconds, so 1 second)

you can reduce wait by 90 in randomize_square_position()

Yes I know but I would like the speed to change every 15 seconds. With what I have already I can just change the speed but it will always remain the same speed it will not increase progressively

Okay, make a timer for this and say wait -= 90;

You already have another timer so you should be able to do it on your own

To decrease the wait every 15 seconds you can just use

float wait = 1000 - millis()/15000*90; //gradual decrease (\)
float wait2 = 1000 - floor(millis()/15000)*90; //sharp decrease (`-_)

Thanks but it is marked every time: “unexpected token : float”
Its the : "int lastTime = 0 ; " which poses a problem?

I forgot to add a ;
I edited the code now (add a ; before the comment)

Thanks but I have to edit

  if (lastTime + wait < millis()) {             //si j'ai pas click au bout de X temps
    randomize_square_position();                //alors nouveau carré

for this to work because I don’t see any difference with just the variables

yeah don’t worry, I was just providing an alternative. Feel free to use anything, it’s your code after all : P

1 Like