Question

I am creating yet another version of pong. This one uses 3 paddles for each person. I have spent almost two hours trying to figure out why the two other paddles are not detecting when the ball strikes the paddle. The original (top) paddle does detect the hit and properly updates the hit counter. I have tried separate if statements, else if statements etc., with no success. I created three Y variables to detect the position of all three paddles still no luck.

Any suggestions?

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

Was it helpful?

Solution

In the paint method you do paint your 3 paddles at different y positions, but for the actual paddle positions, paddleYP? which are used for collision detection, you just set the 3 paddles to the same y in mouseMoved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top