How I can do score

Hello, I’m doing a game and I’m new to Processing, I’ve done this,I’ve been looking for videos, in forums and nothing, I’m having problems in the score, every time I give it where it has to add +1, it adds much more.

PImage pentagon;
PImage triangle;
PImage sis;
PImage cinc;
PImage quatre;
PImage tres;
PImage rectangle;
int state; 
int points;
boolean pCount = true;
void setup() {
  size(400, 600);
  background(255);
  quatre = loadImage("RECTAANGULO.png");
  tres = loadImage("TRIAANGULO.png");
  rectangle = loadImage("RECTANGLE.png");
  sis = loadImage("HEXAAGONO.png");
  cinc = loadImage("PENTAAGONO.png");
  triangle = loadImage("TRIÀNGLE.png");
  pentagon = loadImage("PENTÀGON.png");
  } 
  
void draw(){
    fill(0);
   textSize(20);
   text("puntuacion:" + points , 20, 550);
   
  if (state == 0) {
   image(rectangle,30,100);
   image(tres,200,220);
   image(quatre,0,220);
   if((mousePressed)&&(mouseX > 20 && mouseX < 190) && (mouseY > 150 && mouseY < 300)){
     ++points;
  }
  }
  if (state == 1) {
   image(pentagon,50,100);
   image(cinc,200,220);
   image(sis, 0, 220);
   if((mousePressed)&&(mouseX > 20 && mouseX < 190) && (mouseY > 150 && mouseY < 300)){
     ++points;
  }
  }
  if (state == 2) {
   image(triangle,30,100);
   image(tres,200,220);
   image(cinc,0,220);
   if((mousePressed)&&(mouseX > 20 && mouseX < 190) && (mouseY > 150 && mouseY < 300)){
     ++points;
  }
  
  }
   if (state == 3) {
   image(rectangle,30,100);
   image(sis,200,220);
   image(quatre,0,220);
   if((mousePressed)&&(mouseX > 20 && mouseX < 190) && (mouseY > 150 && mouseY < 300)){
     ++points;
  }
   }
   
}


void mouseClicked() {
  state++;
  if(state == 4){
    state = 0;
  }
}
1 Like

Compare this code:

int counter;

void setup(){
  size(400,400);
}

void draw(){
  background(0);
  if(mousePressed){
    counter++;
  }
  fill(255);
  text( counter, 20, 20);
}

To this code:

int counter;

void setup() {
  size(400, 400);
}

void draw() {
  background(0);
  fill(255);
  text( counter, 20, 20);
}

void mousePressed() {
  counter++;
}

See the difference? By using the mousePressed boolean in a conditional statement in draw() you get a condition that might be true for several iterations of draw(). That is, one click might span several frames of drawing.

But if you use the mousePressed() function, then this function only runs once per click. Thus code inside it only executes once per click.

2 Likes

THANK YOU!!. I got it, but when I got the points every time, the numbers are put on top of each other.
pedrito

You probably need to clear the background then!

Add:

  background(255);

As the first line of draw().

1 Like

You are the best…

1 Like