gpt4 book ai didi

Java Pong 游戏碰撞检测问题

转载 作者:行者123 更新时间:2023-12-01 09:34:58 24 4
gpt4 key购买 nike

我对 Java 还很陌生,需要一些帮助。我已经创建了 Pong 游戏(使用 Eclipse),并且在大多数情况下,它运行得很好。然而,碰撞检测有些不对劲。球从人类控制的 Racket 上反弹得很好,但总是直接穿过人工智能控制的 Racket ,我一辈子都无法弄清楚我的代码出了什么问题。代码如下:

主课“网球”:

package PongV2;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Tennis extends Applet implements Runnable, KeyListener{
final int WIDTH = 700, HEIGHT = 500;
Thread thread;
HumanPaddle p1;
AIPaddle p2;
Ball b1;
boolean gameStarted;
Graphics gfx;
Image img;


public void init(){
this.resize(WIDTH, HEIGHT);
gameStarted = false;
this.addKeyListener(this);
p1 = new HumanPaddle(1);
b1 = new Ball();
p2 = new AIPaddle(2, b1);
img = createImage(WIDTH, HEIGHT);
gfx = img.getGraphics();
thread = new Thread(this);
thread.start();
}

public void paint(Graphics g){
gfx.setColor(Color.black);
gfx.fillRect(0, 0, WIDTH, HEIGHT);
if(b1.getX() < -10 || b1.getX() > 710){
gfx.setColor(Color.red);
gfx.drawString("Game Over", 350, 250);
}
else{
p1.draw(gfx);
b1.draw(gfx);
p2.draw(gfx);
}

if(!gameStarted) {
gfx.setColor(Color.white);
gfx.drawString("Pong", 343, 100);
gfx.drawString("Press Enter to Begin...", 295, 130);
}
g.drawImage(img, 0, 0, this);

}


public void update(Graphics g){
paint(g);
}

public void run() {
for(;;){
if(gameStarted) {
p1.move();
p2.move();
b1.move();
b1.checkPaddleCollision(p1, p2);
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
p1.setUpAccel(true);
} else if(e.getKeyCode() == KeyEvent.VK_DOWN){
p1.setDownAccel(true);
} else if(e.getKeyCode() == KeyEvent.VK_ENTER){
gameStarted = true;

}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
p1.setUpAccel(false);
} else if(e.getKeyCode() == KeyEvent.VK_DOWN){
p1.setDownAccel(false);
}

}


public void keyTyped(KeyEvent arg0) {

}

}

如果有人需要的话,还可以看看“AIPaddle”类:

package PongV2;

import java.awt.Color;
import java.awt.Graphics;

public class AIPaddle implements Paddle{
double y, yVel;
boolean upAccel, downAccel;
final double GRAVITY = 0.94;
int player, x;
Ball b1;

public AIPaddle(int player, Ball b){
upAccel = false; downAccel = false;
b1 = b;
y = 210; yVel = 0;
if(player == 1)
x = 20;
else
x = 660;
}

public void draw(Graphics g) {
g.setColor(Color.white);
g.fillRect(x, (int)y, 20, 80);

}

public void move() {
y = b1.getY() -40;


if(y < 0)
y = 0;
if(y > 420)
y = 420;

}


public int getY() {
return (int)y;
}

}

“球”类:

package PongV2;

import java.awt.Color;
import java.awt.Graphics;

public class Ball {
double xVel, yVel, x, y;

public Ball(){
x = 350;
y = 250;
xVel = getRandomSpeed() * getRandomDirection();
yVel = getRandomSpeed() * getRandomDirection();
}

public double getRandomSpeed(){
return(Math.random() *3 + 2);
}

public int getRandomDirection(){
int rand = (int)(Math.random() * 2);
if(rand == 1)
return 1;
else
return -1;
}

public void draw(Graphics g){
g.setColor(Color.white);
g.fillOval((int)x-10, (int)y-10, 20, 20);
}

public void checkPaddleCollision(Paddle p1, Paddle p2){
if (x <= 50){
if(y >= p1.getY() && y <= p1.getY() + 80){
xVel = -xVel;
}
else if(x >= 650){
if(y >= p2.getY() && y <= p2.getY() + 80)
xVel = -xVel;
}
}
}

public void move(){
x += xVel;
y += yVel;

if(y < 10)
yVel = -yVel;
if(y > 490)
yVel = -yVel;
}

public int getX(){
return (int)x;
}

public int getY(){
return (int)y;
}

}

有人可以建议我的代码哪里需要更正吗?谢谢。

最佳答案

它是 checkPaddleCollisionMethod 中的 if 语句。你现在就有这个了。

if(...) {
if(...) {
}
else if(...) {
if(...) {
}
}
}

else if 与第二个 if 并行,而不是与第一个 if 并行。你想要一个更像这样的结构。

if(...) {
if(...) {
}
}
else if(...) {
if(...) {
}
}

关于Java Pong 游戏碰撞检测问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39070171/

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