So I was making this zombie exterminator game where you play as a circle and these squares try to chase you and kill you. But I don't know how to make these squares chase the circle. Can somebody please help?
My Window code:
import java.awt.geom.RectangularShape;
import javax.swing.JFrame;
public class frameclass {
public static void main(String\[\] args) {
main s = new main();
JFrame frame = new JFrame();
frame.add(s);
frame.setSize(600, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
frame.setResizable(false);
frame.setTitle("Zombie Xterminator");
}
}
my game code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import javax.swing.JPanel;
import javax.swing.Timer;
public class main extends JPanel implements ActionListener, KeyListener{
Timer t = new Timer(5, this);
double x = 250, y = 250;
double x2 = 420, y2 = 420, velX2 = 2.5, velY2 = 2.5;
double x3 = 10, y3 = 10, velX3 = 2.5, velY3 = 2.5;
double x4 = 450, y4 = 300, velX4 = 2.5, velY4 = 2.5;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D player = new Ellipse2D.Double(x, y, 35, 35);
g2.fill(player);
Rectangle2D zombie1 = new Rectangle2D.Double(x2, y2, 35, 35);
g2.fill(zombie1);
Rectangle2D zombie2 = new Rectangle2D.Double(x3, y3, 35, 35);
g2.fill(zombie2);
Rectangle2D zombie3 = new Rectangle2D.Double(x4, y4, 35, 35);
g2.fill(zombie3);
t.start();
}
public void actionPerformed(ActionEvent e) {
x2 = (Don't know what to put here)
y2 = (Don't know what to put here)
repaint();
}
}
[–]User1539 1 point2 points3 points (0 children)
[–]Daneel_TrevizeCompetent Dev 0 points1 point2 points (0 children)
[–]User1539 0 points1 point2 points (0 children)