示例#1
0
  /** Paints the game! */
  @Override
  public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mInitialized == false) {
      return;
    }

    Context context = getContext();

    // Draw the paddles / touch boundaries
    mRed.draw(canvas);
    mBlue.draw(canvas);
    mSnowy.draw(canvas);

    mGreen.draw(canvas);
    mYellow.draw(canvas);
    mOrange.draw(canvas);
    mCyan.draw(canvas);

    // Draw touchboxes if needed
    if (gameRunning() && mRed.player && mCurrentState == State.Running) mRed.drawTouchbox(canvas);

    if (gameRunning() && mBlue.player && mCurrentState == State.Running) mBlue.drawTouchbox(canvas);

    // Draw ball stuff
    mPaint.setStyle(Style.FILL);
    mPaint.setColor(Color.YELLOW);
    mBall.draw(canvas);

    // If either is a not a player, blink and let them know they can join in!
    // This blinks with the ball.
    if (mBall.serving()) {
      String join = context.getString(R.string.join_in);
      int joinw = (int) mPaint.measureText(join);

      if (!mRed.player) {
        mPaint.setColor(Color.MAGENTA);
        canvas.drawText(join, getWidth() / 2 - joinw / 2, mRed.touchCenterY(), mPaint);
      }

      if (!mBlue.player) {
        mPaint.setColor(Color.CYAN);
        canvas.drawText(join, getWidth() / 2 - joinw / 2, mBlue.touchCenterY(), mPaint);
      }
    }

    // Show where the player can touch to pause the game
    if (mBall.serving()) {
      String pause = context.getString(R.string.pause);
      int pausew = (int) mPaint.measureText(pause);

      mPaint.setColor(Color.GREEN);
      mPaint.setStyle(Style.STROKE);
      mPaint.setTextSize(36);
      canvas.drawRect(mPauseTouchBox, mPaint);
      canvas.drawText(pause, getWidth() / 2 - pausew / 2, getHeight() / 2, mPaint);
    }

    // Paint a PAUSED message
    if (gameRunning() && mCurrentState == State.Stopped) {
      String s = context.getString(R.string.paused);
      int width = (int) mPaint.measureText(s);
      int height = (int) (mPaint.ascent() + mPaint.descent());

      mPaint.setColor(Color.WHITE);
      canvas.drawText(s, getWidth() / 2 - width / 2, getHeight() / 3 - height / 2, mPaint);
    }

    // Draw a 'lives' counter
    mPaint.setColor(Color.YELLOW);
    mPaint.setStyle(Style.FILL_AND_STROKE);
    for (int i = 0; i < mRed.getLives(); i++) {
      canvas.drawCircle(
          Ball.RADIUS + PADDING + i * (2 * Ball.RADIUS + PADDING),
          PADDING + Ball.RADIUS,
          Ball.RADIUS,
          mPaint);
    }

    for (int i = 0; i < mBlue.getLives(); i++) {
      canvas.drawCircle(
          Ball.RADIUS + PADDING + i * (2 * Ball.RADIUS + PADDING),
          getHeight() - PADDING - Ball.RADIUS,
          Ball.RADIUS,
          mPaint);
    }

    // Announce the winner!
    if (!gameRunning()) {
      mPaint.setColor(Color.MAGENTA);
      String s = "You both lose";

      if (!mBlue.living()) {
        s = context.getString(R.string.red_wins);
        mPaint.setColor(Color.MAGENTA);
      } else if (!mRed.living()) {
        s = context.getString(R.string.blue_wins);
        mPaint.setColor(Color.CYAN);
      }

      int width = (int) mPaint.measureText(s);
      int height = (int) (mPaint.ascent() + mPaint.descent());
      canvas.drawText(s, getWidth() / 2 - width / 2, getHeight() / 2 - height / 2, mPaint);
    }
  }