Опитвам се да завъртя hitbox, който открива сблъсък. Искам невъртящ се правоъгълник_1, движещ се отляво надясно, и въртящ се правоъгълник_2, движещ се отдясно наляво, и искам въртящият се хитбокс да открие сблъсъка, независимо от ориентацията му. Сблъсък обаче се открива само когато rectangle_1 се сблъска с незавъртяния rectangle_2. Как мога да накарам въртяща се кутия за удари да открива сблъсък? Ето кодовете:
Основната програма:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
public class Rectangles extends JPanel implements MouseListener{
public static final int SCREEN_WIDTH = 400;
public static final int SCREEN_HEIGHT = 400;
private static final int TIMER_RATE = 20;
public static void main(String[] args) throws Exception {
Rectangles game = new Rectangles();
game.setup();
}
public void setup() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(this));
f.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
f.setLocation(200, 200);
f.setVisible(true);
this.addMouseListener(this);
this.setFocusable(true);
}
public Rectangles() {
rectangle_1 = new Rectangle_1();
rectangle_2 = new Rectangle_2();
gameover = false;
play();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(Color.white);
draw((Graphics2D) g);
}
public void draw(Graphics2D g) {
if (gameover) {
rectangle_1.drawRectangle(g);
rectangle_2.drawRectangle(g);
return;
}
rectangle_1.drawRectangle(g);
rectangle_2.drawRectangle(g);
rectangle_1.move();
rectangle_2.move(1);
rectangle_2.rotateRectangle(g);
if (rectangle_2.collides(rectangle_1)) {
gameover = true;
}
}
public void play() {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
repaint();
}
};
Timer timer = new Timer(TIMER_RATE, listener);
timer.start();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
mouseAction(e.getX(), e.getY(), e.getButton());
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/**
* Use this method to handle all mouse input. The main action here is to have the bird flap whenever the mouse is clicked.
*/
public void mouseAction(int x, int y, int button) {
if (button == 1) {
rectangle_1.flap();
}
}
private Rectangle_1 rectangle_1;
private Rectangle_2 rectangle_2;
private boolean gameover;
private int ticks = 0;
}
Класът rectangle_1:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Rectangle_1 {
public Rectangle_1() {
x = 0;
y = 240;
rectangle = new Rectangle((int) x, (int) y, 25, 25);
}
public void drawRectangle(Graphics2D g) {
g.setColor(Color.blue);
g.draw(rectangle);
}
public void move() {
x = x + 1;
rectangle.setLocation((int) x, (int) y);
}
public void flap() {
z = 10;
rectangle.setLocation((int) x, (int) y);
}
public boolean collides(Rectangle r) {
return r.intersects(rectangle);
}
public Rectangle getRectangle() {
return rectangle;
}
private double x;
private double y;
private double z = 0;
private Rectangle rectangle;
}
Класът rectangle_2:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.Rectangle;
public class Rectangle_2 {
public Rectangle_2() {
x = 375;
y = 200;
hitBox_1 = new Rectangle((int) x + 25, (int) y, 25, 100);
}
public void drawRectangle(Graphics2D g) {
g.setColor(Color.red);
g.draw(hitBox_1);
}
public void rotateRectangle(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = g2.getTransform();
AffineTransform saveTransform = g2.getTransform();
angle += 0.01;
at.rotate(angle, hitBox_1.getX() + hitBox_1.getWidth() / 2, hitBox_1.getY() + hitBox_1.getHeight() / 2);
g2.setTransform(at);
g2.setColor(Color.red);
g2.draw(hitBox_1);
g2.setTransform(saveTransform);
}
public void move(double dx) {
x = x - dx;
hitBox_1.setLocation((int) x, (int) y);
}
public boolean collides(Rectangle_1 r) {
return r.collides(hitBox_1);
}
@Override
public String toString() {
return "Pipe [x = " + x + ", y = " + y + ", hitBox_1 = " + hitBox_1 + "]";
}
public Rectangle getHitBox_1() {
return hitBox_1;
}
public Rectangle getHitBox_2() {
return hitBox_1;
}
public double getX() {
return x;
}
private double x;
private double y;
private double angle = 0;
private Rectangle hitBox_1;
}