#NOT SOLVED Pong game problem with checking if ball hit paddle

7 messages · Page 1 of 1 (latest)

acoustic sealBOT
#

This post has been reserved for your question.

Hey @dawn condor! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dawn condor
#
import java.awt.*;
import java.awt.event.KeyEvent;

public class Paddle extends Rectangle {
    int xPosition;
    int yPosition;
    final int id;
    Paddle(int xPosition, int yPosition, int id){
        super(xPosition, yPosition, 20, 100);
        this.xPosition = xPosition;
        this.yPosition = yPosition;
        this.id = id;
    }
    public void draw(Graphics2D graphics2D){
        if (id == 1){
            graphics2D.setColor(new Color(0x00ff00));
        } else if (id == 2) {
            graphics2D.setColor(new Color(0xff0000));
        }
        else {
            System.out.println("Unexpected paddle ID: "+id);
            return;
        }
        graphics2D.fillRect(xPosition, yPosition, 20, 100);
    }
    public void keyPressed(KeyEvent e){
        switch (e.getKeyCode()){
            case 87:
                if (id == 1){
                    movePaddle(-10);
                }
                break;
            case 83:
                if (id == 1){
                    movePaddle(10);
                }
                break;
            case 38:
                if (id == 2){
                    movePaddle(-10);
                }
                break;
            case 40:
                if (id == 2){
                    movePaddle(10);
                }
                break;
        }
    }
    public void movePaddle(int yPositionChange){
        // check if paddle is out of bounds
        if (!((yPosition <= 0 && yPositionChange < 0) || (yPosition >= 300 & yPositionChange > 0))){
            yPosition += yPositionChange;
        }
    }

}

Paddle class

#
import java.awt.*;
import javax.swing.*;

public class Ball extends Rectangle {
    int xPosition;
    int yPosition;
    int xVelocity;
    int yVelocity;
    Ball(int xPosition, int yPosition, int xVelocity, int yVelocity){
        super(xPosition, yPosition, 30, 30);
        this.xPosition = xPosition;
        this.yPosition = yPosition;
        this.xVelocity = xVelocity;
        this.yVelocity = yVelocity;
    }
    public void draw(Graphics2D graphics2D){
        graphics2D.setColor(new Color(0xffffff));
        graphics2D.fillOval(xPosition, yPosition, 25, 25);
    }
    public void moveBall(){
        xPosition += xVelocity;
        // check if hit border
        if (xPosition <= 0){
            // hit left border
        } else if (xPosition >= 570) {
            // hit right border

        }
        // bounce of top or bottom
        if (yPosition <= 0 || yPosition >=370){
            yVelocity *= -1;
            // multiplying velocity by -1 means that direction is reversed
        }
        yPosition += yVelocity;
    }
}

Ball class

dawn condor
#

NOT SOLVED Pong game problem with checking if ball hit paddle

acoustic sealBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

hallow tartan
#

Could you also share the code where you check for collision between the paddle and the ball ?