好的,我剛寫完我的乒乓球遊戲,但現在我想要一個AI讓玩家對抗。 我試過這樣做,當NPC的位置超過Y的球位時,它會移動到正確的位置試圖阻擋球。 這裏是我的帕德爾類代碼:Ai /簡單的乒乓球遊戲的電腦播放器
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Padel implements KeyListener{
private Ball ball = new Ball();
private boolean LeftPlayerUp=false;
private boolean LeftPlayerDown=false;
private boolean RightPlayerUp=false;
private boolean RightPlayerDown=false;
public int RightLocation=200;
public int LeftLocation=200;
public void keyPressed(KeyEvent e){
int KeyPressed = e.getKeyCode();
if (KeyPressed== KeyEvent.VK_W){
LeftPlayerUp=true;
}
else if (KeyPressed==KeyEvent.VK_S){
LeftPlayerDown=true;
}
}
public void keyReleased(KeyEvent e){
int KeyReleased = e.getKeyCode();
if (KeyReleased== KeyEvent.VK_W){
LeftPlayerUp=false;
}
else if (KeyReleased==KeyEvent.VK_S){
LeftPlayerDown=false;
}
}
public void ShouldLeftMove(){
if(LeftPlayerUp==true){
if(LeftLocation==50){}
else{
LeftLocation-=2;
}
}
else if(LeftPlayerDown==true){
if(LeftLocation==Main.Height-190){}
else{
LeftLocation+=2;
}
}
}
public void ShouldRightMove(){
if(ball.BallLocationY>=RightLocation){
RightPlayerUp=false;
RightPlayerDown=true;
}else{
RightPlayerUp=true;
RightPlayerDown=false;
}
if(RightPlayerUp==true){
if(RightLocation==50){}
else{
RightLocation-=2;
}
}
else if(RightPlayerDown==true){
if(RightLocation==Main.Height-190){}
else{
RightLocation+=2;
}
}
}
//This just needs to be here :P
public void keyTyped(KeyEvent e) {}
}
這段代碼的帕德爾去約2 /三分之二倒在屏幕右側,然後就停止移動,怪人的了。 我在做什麼錯?
您確定您正確計算了槳的大小並正確地抵消了嗎? – yoozer8
我認爲尺寸我相信,除非我想它的方式工作不正確。沒有人會有答案嗎? – Poo2thegeek