Example #1
0
 @Override
 public void draw(Canvas c) {
   inner_.dx = this.dx;
   inner_.dy = this.dy;
   inner_.x = this.x;
   inner_.y = this.y;
   super.draw(c);
   inner_.draw(c);
 }
  // drawメソッド(壁紙描画処理)
  public void draw(Canvas canvas) {
    // 画面をクリア
    canvas.drawColor(Color.BLACK);
    canvas.save();

    ball.setBounds(
        (int) ball.ballX,
        (int) ball.ballY,
        (int) ball.ballX + ball.ballDiameter,
        (int) ball.ballY + ball.ballDiameter);
    ball2.setBounds(
        (int) ball2.ballX,
        (int) ball2.ballY,
        (int) ball2.ballX + ball2.ballDiameter,
        (int) ball2.ballY + ball2.ballDiameter);
    ball3.setBounds(
        (int) ball3.ballX,
        (int) ball3.ballY,
        (int) ball3.ballX + ball3.ballDiameter,
        (int) ball3.ballY + ball3.ballDiameter);
    ball.draw(canvas);
    ball2.draw(canvas);
    ball3.draw(canvas);

    canvas.restore();

    // 画面外へ玉が出た場合
    if (ball.width < ball.ballX
        || ball.height < ball.ballY
        || ball.ballX + ball.ballDiameter <= 0
        || ball.ballY + ball.ballDiameter <= 0) {

      // 玉の表示位置を再設定
      ball.resetPosition();
    }

    if (ball2.width < ball2.ballX
        || ball2.height < ball2.ballY
        || ball2.ballX + ball2.ballDiameter <= 0
        || ball2.ballY + ball2.ballDiameter <= 0) {

      ball2.resetPosition();
    }

    if (ball3.width < ball3.ballX
        || ball3.height < ball3.ballY
        || ball3.ballX + ball3.ballDiameter <= 0
        || ball3.ballY + ball3.ballDiameter <= 0) {

      ball3.resetPosition();
    }
  }
Example #3
0
  public void paint(Graphics g) {
    // set up the double buffering to make the game animation nice and smooth
    Graphics2D twoDGraph = (Graphics2D) g;

    // take a snap shop of the current screen and same it as an image
    // that is the exact same width and height as the current screen
    if (back == null) back = (BufferedImage) (createImage(getWidth(), getHeight()));

    // create a graphics reference to the back ground image
    // we will draw all changes on the background image
    Graphics graphToBack = back.createGraphics();

    if (lives < 1) {
      ball.setXSpeed(0);
      ball.setYSpeed(0);
      paddle.setSpeed(0);
      graphToBack.drawString("GAME OVER", 387, 30);
    } else if (bricks.isEmpty()) {
      ball.setXSpeed(0);
      ball.setYSpeed(0);
      paddle.setSpeed(0);
      graphToBack.drawString("YOU WIN", 387, 30);
    }

    ball.moveAndDraw(graphToBack);
    paddle.draw(graphToBack);
    for (Block x : bricks) {
      x.draw(graphToBack);
    }
    graphToBack.setColor(Color.white);
    graphToBack.drawString(score + ":" + lives, 387, 15);

    if (ball.didCollide(left) || ball.didCollide(right)) {
      ball.setXSpeed(-ball.getXSpeed());
    }

    if (ball.didCollide(top)) {
      ball.setYSpeed(-ball.getYSpeed());
    }

    if (ball.didCollide(bottom)) {
      ball.draw(graphToBack, Color.white);
      ball.setXSpeed(2);
      ball.setYSpeed(2);
      ball.setX(400);
      ball.setY(500);
      lives--;
      ball.setYSpeed(-ball.getYSpeed());
    }

    // see if the ball hits the paddle
    if (ball.didCollide(paddle)) ball.setYSpeed(-ball.getYSpeed());

    // brick collision
    for (Block x : bricks) {
      if (x.didCollide(ball)) {
        score++;
        x.draw(graphToBack, Color.white);
        bricks.remove(x);
        if (ball.didCollideTop(x) || ball.didCollideBottom(x)) ball.setYSpeed(-ball.getYSpeed());
        else ball.setXSpeed(-ball.getXSpeed());
      }
    }
    // see if the paddles need to be moved
    graphToBack.setColor(Color.black);
    graphToBack.drawString(score + ":" + lives, 387, 15);

    if (keys[0]) paddle.moveLeftAndDraw(graphToBack);
    if (keys[1]) paddle.moveRightAndDraw(graphToBack);

    twoDGraph.drawImage(back, null, 0, 0);
  }
 private void render() {
   glClear(GL_COLOR_BUFFER_BIT);
   ball.draw();
   bat.draw();
 }
Example #5
0
 @Override
 public void draw(Canvas canvas) {
   paint.setAlpha(ballOver ? 50 : 255);
   super.draw(canvas);
 }
Example #6
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);
    }
  }