bouncing Ball (Brick-Breaker-Game)

Jxhnny.lpz

Mitglied
Hallo ihr Lieben,

ich habe ein Problem, welches ich nach stunden langem herum versuchen selber nicht hin bekomme.
Im folgenden Code bin ich dabei eine eigene Version vom einem Brick-Breaker-Game zu erstellen, doch dabei kommt es zu denn Schwierigkeiten das der Ball nicht ordentlich am Schläger oder später auch an einem Brick abprallt. Langsam gehen mir Leider die Ideen für eine Umsetzung aus.

Kurze INFO:
Ich habe denn Schläger zur Testung einfach etwas größer gemacht. Die Größe wird später wieder angepasst.

Würde mich Freuen wenn Ihr mir Helfen könntet.
Für Verbesserungstipps steh ich immer gerne offen.

schon mal Frohe Weihnachten an euch alle.

Java:
import javax.swing.*;
import java.awt.*;

public class BBGAME extends JPanel {

    public static final int WINDOW_HEIGTH = 750;
    public static final int WINDOW_WIDTH = 700;
    public static final int WINDOW_X = 610;
    public static final int WINDOW_Y = 145;

    //test
    public static int X = 150+50;
    public static int Y = 150+20;

    public int BallmoveX = 3;
    public int BallmoveY = 3;

    int paddingLinks = 44;
    //


    public Block block = new Block((int) (WINDOW_X * 0.05), (int) (WINDOW_Y * 0.15), (int) (WINDOW_WIDTH * 0.90), (int) (WINDOW_HEIGTH * 0.75));
    public Block schlaegerblock = new Block(
//            block.getWidth() - 330
                    260,
//            block.getHeight() - 50
                    380, 80, 80 );
    public Ball ball = new Ball(block.getX() , block.getY(), 20, 20 ,Color.GREEN);

    public BBGAME() throws InterruptedException {

        //*
        Thread thread = new Thread() {
            public void run() {







//                int i = 0;
                while (true) {
//
                    X = X + BallmoveX;
                    Y = Y + BallmoveY;

                    //Prallt am Rand ab:
                        //links-rechts:
                    if(X < 3){
                        BallmoveX = -BallmoveX;
                        X = ball.getWidth() - ball.getWidth();
                    }else if(X + ball.getWidth() > block.getWidth()){
                        BallmoveX = -BallmoveX;
                        X = block.getWidth() - ball.getWidth();
                    }
                        //oben-unten:
                    if(Y < 3){
                        BallmoveY = -BallmoveY;
                        Y = ball.getHeight() - ball.getHeight();
                    }else if(Y + ball.getHeight() > block.getHeight()){
                        BallmoveY = -BallmoveY;
                        Y = block.getHeight() - ball.getHeight();
                    }

                    //bouncing
                    if((Y + ball.getHeight() >= schlaegerblock.getY() - ball.getHeight() ) && // bounnce oben
                            (Y + ball.getHeight() <= schlaegerblock.getY() + schlaegerblock.getHeight()) && //bounce unten
                            (( X + ball.getHeight() >= schlaegerblock.getX()  ) &&
                            ( X + ball.getHeight() <= schlaegerblock.getX() + schlaegerblock.getHeight() ))) {
                            BallmoveY = -BallmoveY;
                            System.out.println("Schläger: abprall oben-unten");

                    } else if((X + ball.getWidth() >= schlaegerblock.getX() - ball.getWidth() - 10) && // bounnce links
                                    (X + ball.getWidth() <= schlaegerblock.getX() + schlaegerblock.getWidth() - 5) && //bounce rechts
                                    (( Y + ball.getWidth() >= schlaegerblock.getY() ) &&
                                    ( Y + ball.getWidth() <= schlaegerblock.getY() + schlaegerblock.getWidth()))) {
                                    BallmoveX = -BallmoveX;
                                    System.out.println("Schläger: abprall Seiten");
                    }
                    
                    repaint();

                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException ex) {
                    }


                }
            }

        };
        thread.start();
      
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D spielFeld = (Graphics2D) g;
        spielFeld.setColor(Color.BLACK);
        spielFeld.setStroke(new BasicStroke(3));
        spielFeld.drawRect(block.getX(), block.getY(), block.getWidth(), block.getHeight());

        Graphics2D ballGraphic = (Graphics2D) g;
        ballGraphic.setColor(ball.getColor());
        ballGraphic.fillOval(X + ball.getX(), Y + ball.getY(), ball.getWidth(), ball.getHeight());

        Graphics2D ballGraphic1 = (Graphics2D) g;
        ballGraphic1.setColor(Color.BLACK);
        ballGraphic1.setStroke(new BasicStroke((float) 0.5));
        ballGraphic1.drawOval(X + ball.getX(), Y + ball.getY(), ball.getWidth(), ball.getHeight());


        Graphics2D ballGraphic2 = (Graphics2D) g;
        ballGraphic2.setColor(Color.RED);
        ballGraphic2.fillOval(X + ball.getX(), Y + ball.getY(),4, 4);

        Graphics2D ballGraphic3 = (Graphics2D) g;
        ballGraphic3.setColor(Color.RED);
        ballGraphic3.fillOval(schlaegerblock.getX(), schlaegerblock.getY(),4, 4);

        Graphics2D schlaeger = (Graphics2D) g;
        schlaeger.setColor(Color.BLUE);
        schlaeger.setStroke(new BasicStroke(3));
        schlaeger.drawRect(schlaegerblock.getX(), schlaegerblock.getY(), schlaegerblock.getWidth(), schlaegerblock.getHeight());
    }



    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Bouncing Ball");
        // alle Eigenschaften
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WINDOW_WIDTH, WINDOW_HEIGTH);
        frame.setLayout(null);
        frame.setContentPane(new BBGAME());
        frame.setDefaultLookAndFeelDecorated(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);











    }


}

Java:
public class Block {

    private int x;
    private int y;
    private int width;

    private int height;

    public Block(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public void setLocation(int x, int y){
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }


}

Java:
import java.awt.Color;

public class Ball {
    private int x;
    private int y;
    private int radius;
    private int height;
    private int width;
    private Color color;


    public Ball(int x, int y,
//                int radius,
                int height, int width, Color color){
        this.x = x;
        this.y = y;
//        this.radius = radius;
        this.height = height;
        this.width = width;
        this.color = color;

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
//    public int getRadius() {
//        return radius;
//    }

//    public int getDurchmesser(){return radius *2;}

//    public void setRadius(int radius) {
//        this.radius = radius;
//    }
    public int getHeight() {return  height;}
    public void setHeight(int height){this.height = height;}
    public int getWidth(){return width;}
    public void setWidth(int width){this.width = width;}
    public Color getColor(){return color;}

    public void setColor(Color color){this.color = color;}
}
 

Blender3D

Top Contributor
Hallo ihr Lieben,

ich habe ein Problem, welches ich nach stunden langem herum versuchen selber nicht hin bekomme.
Im folgenden Code bin ich dabei eine eigene Version vom einem Brick-Breaker-Game zu erstellen, doch dabei kommt es zu denn Schwierigkeiten das der Ball nicht ordentlich am Schläger oder später auch an einem Brick abprallt. Langsam gehen mir Leider die Ideen für eine Umsetzung aus.
Ich habe zu diesem Thema ein Beispiel gepostet.
https://www.java-forum.org/thema/bouncing-ball-abprallender-ball.199894/#post-1330692

Hier die Jar zun Testen mit der Taste s
https://www.java-forum.org/thema/bouncing-ball-abprallender-ball.199894/#post-1330741
 

Ähnliche Java Themen

Neue Themen


Oben