Java while循环条件问题

时间:2014-04-19 12:28:10

标签: java loops while-loop

这个简单的代码应该是尝试制作游戏。目前有可移动的绿色矩形(玩家)和两个带有半径的黑色矩形(敌人)。如果玩家进入raidus,敌人开始改变它的位置。我想实现敌人。移动到玩家的x和y坐标,但每次敌人击中玩家的x或y轴坐标(只有其中一个,而不是两个)它停止。我希望它也完成第二轴移动。为什么会发生?我想问题可能在run{}方法中,考虑到移动条件的位置。

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class Drawing extends JPanel implements KeyListener,Runnable
{
public static JFrame frame;
public static JLabel label;
public static int[] x=new int[10];
public static int[] y=new int[10];
public static int a;
public static Random random;
public static int p_x=0;
public static int p_y=0;
public static int enemy_stop=0;
public static boolean isin=false;
public static Thread move;
public static Rectangle active;
public static Rectangle[] generated=new Rectangle[10];;

public static void drawframe(int width,int height)
{       
    frame=new JFrame("The game");
    frame.setLayout(new BorderLayout());
    frame.setSize(width,height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
}

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

    drawenemies(g,2);
    setradius(g);
    setplayer(g);

        if(entered()&&isin!=true)
        {
            isin=true;
            move=new Thread(new Drawing());
            move.start();
        }

        if(entered()==false&&isin!=false)
        {
            isin=false;
        }
}

public void drawenemies(Graphics g,int amount)
{
    random=new Random();
    a=amount;
    enemy_stop=enemy_stop+1;

    if(enemy_stop<=1)
    {
        for(int i=1;i<=amount;i++)
        {
            x[i]=random.nextInt(frame.getWidth());
            y[i]=random.nextInt(frame.getHeight());
        }
    }   

    for(int i=1;i<=amount;i++)
    {   
        g.fillRect(x[i], y[i], 20, 20);
        Rectangle store=new Rectangle(x[i], y[i], 20, 20);
        generated[i]=store;
    }
}

public void setradius(Graphics g)
{
    g.setColor(Color.RED);

    for(int i=1;i<=a;i++)
    {
        g.drawRect(x[i]-40, y[i]-40, 100, 100);
    }
}

public void setplayer(Graphics g)
{
    g.setColor(Color.GREEN);
    g.fillRect(p_x, p_y, 20, 20);
    g.setColor(Color.BLACK);
    g.drawRect(p_x, p_y, 20, 20);
}

public static void main(String args[])
{   
    drawframe(500,500);
    Drawing instance=new Drawing();
    frame.add(instance);
    frame.addKeyListener(instance);

}

public boolean entered()
{
    boolean entered=false;

    for(int i=1;i<=a;i++)
    {
        Rectangle q=new Rectangle(x[i]-40, y[i]-40, 100, 100);
        Rectangle s=new Rectangle(p_x, p_y, 20, 20);

        if(q.contains(s))
        {
            entered=true;
            active=new Rectangle(x[i], y[i], 20, 20);
        }
    }

    return entered;
}

@Override
public void keyPressed(KeyEvent e) 
{
    if(e.getKeyCode()==39)
    {
        p_x=p_x+10;
        repaint();
    }
    if(e.getKeyCode()==37)
    {
        p_x=p_x-10;
        repaint();
    }
    if(e.getKeyCode()==38)
    {
        p_y=p_y-10;
        repaint();
    }
    if(e.getKeyCode()==40)
    {
        p_y=p_y+10;
        repaint();
    }
}

public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0){}

@Override
public void run() 
{
    try
    {

    for(int i=1;i<=a;i++)
    {
        if(active.getX()==generated[i].getX())
        {
            while(x[i]!=p_x&&y[i]!=p_y)
            {
                if(x[i]>p_x)
                    x[i]=x[i]-1;
                else
                    x[i]=x[i]+1;

                if(y[i]>p_y)
                    y[i]=y[i]-1;
                else
                    y[i]=y[i]+1;

                Thread.sleep(1500);
                frame.repaint();
            }
        }
    }
}
catch(InterruptedException e)
{
}
   }
 }

1 个答案:

答案 0 :(得分:3)

我想while(x[i]!=p_x&&y[i]!=p_y)应该是||而不是&&

同样在runenteredsetradiusdrawenemies循环方法应该是: for(int i=0; i < a; i++) 因为Java中的数组是从0n-1的数字,其中n是数组的大小。您的代码将生成ArrayIndexOutOfBoundsException

keyPressed方法中,您可以用一个switch语句替换这四个if

您还可以考虑将您的名称更改为camelCase