// Method to check if the two YinYang shapes have collided with each other, // and subsequently perform scaling operations if they have private void checkForCollision() { // Creates two MyYinYang objects with the already used coordinates // for use in the changing of color, direction, and scale once they // have collided yin1 = new MyYinYang(yin1X, yin1Y, yin1Width, yin1Height); yin2 = new MyYinYang(yin2X, yin2Y, yin2Width, yin2Height); if (hasCollided(yin1)) { // If the two shapes have collided, then the // shapes will reverse direction to go on until they // hit the edges of the screen // The reason the directions are not random // is because if they don't bounce away // from each other, they keep colliding // until they fly off of the screen deltaX1 = deltaX1 * -1; deltaY1 = deltaY1 * -1; deltaX2 = deltaX2 * -1; deltaY2 = deltaY2 * -1; // Commands to scale the two Yin Yang shapes based on the current // size when being compared to each other // IF SECTION IS IF YIN1 IS LARGER, ELSE IS IF THE SECOND IS LARGER double scale = 0; if (yin1.getWidth() >= yin2.getWidth()) { // Uses a do while loop so that it executes once, but if // the scale ends up being 0, it does it again do { // Scales by a random double between .5 and 1 scale = ((randomNumber.nextDouble() + .5) - randomNumber.nextDouble()); } while (scale <= 0); // Yin1Width is now set to it's original value times the scale // which has been set yin1Width = (int) (scale * yin1Width); // Simple equality so that the height and width are the same, since it is a circular object yin1Height = yin1Width; do { // Scales by a factor between 1 and 2 scale = randomNumber.nextDouble() + 1; } while (scale <= 0); yin2Width = (int) (yin2Width * scale); yin2Height = yin2Width; } else { do { scale = randomNumber.nextDouble() + 1; } while (scale <= 0); yin1Width = (int) (yin1Width * scale); yin1Height = yin1Width; do { scale = ((randomNumber.nextDouble() + .5) - randomNumber.nextDouble()); } while (scale <= 0); yin2Width = (int) (yin2Width * scale); yin2Height = yin2Width; } } }
@Override public void mousePressed(MouseEvent event) { x1Clicked = event.getX(); y1Clicked = event.getY(); x2Clicked = event.getX(); y2Clicked = event.getY(); // Conditions to update the score depending on speed and size of shapes if (yin1.getBounds().contains(x1Clicked, y1Clicked)) { if (yin1.getWidth() > 250) { if (deltaX1 < 2) { points += 1; } else if (deltaX1 < 3) { points += 3; } else if (deltaX1 < 6) { points += 5; } else { points += 7; } } else if (yin1.getWidth() > 150) { if (deltaX1 < 2) { points += 15; } else if (deltaX1 < 3) { points += 35; } else if (deltaX1 < 6) { points += 55; } else { points += 75; } } else { if (deltaX1 < 2) { points += 125; } else if (deltaX1 < 3) { points += 225; } else if (deltaX1 < 6) { points += 325; } else { points += 425; } } } else if (yin2.getBounds().contains(x2Clicked, y2Clicked)) { if (yin2.getWidth() > 250) { if (deltaX2 < 2) { points += 1; } else if (deltaX2 < 3) { points += 3; } else if (deltaX2 < 6) { points += 5; } else { points += 7; } } else if (yin2.getWidth() > 150) { if (deltaX2 < 2) { points += 15; } else if (deltaX2 < 3) { points += 35; } else if (deltaX2 < 6) { points += 55; } else { points += 75; } } else { if (deltaX2 < 2) { points += 125; } else if (deltaX2 < 3) { points += 225; } else if (deltaX2 < 6) { points += 325; } else { points += 425; } } } else { misses--; } }
public boolean hasCollided(MyYinYang y1) { // Checks to see if the bounds of the first yinYang intersect anywhere within // the bounds of the second yinYang return y1.getBounds().intersects(yin2X, yin2Y, yin2Width, yin2Height); }