Small problem with counter

edited March 2018 in Questions about Code

Sorry for lack of comments in code feel free to ask questions about code, but this is my code so far, its 99% complete, however I have small issue, when you press the down arrow key, you will shoot a flame out, when this hits a ball it will add 1 to hits, however if you hit 2 balls with one flame it will add 2 to hits, I only want it to add 1 to hits no matter how many balls you hit with one flame (copy paste code and run for a better understanding).

```

int[] orbit = {50, 100, 150, 200, 250};

float[]speed = new float [5];

float[] angle = new float [5];

color[] c = new color [255];

float[] planetx = new float [5];

float[] planety = new float [5];

int sunX = 200; Int sunY = 25;

int flameX = sunX;

int flameY = -10000;

int flames = 5;

int hits = 0;

int planetcount = 5;

float collision;

boolean[] hitornot = new boolean [5];

boolean hitred[] = new boolean [5];

void hitcount() {

for (int i = 0; i < orbit.length; i++) {

collision = dist(flameX, flameY, planetx[i], planety[i]);

if (collision <= 10 + 10/2 && !hitornot[i] ) {

  hitornot[i] = true;

  hits++;

  planetcount--;

}

}

}

void setup() {

background(148, 103, 252);

size(400, 400);

//populating the arrays

for (int i = 0; i<orbit.length; i++) {

speed[i] = random(0.02, 0.05);

angle[i] = random(2*PI);

c[i] = color(random(255), random(255), random(255));

if ( random(0, 1) > 0.5) {

  speed[i] *= -1;

  hitornot[i] = false;

}

}

}

void keyPressed() {

if (key == CODED) {

if (keyCode == DOWN) {

  flameX = sunX;

  flameY = sunY;     

  flames--;

  flames = max(0, flames);

}

}

if (key == CODED) {

if (keyCode == RIGHT && sunX < width-25) {

  sunX++;

}

}

if (key == CODED) {

if (keyCode == LEFT && sunX >25) {

  sunX--;

}

}

}

void drawPlanet() {

for (int i = 0; i < orbit.length; i++) {

planetx[i] = width/2 + orbit[i]/2 *cos(angle[i]);

planety[i] = width/2 + orbit[i]/2 *sin(angle[i]);

angle[i]+=speed[i];

fill(c[i]);

noStroke();

if (red(c[i]) < 255 || green(c[i]) >0 || blue(c[i]) >0) {

  ellipse(planetx[i], planety[i], 20, 20);

}

}

}

void Hitred() {

for (int i = 0; i < hitred.length; i++) {

if (hitornot[i]) {

  c[i] = color (red(c[i]) +10, green(c[i]) -20, blue(c[i]) -20);

}

}

}

void flame() {

fill(255, 255, 0);

noStroke();

ellipse(flameX, flameY, 10, 10);

/* if (flameY >= 400) {

flameY = 10000000;

}*/

}

void draw() {

background(148, 103, 252);

//draws earth shadow

noStroke();

fill(30, 40, 0);

ellipse(200, 400, 400, 120);

//draws sun

noStroke();

fill(255, 255, 0);

ellipse(sunX, 25, 25, 25);

//draws glow

fill(255, 255, 0, 120);

ellipse(sunX, 25, 50, 50);

//draws orbits for planets to travel around

for (int i = 0; i < orbit.length; i++) {

noFill();

stroke(103, 230, 252);

ellipse(200, 200, orbit[i], orbit[i]);

}

//calling the functions

drawPlanet();

flame();

Hitred();

flameY += 5;

hitcount();

textSize(24);

text("Flames: " + flames, 20, 50);

text("Hits: " + hits, 20, 75);

if ((flames <= 0 && flameY >= height) || hits == 5) {

sunY = 450;

background(20, 100, 200);

fill(255);

text("Game is Over ", 75, 100);

text("Number of Flames left: " + flames, 75, 150);

text("Number of Planets left: " + planetcount, 75, 200);

text("Number of hits: " + hits, 75, 250);

}

}

```

Answers

  • Ummm, sorry for the code formatting, idk how to use forums haha

  • edit post, highlight code, press ctrl-o to format

  • Taking a wild guess here as the code is hard to follow (please format)... This next:

    if (collision <= 10 + 10/2 && !hitornot[i] ) {
      hitornot[i] = true; 
      hits++; 
      planetcount--; 
    }
    

    Should be replace with:

    boolean gloabalHit=false;
    if (collision <= 10 + 10/2 && !hitornot[i] ) {
      hitornot[i] = true; 
      globalHit |=  hitornot[i];  //Set globalHit to true if at least one is hit
      planetcount--; 
    }
    
    //Now increase it once
    if(globalHit) { hits++; }
    

    Kf

Sign In or Register to comment.