コード例 #1
0
ファイル: Panel.java プロジェクト: TOL1990/courses
 @Override
 public void paint(Graphics g) {
   super.paint(g);
   for (Ball b : ballsList) {
     g.setColor(b.getColor());
     g.fillOval(b.getX(), b.getY(), b.getSize(), b.getSize());
     b.move(getWidth(), getHeight());
   }
 }
コード例 #2
0
ファイル: Breakout.java プロジェクト: BuyouT/COP3502
    @Override
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;

      // paint ball, paddle, and brick configuration
      g2.drawImage(background.getImage(), 0, 0, null);
      bconfig.paint(g2);
      paddle.paint(g2);
      ball.paint(g2);
      g2.setColor(Color.WHITE);
      g2.setFont(new Font("Serif", Font.PLAIN, 20));
      g2.drawString("Score: " + score, 15, 20);
      if (ball.getY() > 500) {
        g2.setColor(Color.WHITE);
        g2.setFont(new Font("Serif", Font.PLAIN, 30));
        g2.drawString("Game Over!", 200, 300);
        g2.drawString("Your final score was " + score, 150, 330);
        timer.stop();
      }
    }
コード例 #3
0
ファイル: Paddle.java プロジェクト: khatanbaatar/CS-2302
  /**
   * Determines if the given ball has collided with the paddle and returns true if it has and false
   * if it has not. If the ball has collided with the paddle the balls movement vectors are modified
   * so the ball will bounce back at the proper angle.
   *
   * @param ball is the ball to examine to see if it has collided with the paddle
   * @return Returns true if ball collides with paddle and if so updates vectors
   */
  public boolean collision(Ball ball) {
    // Temp variables for paddle corner coordinates;
    int pX1 = this.x;
    int pX2 = pX1 + this.width - 1;
    int pY1 = this.y;
    int pY2 = pY1 + this.height - 1;

    // Temp variables  for ball coordinates, movement vectors, and radius
    int bX = ball.getX();
    int bY = ball.getY();
    int vX = ball.getXVector();
    int vY = ball.getYVector();
    int bRad = ball.getRadius();

    // Testing for collisions with the paddle edges and the Ball
    if (bX < pX1 && bY >= pY1 && bY <= pY2 && vX > 0) // left edge
    {
      if (bX + bRad > pX1) // will collide with edge
      {
        ball.setX(pX1 - bRad);
        ball.setXVector(-Math.abs(vX)); // Change x direction
        return true;
      }
    } else if (bX > pX2 && bY >= pY1 && bY <= pY2 && vX < 0) // right edge
    {
      if (bX - bRad < pX2) // will collide with edge
      {
        ball.setX(pX2 + bRad);
        ball.setXVector(Math.abs(vX)); // Change x direction
        return true;
      }
    } else if (bY < pY1 && bX >= pX1 && bX <= pX2 && vY > 0) // top edge
    {
      if (bY + bRad > pY1) // will collide with edge
      {
        ball.setY(pY1 - bRad);
        ball.setYVector(-Math.abs(vY)); // Change y direction
        return true;
      }
    } else if (bY > pY2 && bX >= pX1 && bX <= pX2 && vY < 0) // bottom edge
    {
      if (bY - bRad < pY2) // will collide with edge
      {
        ball.setY(pY2 + bRad);
        ball.setYVector(Math.abs(vY)); // Change y direction
        return true;
      }
    } else // Otherwise test to see if the ball hits a corner of the paddle
    {
      if (ball.distance(pX1, pY1) <= bRad
          && ball.distance(pX1, pY1) <= ball.distance(pX2, pY1)) { // top-left corner collision
        if (Math.abs(pX1 - bX) >= Math.abs(pY1 - bY)) if (vX > 0) ball.setXVector(-vX);
        if (Math.abs(pX1 - bX) <= Math.abs(pY1 - bY)) if (vY > 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX2, pY1) <= bRad) { // top-right corner collision
        if (Math.abs(pX2 - bX) >= Math.abs(pY1 - bY)) if (vX < 0) ball.setXVector(-vX);
        if (Math.abs(pX2 - bX) <= Math.abs(pY1 - bY)) if (vY > 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX2, pY2) <= bRad
          && ball.distance(pX2, pY2) <= ball.distance(pX1, pY2)) { // bottom-right corner collision
        if (Math.abs(pX2 - bX) >= Math.abs(pY2 - bY)) if (vX < 0) ball.setXVector(-vX);
        if (Math.abs(pX2 - bX) <= Math.abs(pY2 - bY)) if (vY < 0) ball.setYVector(-vY);
        return true;
      } else if (ball.distance(pX1, pY2) <= bRad) { // bottom-left corner collision
        if (Math.abs(pX1 - bX) >= Math.abs(pY2 - bY)) if (vX > 0) ball.setXVector(-vX);
        if (Math.abs(pX1 - bX) <= Math.abs(pY2 - bY)) if (vY < 0) ball.setYVector(-vY);
        return true;
      }
    }
    return false;
  }