Score & life count issues.

Hi there guys,

Really new to programming so go easy and forgive me aha. I have been tasked with creating a game for university using various Arduino bits.

I'm using an Arduino Uno R2 and a TinkerKit Sensor Shield. I have connected a joystick like module to the shield.

My game is simple. The black hole (controlled by the joystick) eats the planets (enemies in my code).

I am trying to display a score & life count on screen for each one of these enemies eaten. 1 point for one enemy. If they reach the end of the screen without being eaten, a life is taken away.

My issue is that when an enemy is eaten, the score rapidly increases without stopping. I could eat one enemy and move the joystick away and it's in the 1000s (still increasing). I'm wondering if it's a joystick influence?

The same thing happens to the lives. If one enemy reaches the end, the life count rapidly decreases.

Sorry, that was a bit long winded.
TLDR: My score count doesn't stop increasing

I'm really sorry if this is something simple i'm missing.

Enemy[] enemies = new Enemy[1000];

PImage bg, blackHole;

int scoreCount;
int lifeCount;

int totalEnemies;
int startTime;
float enemyAttack = random(1000,3000);

float new_x;
float new_y;

import processing.serial.*;
import cc.arduino.*;
import com.tinkerkit.*;

Arduino arduino;

//declare the joystick
TKJoystick joystick;

void setup() {  

  size(1280,800);
  
bg = loadImage("stars-07.jpg");
blackHole = loadImage("blackhole.png");

scoreCount = 0;
lifeCount = 5;

  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[3], 57600);    
  
    joystick = new TKJoystick(arduino, TK.I0, TK.I1);

  //for every tinkerkit component we have to pass
  //the arduino and the port
 
  //style
  noStroke();
  fill(255);
  smooth();
  startTime = millis();
  
   //Initialise all objects (enemies)
 
    for (int i = 0; i < enemies.length; i++) {
    enemies[i] = new Enemy();
  }
  
}

void draw() {

 background(bg);
 
 //read joystick values
 float bh_x = joystick.readX();
 float bh_y = joystick.readY(); 
 
 //map values on the window width and height
 bh_x = map(bh_x,0,1023,0,width);
 bh_y = map(bh_y,0,1023,0,height);
 
 new_x = bh_x;
 new_y = bh_y;
 
image(blackHole, bh_x,bh_y,150,150);
println("x: " + bh_x + " , y: " + bh_y);
 
 //Spawn the first enemy 1-3s after the game starts, then every
//1-2s after that
  
  if ((millis() - startTime) > enemyAttack)  {
    enemies[totalEnemies].alive = true;
    
    if (totalEnemies >= enemies.length) {
  totalEnemies = 0;
    }
    
    totalEnemies++;
    startTime = 0;
    enemyAttack = enemyAttack + (random(1000,2000));
  }
    
//Display enemies (only if alive/after elapsed time)

  for (int i = 0; i < enemies.length; i++) {
    enemies[i].move();
    enemies[i].display();
    
    if (i == enemies.length) {
  i = 0;
    }

//Collision 

   if (dist(enemies[i].x + 20, enemies[i].y - 80, new_x, new_y)<=100){ 
      enemies[i].kill();
      scoreCount++;
      fill(#ffffff);                
      text("score = "+scoreCount,100,100);
      println(new_x);
      println(scoreCount);
     
    }
    

//Kill enemies if they reach the left side of the screen

    if (enemies[i].reachedEnd()) {
    enemies[i].kill();
    lifeCount--;
    fill(#ffffff);
    text("lives = "+lifeCount,500,500);
    
    }
  }
}

  class Enemy {

  float x,y;
  float speed;
  color c;
  int r;
  boolean alive;

  Enemy() {
    r = 25;
    x = height + r*17;
    y = random(height);
    speed = random(1,3);
    c = color(234,76,76);
    alive = false;
  }

  void move() {
    if (alive) {      //Only move if alive (after elapsed time)
  x -= speed;     //Move left
    }
  }

//Determines whether the enemies have reached the left side of the
//screen

  boolean reachedEnd() {
    if (x < 0 - r*2) {
  return true;
    } 
    else {
  return false;
    }
  }

  void display() {
    if (alive) {      //Display only if alive (after elapsed time)
  fill(c);
  stroke(0);
  ellipse(x,y,r,r);
    }
 
  }
  
  void kill() {
    alive = false;
  } 
}
if (dist(enemies[i].x + 20, enemies[i].y - 80, new_x, new_y)<=100
  // ADD THIS CHECK  
  && enemies[i].alive
){

Better still, just skip the dead enemies at the top of your loop:

  for (int i = 0; i < enemies.length; i++) {
    if(!enemies[i].alive) continue;

    // rest of code goes here.

Oh incidentally, when computing a collision distance, don’t bother taking the square root - it wastes cycles. Just check if the square of the pdistance (xdxd+ydyd) is less than the square of the kill radius (100000.0).

PaulMurrayCbr:

if (dist(enemies[i].x + 20, enemies[i].y - 80, new_x, new_y)<=100

// ADD THIS CHECK 
  && enemies[i].alive
){




Better still, just skip the dead enemies at the top of your loop:


for (int i = 0; i < enemies.length; i++) {
    if(!enemies[i].alive) continue;

// rest of code goes here.




Oh incidentally, when computing a collision distance, don't bother taking the square root - it wastes cycles. Just check if the square of the pdistance (xd*xd+yd*yd) is less than the square of the kill radius (100000.0).

Thank youuuuuuuuu! This worked a charm.