1

我正在制作一个类似于 pong 的游戏,屏幕两侧有两个球拍,玩家必须击球。问题是,我如何对图像进行碰撞检测。我知道有一个简单的矩形方法。这是我的代码。我的橱窗课

import javax.swing.JFrame;
import java.awt.Rectangle;
public class window{
    public static void main(String args[])
    {
        JFrame f = new JFrame();
        second s = new second();
        f.add(s);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(806,628);
        f.setResizable(false);
        f.setTitle("Pong!");
    }
}

而在这堂课中,我真的需要帮助!这是我的第二节课,我称它为第二节课(多么有创意)。

import javax.swing.*;
import java.awt.Rectangle;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.JFrame;
public class second extends JPanel implements ActionListener, KeyListener
{
    Timer t = new Timer(5, this);
    double x1 = 20, y1 = 250, velx1 = 0, vely1 = 0;
    double x = 750, y = 250, velx = 0, vely = 0;
    double xb = 384, yb  = 290, velbx = 3.0, velby = 3.0;
    boolean collision = false;
    public second()
    {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.fill(new Rectangle2D.Double(x,y,40,40));
        super.paintComponent(g);
        ImageIcon i = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin\\pong1.png");
        ImageIcon a = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin\\pong-bg.png");
        ImageIcon b = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin\\pong1.png");
        ImageIcon c = new ImageIcon("C:\\Users\\gsjha\\workspace\\JFrame Pong\\bin/Ball.png");
        a.paintIcon(this, g, 0, 0);
        i.paintIcon(this, g, (int)x1 , (int)y1);
        b.paintIcon(this, g, (int) x, (int) y); 
        c.paintIcon(this, g, (int)xb, (int)yb);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
        if (y1<=0 || y1 >= 600 || y1 >= 500)
        {
            vely1 = -vely1;
        }
        x1 +=velx1;
        y1 += vely1;
        repaint();
        if (y<=0 || y >= 600 || y > 500)
        {
            vely = -vely;
        }
        repaint();
        x += velx;
        y += vely;
        repaint();
        if (xb<0 || xb > 800)
        {
            velbx = -velbx;
        }
        if (yb<0 || yb > 600)
        {
            velby = -velby;
        }
        xb += velbx;
        yb += velby;
        repaint();
    }
    public void up()
    {
        vely1 = -3.0;
        velx1 = 0;

    }
    public void up1()
    {
        vely = -3.0;
        velx = 0;
    }
    public void down()
    {
        vely1 = 3.0;
        velx1 = 0;
    }
    public void down1()
    {
        vely = 3.0;
        velx = 0;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_W)
        {
                up();
        }
        if(code == KeyEvent.VK_S)
        {
            down();
        }
        if (code == KeyEvent.VK_UP)
        {
            up1();
        }
        if(code == KeyEvent.VK_DOWN)
        {
            down1();
        }
    }
    @Override
    public void keyReleased(KeyEvent ee) {
        int code1 = ee.getKeyCode();
        if(code1 == KeyEvent.VK_W)
        {
            vely1 = 0;
        }
        if (code1 == KeyEvent.VK_S)
        {
            vely1 = 0;
        }
        if(code1 == KeyEvent.VK_UP)
        {
            vely = 0;
        }
        if(code1 == KeyEvent.VK_DOWN)
        {
            vely = 0;
        }
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}

正如你所看到的,除了乒乓球和球拍之间的碰撞检测之外,我已经完成了大部分乒乓球比赛。游戏中最重要的部分。谢谢你,阿达什·贾

4

1 回答 1

0

有很多方法可以做到这一点。如果你不需要太多的精度,你可以使用边界框,并根据它们的大小和位置,找出是否有碰撞。您可以阅读以下内容:http: //gpsnippets.blogspot.com.es/2008/10/bounding-box-collision-detection.html了解更多信息。

于 2013-08-26T23:27:27.763 回答