class Soldier { // -------------------------------------------------------------------- // CONSTANTS // -------------------------------------------------------------------- int MIN_SIZE = 10; // -------------------------------------------------------------------- // VARIABLES // -------------------------------------------------------------------- float xPos, yPos; float dX, dY; int strength; color colour; boolean isAlive; // -------------------------------------------------------------------- // CONSTRUCTOR // -------------------------------------------------------------------- Soldier(float x, float y) { xPos = x; yPos = y; strength = 1; colour = color(random(100, 255), random(100, 255), random(100, 255)); isAlive = true; // randomly set a start direction and speed dX = random(3); dY = random(3); if (random(1) < .5) dX *= -1; if (random(1) < .5) dY *= -1; } // -------------------------------------------------------------------- // METHODS // -------------------------------------------------------------------- /* moves the soldier and make it bounce off the walls */ void move() { float radius = (strength+MIN_SIZE)/2.0; // if the soldier hit a wall, bounce back if (yPos+radius >= height) { // bottom wall dY *= -1; } else if (yPos-radius <= 0) { // top wall dY *= -1; } if (xPos+radius >= width) { // right wall dX *= -1; } else if (xPos-radius <= 0) { // left wall dX *= -1; } xPos += dX; yPos += dY; } /* draws the soldier */ void draw() { // draw the shape first fill(colour); ellipse(xPos, yPos, strength+MIN_SIZE, strength+MIN_SIZE); // draw the number over it fill(0); text(strength, xPos, yPos); } /* checks if the current soldier collides with the passed other soldier */ boolean collidesWith(Soldier other) { float distance = dist(xPos, yPos, other.xPos, other.yPos); float sumRadius = (strength+MIN_SIZE)/2.0 + (other.strength+MIN_SIZE)/2.0; if (distance < sumRadius) { return true; } return false; } }