gpt4 book ai didi

java - 为什么球的大小不会增加?(游戏)

转载 作者:行者123 更新时间:2023-12-03 21:22:31 28 4
gpt4 key购买 nike

我正在尝试添加一个乒乓游戏框架。我希望每次球与 Racket 接触时球的大小都会增加。

这是我的尝试。第一 block 代码是我认为问题所在的地方。第二 block 是全类。

public class PlayState extends State {
private Paddle paddleRight, paddleLeft;
private static final int PADDLE_WIDTH = 30;
private static final int PADDLE_HEIGHT = 60;

private Ball ball;

//bDiam stands for ball diameter

private static int bDiam = 10;

private int playerScore = 0;
private Font scoreFont;

@Override
public void init() {
paddleLeft = new Paddle(0, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
paddleRight = new Paddle(785, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
scoreFont = new Font("SansSerif", Font.BOLD, 25);
ball = new Ball(300, 200, bDiam, bDiam);
}

@Override
public void onClick(MouseEvent e) {
// playerScore = 5;


}

@Override
public void update() {
paddleLeft.update();
paddleRight.update();
ball.update();

if (ballCollides(paddleLeft)) {
playerScore++;

bDiam+= 20;
ball.onCollideWith(paddleLeft);
Resources.hit.play();
} else
if (ballCollides(paddleRight)) {
playerScore++;
bDiam= bDiam + 20;
ball.onCollideWith(paddleRight);
Resources.hit.play();
} else if (ball.isDead()) {
playerScore -= 3;

ball.reset();

}
}

完整的类

public class PlayState extends State {
private Paddle paddleRight, paddleLeft;
private static final int PADDLE_WIDTH = 30;
private static final int PADDLE_HEIGHT = 60;

private Ball ball;

//bDiam stands for ball diameter

private static int bDiam = 10;

private int playerScore = 0;
private Font scoreFont;

@Override
public void init() {
paddleLeft = new Paddle(0, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
paddleRight = new Paddle(785, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
scoreFont = new Font("SansSerif", Font.BOLD, 25);
ball = new Ball(300, 200, bDiam, bDiam);
}

@Override
public void onClick(MouseEvent e) {
// playerScore = 5;


}

@Override
public void update() {
paddleLeft.update();
paddleRight.update();
ball.update();

if (ballCollides(paddleLeft)) {
playerScore++;

bDiam+= 20;
ball.onCollideWith(paddleLeft);
Resources.hit.play();
} else
if (ballCollides(paddleRight)) {
playerScore++;
bDiam= bDiam + 20;
ball.onCollideWith(paddleRight);
Resources.hit.play();
} else if (ball.isDead()) {
playerScore -= 3;

ball.reset();

}
}

@Override
public void render(Graphics g) {
// Draw Background
g.setColor(Resources.darkBlue);
g.fillRect(0, 0, GameMain.GAME_WIDTH, GameMain.GAME_HEIGHT);
g.setColor(Resources.darkRed);
g.fillRect(GameMain.GAME_WIDTH / 2, 0, GameMain.GAME_WIDTH / 2,
GameMain.GAME_HEIGHT);
// Draw Separator Line
g.drawImage(Resources.line, (GameMain.GAME_WIDTH / 2) - 2, 0, null);

// Draw Paddles
g.setColor(Color.white);
g.fillRect(paddleLeft.getX(), paddleLeft.getY(), paddleLeft.getWidth(),
paddleLeft.getHeight());
g.fillRect(paddleRight.getX(), paddleRight.getY(),
paddleRight.getWidth(), paddleRight.getHeight());

// Draw Ball
g.drawRect(ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

// Draw UI
g.setFont(scoreFont); // Sets scoreFont as current font
g.drawString("" + playerScore, 350, 40); // Draws String using current
// font
}



@Override
public void onKeyPress(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
paddleLeft.accelUp();
paddleRight.accelUp();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
paddleLeft.accelDown();
paddleRight.accelDown();
}
}

@Override
public void onKeyRelease(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP
|| e.getKeyCode() == KeyEvent.VK_DOWN) {
paddleLeft.stop();
paddleRight.stop();
}
}

private boolean ballCollides(Paddle p) {
return ball.getRect().intersects(p.getRect());
}
}

最佳答案

您的直径变量正在按值传递到您的 Ball 构造函数中。这意味着在您的主程序中更改它的值不会改变它。

你需要类似的东西

ball.setDiameter(ball.getDiameter() +20)

关于java - 为什么球的大小不会增加?(游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30971054/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com