【发布时间】:2021-05-03 19:51:32
【问题描述】:
明天我在碰撞方面寻求帮助。我有很好的建议,碰撞检测有效,但是在什么物体碰撞之后,他真的飞到了屏幕外的任何地方。我想这是因为我没有打电话给@ Action 中的 987654325@ 方法在动画中执行,但是我为碰撞绘制的矩形与 gif 分割为 Jlabel 我设置为 Icon.T My question about collision
Pac-Man before collision after collision,Pac-Man just fly away
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Hra extends JFrame implements ActionListener {
static ArrayList<Body> bodiky = new ArrayList<Body>(50);
Rectangle platforma;
Rectangle packRect;
Rectangle ghostRect;
Timer timer;
private JPanel contentPane;
static Body body ;
static JLabel gifLabel = new JLabel(new ImageIcon("C:\\Users\\petrb\\Downloads\\packmanGifRes.gif"));
JLabel lblNewLabel;
private Color bodyCol = Color.white;
private Color packCol = Color.YELLOW;
static PackMan packman;
private int xRychlost =1;
private int yRychlost = 1;
static int count = 0;
static Duch duch;
/**
* Launch the application.
*/
public static void main(String[] args) {
Hra frame = new Hra();
frame.setVisible(true);
frame.getContentPane().add(gifLabel);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
}
/**
* Create the frame.
*/
public Hra() {
packman = new PackMan(0, 900, 800, packCol);
packRect = new Rectangle(packman.getSouradniceX(), packman.getSouradniceY(),50, 70);
ghostRect = new Rectangle(800,800,50,70);
platforma = new Rectangle(0, 120, 50, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
getContentPane().setBackground(Color.gray);
timer = new Timer(0,this);
timer.start();
lblNewLabel = new JLabel("Score:");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(154)
.addComponent(gifLabel, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(227, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
.addComponent(gifLabel)
.addGap(73))
);
contentPane.setLayout(gl_contentPane);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
switch(e.getKeyCode()) {
case 37: //doleva
// packman.setSouradniceX(packman.getSouradniceX()-20);
// gifLabel.setLocation(gifLabel.getX()-20, gifLabel.getY());
repaint();
count = 1;
break;
case 38: //nahorů
// packman.setSouradniceY(packman.getSouradniceY()-20);
// gifLabel.setLocation(gifLabel.getX(), gifLabel.getY()-20);
repaint();
count = 2;
// zkontrolujKolizi();
break;
case 39://doprava
// packman.setSouradniceX(packman.getSouradniceX()+20);
// gifLabel.setLocation(gifLabel.getX()+20, gifLabel.getY());
repaint();
count = 3;
// zkontrolujKolizi();
break;
case 40://dolů
// packman.setSouradniceY(packman.getSouradniceY()+20);
// gifLabel.setLocation(gifLabel.getX(), gifLabel.getY()+20);
count =4;
// zkontrolujKolizi();
repaint();
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("hodnota:"+ e.getKeyCode());
System.out.println("Znak:" + e.getKeyChar());
}
});
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawRect(packRect.x, packRect.y, packRect.height, packRect.width);
g.setColor(bodyCol);
g.drawRect(ghostRect.x,ghostRect.y,ghostRect.width,ghostRect.height);
g.setColor(packCol);
g.fillRect(0, 120, 50, 800);
g.fillRect(40, 858, 200 ,62);
g.drawRect(platforma.x, platforma.y, platforma.width, platforma.height);
}
public boolean zkontrolujKolizi() {
return packRect.intersects(ghostRect) || packRect.intersects(platforma) ;
}
@Override
public void actionPerformed(ActionEvent e) {
int rollzpetX = packRect.x;
int rollzpetY = packRect.y;
switch (count) {
case 1:
packman.setSouradniceX(packman.getSouradniceX() - xRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.x = packRect.x - xRychlost;
// repaint();
break;
case 2:
packman.setSouradniceY(packman.getSouradniceY() - yRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.y = packRect.y - yRychlost;
break;
case 3:
packman.setSouradniceX(packman.getSouradniceX() + xRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.x = packRect.x + xRychlost;
break;
case 4:
packman.setSouradniceY(packman.getSouradniceY() + yRychlost);
gifLabel.setLocation(packman.getSouradniceX(), packman.getSouradniceY()-38);
packRect.y = packRect.y+yRychlost;
break;
}
if (zkontrolujKolizi()) {
packRect.x = rollzpetX;
packRect.y = rollzpetY;
}else {
repaint();
}
}
}
//吃豆人类
import java.awt.Color;
import java.util.ArrayList;
public class PackMan {
private double skore;
private int souradniceX;
private int souradniceY;
private Color color;
public double getSkore() {
return skore;
}
public void setSkore(double skore) {
this.skore = skore;
}
public int getSouradniceX() {
return souradniceX;
}
public void setSouradniceX(int souradniceX) {
this.souradniceX = souradniceX;
}
public int getSouradniceY() {
return souradniceY;
}
public void setSouradniceY(int souradniceY) {
this.souradniceY = souradniceY;
}
public PackMan(int skore, int souradniceX, int souradniceY,Color color) {
super();
this.skore = skore;
this.souradniceX = souradniceX;
this.souradniceY = souradniceY;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
//吃豆人类
import java.awt.Color;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Body {
private int bodyX;
private int bodyY;
private double hodnotaBodu;
private Color color;
public int getBodyX() {
return bodyX;
}
public Body(int bodyX, int bodyY, double hodnotaBodu,Color color) {
super();
this.bodyX = bodyX;
this.bodyY = bodyY;
this.hodnotaBodu = hodnotaBodu;
this.color = color;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void setBodyX(int bodyX) {
this.bodyX = bodyX;
}
public int getBodyY() {
return bodyY;
}
public void setBodyY(int bodyY) {
this.bodyY = bodyY;
}
public double getHodnotaBodu() {
return hodnotaBodu;
}
public void setHodnotaBodu(double hodnotaBodu) {
this.hodnotaBodu = hodnotaBodu;
}
}
【问题讨论】:
-
考虑提供minimal reproducible example 以吸引更好的回复
-
我已经粘贴了我知道有问题的部分。我已经在碰撞之前和之后设置了坐标。但是 pacman 仍然能够穿墙
-
“我已经粘贴了我知道有问题的部分。” 但是您还没有粘贴让我可以轻松编译和运行代码的部分,并且查看我可能尝试的任何更改的结果,以使其正常工作。嗯,NVM。对我来说,这纯粹是学术性的..
-
如果它是更大的项目怎么办?我已经阅读了最小的可重现示例。所以我希望我做对了。好吧,我会发布我所有的代码,但是代码不是英文有关系吗?
-
"如果它是更大的项目怎么办?" 不要将更大的项目和手头的问题混淆,即"物体在碰撞后飞走" 。我们不需要也不想要“项目”。我们确实鼓励人们发布我们在屏幕上实际看到问题所需的最小/短代码(包括导入、类结构、
main方法等)。就我而言,我通常无法仔细查看代码,直到它在我的 IDE 中编译并格式化,因为我习惯于看到格式化的代码。 提示: 添加@MadProgrammer(或@很重要的任何人)以通知此人有新评论。
标签: java swing 2d collision-detection collision