在java中按向左和箭头键时,形状不在正确位置

在java中按向左和箭头键时,形状不在正确位置,java,android,swing,netbeans,awt,Java,Android,Swing,Netbeans,Awt,我正在玩这个吃豆人游戏。它已经开始工作了,但是我对其他形状特别是吃豆人的眼睛有问题。问题是,当我按下向左和向上箭头键时,眼睛的位置得到了错误的位置。见附件图片 有人能帮我吗。这是我的密码 public void paintComponent(Graphics graphics) { super.paintComponent(graphics); //pacman graphics.setColor(Color.yellow);

我正在玩这个吃豆人游戏。它已经开始工作了,但是我对其他形状特别是吃豆人的眼睛有问题。问题是,当我按下向左和向上箭头键时,眼睛的位置得到了错误的位置。见附件图片

有人能帮我吗。这是我的密码

 public void paintComponent(Graphics graphics)
    {
        super.paintComponent(graphics);

        //pacman
        graphics.setColor(Color.yellow);
        graphics.fillArc(xLocation, yLocation, 100, 100, angle, mouth);

        //eyes
        graphics.setColor(Color.BLACK);
        graphics.fillOval(xLocationEyes, yLocationEyes, 20, 20);
        food(graphics);
    }
 @Override
 public void keyPressed(KeyEvent keyboard) {


int keyboardPress = keyboard.getKeyCode();
         if(keyboardPress == KeyEvent.VK_RIGHT){
             if(xLocation + 130 >= getWidth()){
                 xLocationEyes = getWidth()-130;
                 xLocation = 600-160;
             }

             xLocation += 30;
             xLocationEyes += 30;
             angle = 45;
             repaint();
     }
 if(keyboardPress == KeyEvent.VK_LEFT){

         if(xLocation  <= 0){
             xLocationEyes = 45;
             xLocation = 30;

         }
         angle = -145;
         xLocation -= 30;            
         xLocationEyes -= 30;
         repaint();

     }
}
public void paintComponent(图形)
{
super.paintComponent(图形);
//吃豆人
图形.设置颜色(颜色.黄色);
图形.填充弧(X位置,Y位置,100,100,角度,口);
//眼睛
图形.设置颜色(颜色.黑色);
图形.圆角(xlocationyes,ylocationyes,20,20);
食物(图片);;
}
@凌驾
按下公共无效键(KeyEvent键盘){
int keyboardPress=keyboard.getKeyCode();
如果(键盘按下==KeyEvent.VK_RIGHT){
如果(xLocation+130>=getWidth()){
xlocationyes=getWidth()-130;
xLocation=600-160;
}
xLocation+=30;
xlocationyes+=30;
角度=45;
重新油漆();
}
如果(键盘按下==KeyEvent.VK_左){

if(xLocationPac Man,我最喜欢的街机游戏之一,脸不是对称的。当你在水平轴上翻转身体时,你也需要翻转他的眼睛。这主要是对你的数字进行一些修补,但是当你调用下面的代码行时

if(keyboardPress == KeyEvent.VK_LEFT){

      //i dont know what this does so tread at your own will
     if(xLocation  <= 0){
         xLocationEyes = 45;
         xLocation = 30;

     }

     angle = -145;
     xLocation -= 30;   
     //tinker with this value and the value of you moving the eye back when moving right         
     xLocationEyes += 30; //note the += this may fix your issue
     repaint();

 }
if(keyboardPress==KeyEvent.VK_左){
//我不知道这有什么用,所以你可以随心所欲

如果(xLocation)只需修改眼睛的x值,你最终会得到它。是的,它在第一次按下时起作用。但由于它+它将向右移动,因此眼睛将向右移动。这里的任何其他解决方案都只需根据direction@MadProgrammer我试过了,但是两种形状(吃豆人和他的眼睛)没有同步。@MadProgrammer您能在这里提供一些示例吗?非常感谢。为了更快地获得更好的帮助,请发布一个or。@MadProgrammer您对此有什么建议吗?