我對Java相當陌生,可以使用一些幫助。我創建了Pong遊戲(使用Eclipse),並且大部分功能都很好。但是,碰撞檢測有些不正確。球從人控槳上反彈得很好,但總是直接通過AI控制的槳,我不能在我的生活中找出我的代碼中有什麼問題。下面的代碼:Java Pong遊戲碰撞檢測問題
的主類, 「網球」:
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;
}
}
有人請指教我的代碼需要糾正的地方嗎?謝謝。
有一些錯誤被拋出? –
不,沒有錯誤。它啓動,所有的東西,球只是通過AI槳 - 沒有碰撞檢測。但它從人控槳反彈很好。找不到代碼中的錯誤。 – codeslayer93
然後嘗試使用調試器。 –