I want this counter to stop at zero? Instead of going into the negatives

edited January 2016 in Questions about Code

Might not be the most advanced thing ever, but i cant seem to figure it out! Pretty much, its an FPS game, sort of. The bullet counter and max amount of bullets go into the negatives. Is there a way to make it stop at 0?

Program:

PImage img;
PImage gun;
int minus;
int add;
int current;
int end;
int stop;
int cap;
int low;
int a;
int b;
int up;


void setup() {
  background(0);
  size(displayWidth, displayHeight);
  frameRate(30);
  img = loadImage("bo2.jpg");
  img.loadPixels();
  loadPixels();
  noCursor();
  gun = loadImage("raygun.png");
  gun.resize(700,700);


minus = -1;
add = 20;
current = 20;
end = 11;
stop = 0;
cap = 100;
low = -20;
a = 1;
b = 1;
up = 1;

}

void draw() {

  {



  { 
    for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++ ) {
      int loc = x + y*img.width;
      float r,g,b;
      r = red (img.pixels[loc]);
      float maxdist = 50;
      float d = dist(x, y, mouseX, mouseY);
      float adjustbrightness = 255*(maxdist-d)/maxdist;
      r += adjustbrightness;
      r = constrain(r, 0, 255);
      color c = color(r);
      pixels[y*width + x] = c;

    }
  }
  updatePixels();

  {
    stroke(255);
    fill(0);
  ellipse(mouseX, mouseY, 20,20);

  }


  if(mouseButton==LEFT) {
    fill(0);
    ellipse(mouseX, mouseY-50, 15,15);
    ellipse(mouseX+20, mouseY-48, 15, 15);
    ellipse(mouseX-20, mouseY-48, 15, 15);
    //top row
    ellipse(mouseX, mouseY+50, 15, 15);
    ellipse(mouseX+20, mouseY+48, 15, 15);
    ellipse(mouseX-20, mouseY+48, 15, 15);
    //bottom row
    ellipse(mouseX-50, mouseY, 15, 15);
    ellipse(mouseX-48, mouseY+20, 15, 15);
    ellipse(mouseX-48, mouseY-20, 15, 15);
    //left row
    ellipse(mouseX+50, mouseY, 15, 15);
    ellipse(mouseX+48, mouseY+20, 15, 15);
    ellipse(mouseX+48, mouseY-20, 15 ,15);
    //right row

  }
  {
    if(mouseButton==RIGHT) {
      sight = loadImage("raygun1.png");


  }
  }

  {
   image(gun, mouseX+175, mouseY+5); 
  }

  {
    rect(1000, 0, 439, 50);

  }

  {
    fill(255);
    textSize(25);
      text(current, 1010,35);
      text(cap, 1055, 35);
      text("/", 1040, 35);

    }


  {
    if(a == 1) {

      if(mousePressed) {

      current += minus;   

     a = 2;

  }

  if(mousePressed) {
    a = up;

    }
    }

   if(current <= stop) {
        fill(255, 0, 0);
        textSize(30);
        text("Press [R] to Reload", 500, 40);
      }

  }

  if(keyPressed) {
    if(key == 'r' || key =='R') {
      current += add;
      cap += low;
    }
  } 

}
  }
}

If you want the same pictures i used (links):

bo2 image: http://www.gamespersecond.com/media/2013/01/black-ops-2-revolution-hydro.jpg

raygun image: http://images2.wikia.nocookie.net/__cb20111027181902/callofdutyzombies/images/b/bc/Ray_Gun_1st_Person_BO.png

Tagged:

Answers

  • i was thinking, would something like mouseReleased() work for shooting and keyReleased() for reloading work?

  • edited October 2013 Answer ✓

    General advice: avoid loading images in draw(), it can slow down your sketch. Pre-load all images in setup(), putting them in global variables.

    A quick scan of the code doesn't show when you decrease something. I see no minus sign. Actually, I don't see a bullet counter or max amount, but global variables named a or b doesn't help in understanding the code...

    Basically, use an if condition: if (amount > 0) amount--;
    So if the value reaches zero, it won't be decreased.

  • Use a simple if statement.

Sign In or Register to comment.