三桨乒乓击中检测

时间:2011-11-30 03:08:20

标签: java pong

我正在创建另一个版本的pong。这个人每个人使用3个拨片。我花了将近两个小时的时间试图弄清楚为什么另外两个桨没有检测到球撞到球拍时。原始(顶部)拨片确实检测到命中并正确更新命中计数器。我尝试过单独的if语句,else if语句等,但没有成功。我创建了三个Y变量来检测所有三个桨的位置仍然没有运气。

有什么建议吗?

import java.awt.Point;


public class box {
    private int xTopLeft, yTopLeft, width, height;
    int xBall, yBall;
    private int radius = 5;
    int xBallVel, yBallVel;
    int  VELOCITY_MAG =5;
    public Point ballLoc = new Point();

    private int paddleY;
    private int paddleYP2;
    private int paddleYP3;
    private int paddleWidth = 20;
    private int paddleWidth2 = 20;
    private int paddleWidth3 = 20;
    int hitCount = 0;
    int missCount = 0;

    private boolean updating=true;

    public int getBallRadius()
    {
        return radius;
    }
    public Point getBallLoc()
    {
        ballLoc.x = xBall;
        ballLoc.y = yBall;
        return ballLoc;
    }
    public box(int x, int y, int w, int h)
    {
        xTopLeft =x;
        yTopLeft = y;
        width =w;
        height = h;
        createRandomBallLocation();
    }
    public void updatePaddle(int y)
    {

        paddleY = y;
    }
    public void updatePaddleP2(int yp2)
    {

        paddleYP2 = yp2;
    }
    public void updatePaddleP3(int yp3)
    {

        paddleYP3 = yp3;
    }
    public int getPaddleWidth()
    {
        return paddleWidth ;
    }
    public int getPaddleWidth2()
    {
        return paddleWidth2 ;
    }
    public int getPaddleWidth3()
    {
        return paddleWidth3 ;
    }
    public int getPaddleY()
    {
        System.out.println(paddleY);
        return paddleY;

    }
    public int getPaddleP2()
    {
        System.out.println(paddleYP2);
        return paddleYP2;
    }
    public int getPaddleP3()
    {
        return paddleYP3;
    }
    public int getHitCount()
    {
        return hitCount;
    }
    public int getMissCount()
    {
        return missCount;
    }
    public int velMag()
    {
        return VELOCITY_MAG;
    }
        public void update()
    {
        if (!updating) return;

        xBall = xBall + xBallVel;
        yBall = yBall + yBallVel;

        if (xBall + radius >= xTopLeft+width && xBallVel>=0)

            if ((yBall >= paddleY-paddleWidth && yBall <= paddleY + paddleWidth)
                    || (yBall >= paddleYP2-paddleWidth2 && yBall <= paddleYP2 + paddleWidth2 )
                    || (yBall >= paddleYP3-paddleWidth3 && yBall <= paddleYP3 + paddleWidth3 ))
            {
                // hit paddles

                xBallVel = - xBallVel;
                hitCount++;

            }

        else if (xBall+radius >= xTopLeft + width)
            {
                xBallVel = - xBallVel;

            }


        if (yBall+radius >= yTopLeft + height  && yBallVel >= 0)
        {
            yBallVel = - yBallVel;
        }
        if (yBall-radius <= yTopLeft && yBallVel <=0)
        {
            yBallVel = - yBallVel;
        }
        if (xBall-radius <= xTopLeft && xBallVel <= 0)
        {
            xBallVel = - xBallVel;
        }

    }
    public void createRandomBallLocation()
    {
        xBall = xTopLeft + radius + 
                (int)((width-2*radius)*Math.random());

            yBall = yTopLeft + radius + 
                (int)((height-2*radius)*Math.random());

            xBallVel = velMag();
            yBallVel = velMag();

    }
}
import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;

import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;

public class Pong extends JFrame
implements Runnable{

    box box = null;
    int top, left, width, height;
    final int LINE_THICKNESS = 5;
    Graphics bufferGraphics; 
    int sleep=25;

    public Pong(String name)
    {
        super(name);
        int windowWidth = 1024;
        int windowHeight =768;

        this.setSize(windowWidth, windowHeight);
        this.setResizable(false);
        this.setLocation(400, 150);
        this.setVisible(true);

        this.createBufferStrategy(4);
        MyMouseClick mmc = new MyMouseClick();
        addMouseListener(mmc);
        MyMouseMotion mmm = new MyMouseMotion();
        addMouseMotionListener(mmm);


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Thread thread = new Thread(this);
        thread.start();  
    }
    public void run()
    {
        while(true)
        {
            try
            {
                if (box != null)
                {
                    box.update();
                    repaint();

                }
                Thread.sleep(sleep); 
            }
            catch (InterruptedException e)
            {}
        }

    }

    public void paint(Graphics g)
    {  
        BufferStrategy bf = this.getBufferStrategy();
        g = bf.getDrawGraphics();
        Dimension d = getSize();
        if (box ==null)
        {
            Insets insets = getInsets();
            left = insets.left;
            top = insets.top;
            width = d.width-left - insets.right;
            height = d.height - top - insets.bottom;
            box = new box(left, top, width, height);

        }

        g.fillRect(0,0, d.width, d.height);
        g.setColor(Color.BLUE);
        g.setFont(new Font("Arial", 20, 30));
        g.drawString("Hits: "+box.getHitCount(), 20, 60);
        g.setColor(Color.red);
        g.setFont(new Font("Arial", 20, 30));
        g.drawString("Misses: "+box.getMissCount(), 20, 380);


        g.fillRect(left, top, left+width, LINE_THICKNESS); // top of box
        g.setColor(Color.white);
        g.fillRect(left, top+height, left+width, LINE_THICKNESS); // bottom of box
        g.drawLine(left, top, left, top+height); // left of box
        g.drawLine(left+width, top, left+width, top+height); // right side

        Point ballLoc = box.getBallLoc();
        int radius = box.getBallRadius();


        g.fillOval(ballLoc.x - radius, ballLoc.y-radius, 2*radius, 2*radius);

        // Draw paddles Player 1
        g.setColor(Color.yellow);
        int yp = box.getPaddleY();
        int yp2 = box.getPaddleP2();
        int yp3 = box.getPaddleP3();
        int yw = box.getPaddleWidth();
        int yw2 = box.getPaddleWidth2();
        int yw3 = box.getPaddleWidth3();
        g.fillRect(left+width-5, yp-yw, 4, 50);
        g.fillRect(left+width-5, (yp2-yw2)+280, 4, 50);
        g.fillRect(left+width-5, (yp3-yw3)+140, 4, 50);
        bf.show();
    }    

    // *********************** Inner classes
    class MyMouseClick extends MouseAdapter
    {
        public void mouseClicked(MouseEvent e)
        {
            box = null;
            repaint();
        }
    }
    class MyMouseMotion extends MouseMotionAdapter
    {
        public void mouseMoved(MouseEvent e)
        {
            int y = e.getY();
            int y2 = e.getY();
            int y3 = e.getY();
            if (box != null)
            {
                box.updatePaddle(y);
                box.updatePaddleP2(y2);
                box.updatePaddleP3(y3);
                repaint();
            }

        }


    }

    // ************************************


    public static void main(String[] args) { 
        Pong t = new Pong("Three Paddle Pong");
        t.setVisible(true);  
    } // end of main


}

Screenshot

1 个答案:

答案 0 :(得分:1)

paint方法中,您可以在不同的y位置绘制3个拨片,但对于用于碰撞检测的实际拨片位置paddleYP?,您只需将3个拨片设置为相同y中的mouseMoved