gpt4 book ai didi

java - 如何在碰撞后移除矩形?

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:37 26 4
gpt4 key购买 nike

我有两个矩形,当它们相交时需要将其移出屏幕。我需要消失的矩形是bulletObject 和e1。当我运行它时它们确实相交但没有任何反应。我尝试过输入“e1 = new Rectangle (0,0,0,0);”在“if (bulletObject.intersects(e1)){”之后,但它告诉我它从未被使用过。我很感激所有的帮助。我的一部分代码如下。

 public void draw(Graphics g){

g.setColor(Color.BLUE);
g.fillRect(x, y, 40, 10);
g.fillRect(x+18, y-7, 4, 7);
Rectangle bulletObject = new Rectangle(x+18, y-7, 4, 7);
if (shot){
g.setColor(Color.BLUE);
g.fillRect(bullet.x, bullet.y , bullet.width, bullet.height);
}
//enemies

g.setColor(Color.RED);
Rectangle e1 = new Rectangle(20,75,35,35);
Rectangle e2 = new Rectangle(85,75,35,35);
Rectangle e3 = new Rectangle(150,75,35,35);
Rectangle e4 = new Rectangle(205,75,35,35);
Rectangle e5 = new Rectangle(270,75,35,35);
Rectangle e6 = new Rectangle(335,75,35,35);
Rectangle e7 = new Rectangle(405,75,35,35);
g.setColor(Color.RED);
g.fillRect(e1.x,e1.y,e1.width,e1.height);
g.fillRect(e2.x,e2.y,e2.width,e2.height);
g.fillRect(e3.x,e3.y,e3.width,e3.height);
g.fillRect(e4.x,e4.y,e4.width,e4.height);
g.fillRect(e5.x,e5.y,e5.width,e5.height);
g.fillRect(e6.x,e6.y,e6.width,e6.height);
g.fillRect(e7.x,e7.y,e7.width,e7.height);
g.fillRect(bulletObject.x,bulletObject.y,
bulletObject.width,bulletObject.height);

if (bulletObject.intersects(e1)){
g.clearRect(e1.x, e1.y,e1.width, e1.height );

}

}

最佳答案

让我们从...开始

您的绘制例程不是做出有关游戏状态决策的适当位置,它应该简单地负责绘制当前状态。

您需要维护一个可渲染元素的列表,您可以根据自己的需要和要求对其进行操作。

首先查看 Collections

您还可以阅读 Performing Custom LaintingPainting in AWT and Swing有用

以下示例演示了一系列动画、随机矩形的基本概念,这些矩形在被火球击中时将被删除,您可以通过按空格键触发火球

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Bullet {

public static void main(String[] args) {
new Bullet();
}

public Bullet() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private List<Rectangle> ships;
private Map<Rectangle, Integer> delats;

private Ellipse2D fireBall;

public TestPane() {
delats = new HashMap<>(25);
ships = new ArrayList<>(25);
Random rnd = new Random();
while (ships.size() < 12) {

boolean intersects = true;
Rectangle rect = null;
while (intersects) {
intersects = false;
int x = (int) (Math.random() * 400);
int y = (int) (Math.random() * 400);
int width = (int) (Math.random() * 50) + 25;
int height = (int) (Math.random() * 50) + 25;

if (x + width >= 400) {
x = 400 - width;
} else if (y + height >= 400) {
y = 400 - height;
}

rect = new Rectangle(x, y, width, height);
for (Rectangle other : ships) {
if (other.intersects(rect)) {
intersects = true;
break;
}
}
}
ships.add(rect);
delats.put(rect, (rnd.nextBoolean() ? 1 : -1));
}

Timer timer;
timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

if (fireBall != null) {

Rectangle bounds = fireBall.getBounds();

bounds.x += 5;
if (bounds.x >= getWidth()) {
fireBall = null;
} else {
fireBall.setFrame(bounds);
}

}
Iterator<Rectangle> it = ships.iterator();
while (it.hasNext()) {
Rectangle rct = it.next();
int delta = delats.get(rct);
rct.y += delta;
if (rct.y + rct.height >= getHeight()) {
rct.y = getHeight() - rct.height;
delta *= -1;
} else if (rct.y <= 0) {
rct.y = 0;
delta *= -1;
}

delats.put(rct, delta);

if (fireBall != null) {

if (fireBall.intersects(rct)) {
it.remove();
delats.remove(rct);
}

}

}

repaint();
}
});
timer.start();

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "fire");
ActionMap am = getActionMap();
am.put("fire", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
fireBall = new Ellipse2D.Float(0, (getHeight() / 2) - 5, 10, 10);
}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

if (fireBall != null) {

g2d.setColor(Color.RED);
g2d.fill(fireBall);

}

g2d.setColor(Color.BLACK);
for (Rectangle rct : ships) {
g2d.draw(rct);
}
g2d.dispose();
}

}

}

关于java - 如何在碰撞后移除矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20961949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com