/**
   * This method detects whether the first sprite is overtop of the second sprite.
   *
   * @param one The first sprite
   * @param two The second sprite
   * @return Returns true if the two sprites are overtop of each other and false if they are not
   */
  public static boolean detectCollision(Sprite one, Sprite two) {
    // To prevent errors concerning concurrent modification
    if (one != null && two != null) {
      // X coordinate detection
      int oneLeft = one.getX();
      int oneRight = one.getX() + one.getWidth();
      int twoLeft = two.getX();
      int twoRight = two.getX() + two.getWidth();
      boolean overLeftSide = (oneRight >= twoLeft) && (oneRight <= twoRight);
      boolean overRightSide = (oneLeft <= twoRight) && (oneLeft >= twoLeft);
      boolean overHoriz = (oneLeft <= twoLeft && oneRight >= twoRight);

      // Y coordinate detection
      int oneTop = one.getY();
      int oneBottom = one.getY() + one.getHeight();
      int twoTop = two.getY();
      int twoBottom = two.getY() + two.getHeight();
      boolean overTopSide = (oneBottom >= twoTop) && (oneBottom <= twoBottom);
      boolean overBottomSide = (oneTop <= twoBottom) && (oneTop >= twoTop);
      boolean overVert = (oneTop <= twoTop && oneBottom >= twoBottom);

      if ((overLeftSide || overRightSide || overHoriz)
          && (overTopSide || overBottomSide || overVert)) {
        return true;
      }
    }

    return false;
  }
Beispiel #2
0
  public boolean collidesWith(Entity other) {

    me.setBounds((int) x, (int) y, sprite.getWidth(), sprite.getHeight());
    him.setBounds((int) other.x, (int) other.y, other.sprite.getWidth(), other.sprite.getHeight());

    return me.intersects(him);
  }
Beispiel #3
0
  public void renderSprite(int xp, int yp, Sprite sprite, boolean fixed) {
    if (fixed) {
      xp -= xOffset;
      yp -= yOffset;
    }

    for (int y = 0; y < sprite.getHeight(); y++) {
      int ya = y + yp;
      for (int x = 0; x < sprite.getWidth(); x++) {
        int xa = x + xp;
        if (xa < 0 || xa >= width || ya < 0 || ya >= height) continue;
        if (sprite.transparent
            && sprite.pixels[x + y * sprite.getWidth()] == sprite.transparentColor) continue;
        pixels[xa + ya * width] = sprite.pixels[x + y * sprite.getWidth()];
      }
    }
  }
  @Override
  public void run() {
    while (running) {
      if (lastTime != 0) {

        elapsedTime();

        switch (game_state) {
          case SPLASH_SCREEN:
            break;
          case MAIN_MENU:
            break;
          case PLAYING:
            // game logic goes here

            // example:
            // move sprite
            xPos += xVel * percent;
            yPos += yVel * percent;

            // check x edge collision
            if (xPos < 0) {
              xPos = 0;
              xVel *= -1;
            } else if (xPos > win_width * scale) {
              xPos = win_width * scale;
              xVel *= -1;
            }

            // check y edge collision
            if (yPos < 0) {
              yPos = 0;
              yVel *= -1;
            } else if (yPos > win_height * scale) {
              yPos = win_height * scale;
              yVel *= -1;
            }

            // touch star
            if (touching
                && touchX >= xPos
                && touchX <= xPos + sprite.getWidth() * scale
                && touchY >= yPos
                && touchY <= yPos + sprite.getHeight() * scale) {
              xVel *= -1;
              yVel *= -1;
              xPos = (float) Math.random() * win_width * scale;
              yPos = (float) Math.random() * win_height * scale;
              mVibrator.vibrate(22);
            }
            break;
        }
        elapsedTime();
      }

      try {
        Thread.sleep(10);
      } catch (InterruptedException ex) {
        // do nothing
      }
    }
  }