好的,我刚写完我的乒乓球游戏,但现在我想要一个 AI 供玩家对抗。我试过这样做,当 NPC padel 的位置超过球的 Y 坐标时,它会移动到正确的区域以尝试阻挡球。这是我的 padel 类的代码:
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) {}
}
使用此代码,padel 将移动到右侧屏幕下方约 2/3 处,然后停止移动,spaz 消失。我究竟做错了什么???