This simple code is supposed to be try to make a game.Currently there is moveable green rectangle (player) and two black rectangles (enemies) with radiuses.If player enters raidus, enemy starts changing it's location.I want to achieve enemy rect. moving to x and y coords of player, but each time enemy hits x or y axis coords of player(only one of them, not both) it stops.I want it to finish also second axis movement.Why does it happen?I suppose problem could be in run{} method, considering that's place where is moving condition.
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)
{
}
}
}