如何确保玩家不会穿墙

时间:2021-05-03 08:07:01

标签: java swing collision-detection collision windowbuilder

我正在编写我的第一个真正的 2D 游戏(吃豆人)。我认为游戏看起来不错,但我有一个大问题 - 碰撞。对象仍在穿墙。我被它困住了,所以我决定寻求经验丰富的真正程序员的帮助。 (当然我做了一些研究,但我不想做COPY和Paste之类的事情,因为我不明白)。正如我所说,游戏快完成了,我需要做的就是阻止吃豆子通过。例如,我将大的白色矩形绘制为平台。我希望有人能够帮助我。在这个项目中,我学到了很多东西,碰撞是我理解的东西,但不知道如何正确编程。我想我已经快要弄清楚了,但缺少一些东西。

PS:我已经在 WindowBuilder 中创建了窗口,所以编译可能是一个问题:(

import java.awt.Color;
import java.awt.Graphics;
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 javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class Main extends JFrame implements ActionListener{
    JPanel contentPane;
    Rectangle packman ;
    Rectangle platform;
    Rectangle secondPlat;

    private int count = 0;
    private int x = 170, y = 50;
    private int xVel = 1, yVel = 1;
    Timer timer;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Main frame = new Main();
        frame.setVisible(true);
    }

    public Main() {
        // TODO Auto-generated constructor stub
        this.setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        getContentPane().setBackground(Color.gray);
        packman = new Rectangle(x,y,50,50);
        platform = new Rectangle(100,70,50,100);
        secondPlat = new Rectangle(220,50,50,100);
        timer = new Timer(0,this);
        timer.start();
        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
                    count = 1;  
                    repaint();
                    break;
                case 38: //nahorů       
                    count = 2;  
                    repaint();
                    break;
                case 39://doprava           
                    count = 3;          
                    repaint();
                    break;
                case 40://dolů      
                    count =4;   
                    repaint();
                    break;
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Char" + e.getKeyCode());
                System.out.println("Hod" + e.getKeyCode());
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        g.drawRect(x,y,packman.width,packman.height);
        g.setColor(Color.blue);
        g.fillRect(x,y,packman.width,packman.height);

        g.drawRect(platform.x,platform.y,platform.width,platform.height);
        g.setColor(Color.blue);
            
        g.drawRect(secondPlat.x,secondPlat.y,secondPlat.width,secondPlat.height);
        g.setColor(Color.blue);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (count == 1) {
            x = x - xVel;
            repaint();
            zkontrolujKolizi();
        }
        
        if (count ==2) {
            y = y - yVel;
            repaint();
            zkontrolujKolizi();
        }
        if (count ==3) {
            x = x + xVel;
            repaint();
            zkontrolujKolizi();
        }
        if (count ==4) {
            y  = y+yVel; 
            repaint();
            zkontrolujKolizi();
        }
    }

    public void zkontrolujKolizi() {
        // TODO Auto-generated method stub
        if (packman.intersects(platform) || packman.intersects(secondPlat)) {
            System.out.println("Got ya!");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在您的代码中,您更新了 x 和 y,但这不是在“packman”对象中完成的,该对象始终位于其初始位置;所以当你检查与墙的交叉点时,packman 总是在 (170,50);我更改了动画方法以反映 packman 中的更改和绘制方法,以便您使用更新的 packman 坐标。

动画:

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if (count == 1) {
        packman.x = packman.x - xVel;
        repaint();
        zkontrolujKolizi();
        
    
    }
    
    if (count ==2) {
        packman.y = packman.y - yVel;
        repaint();
        zkontrolujKolizi();
    }
    if (count ==3) {
        packman.x = packman.x + xVel;
        repaint();
        zkontrolujKolizi();
    }
    if (count ==4) {
        packman.y  = packman.y+yVel; 
        repaint();
        zkontrolujKolizi();
    }
    
    
}

油漆:

@Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        g.drawRect(packman.x,packman.y,packman.width,packman.height);
        g.setColor(Color.blue);
        g.fillRect(packman.x,packman.y,packman.width,packman.height);
        
        g.drawRect(platform.x,platform.y,platform.width,platform.height);
        g.setColor(Color.blue);
        
        g.drawRect(secondPlat.x,secondPlat.y,secondPlat.width,secondPlat.height);
        g.setColor(Color.blue);
        
    }

当然,代码中还有很多需要重构的地方,但这就是没有检测到冲突的原因。

为了避免穿过墙壁,计算新位置,检查碰撞,如果为真,回滚位置变化:

@Override
public void actionPerformed(ActionEvent arg0) {
    int rollbackX=packman.x;
    int rollbackY=packman.y;

    switch (count) {
    case 1:
        packman.x = packman.x - xVel;
        break;
    case 2:
        packman.y = packman.y - yVel;
        break;
    case 3:
        packman.x = packman.x + xVel;
        break;
    case 4:
        packman.y  = packman.y+yVel;
        break;
    }

    //Collision found, rollback
    if (zkontrolujKolizi()) {
        packman.x=rollbackX;
        packman.y=rollbackY;
    } else {        
        repaint();
    }
}

public boolean zkontrolujKolizi() {
    return packman.intersects(platform) || packman.intersects(secondPlat);
}

答案 1 :(得分:0)

before collision after collision 我用过完全一样,但只是移动物体

 @Override
        public void actionPerformed(ActionEvent e) {
            //This is my actually code I've implemented from your answer
            int rollbackX = packRect.x;
            int rollbackY = packRect.y;
    
        switch (count) {
        case 1:
            packman.setCoordinatesX(packman.getCoordinatesX() - xVel);
            gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
            packRect.x = packRect.x - xVel;
            collectPoint();
    //      repaint();
            break;
    
        case 2:
            packman.setCoordinatesY(packman.getCoordinatesY() - yVel);
            gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
            packRect.y = packRect.y - yVel;
            sbirejBody();
    //      repaint();
            break;
        case 3:
            packman.setCoordinatesX(packman.getCoordinatesX() + xVel);
            gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
            packRect.x = packRect.x + xVel;
            collectPoint();
    //      repaint();
            break;
        case 4:
            packman.setCoordinatesY(packman.getCoordinatesY() + yVel);
            gifLabel.setLocation(packman.getCoordinatesX(), packman.getCoordinatesY()-38);
            packRect.y  = packRect.y+yVel; 
            collectPoint();
        //  repaint();
            break;
        }
        
      

  int originX = packRect.x;
int originY = packRect.y;
packRect.setLocation(originX, originY); 
packman.setCoordinatesX(originX);
packman.setCoordinatesY(originY);
gifLabel.setLocation(originX, originY);
if (checkCollision) {
    rollbackX= originX;
    rollbackY = originY;
    
}else {
    repaint();
}
 packRect.setLocation(originX, originY); 
    packman.setCoordinatesX(originX);
    packman.setCoordinatesY(originY);
    gifLabel.setLocation(originX, originY);
}

public boolean checkCollision() {
    return packman.intersects(platform) || packman.intersects(secondPlat);
}