Home > Mobile >  Ball will not bounce off paddle nor wall in my brick breaker game
Ball will not bounce off paddle nor wall in my brick breaker game

Time:07-22

I have been sitting here for 5 hours trying to make my ball bounce off the paddle and the walls when it hits it but it just goes straight through the walls and paddle then re enters through the top and the same cycle continues. I don't know where I am going wrong

My MainWindow class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class MainWindow extends JPanel implements KeyListener, ActionListener {

private boolean gameRunning = true;
private int BOARD_WIDTH = 500;
private int BOARD_HEIGHT = 500;
private Thread animator;
private BufferedImage img;
private Dimension dimension;
private int ballX = 20;
private int ballY = 200;
private int ballSpeedX = 4;
private int ballSpeedY = 5;
private int ballWidth = 20;
private Brick[] bricks = new Brick[24];
private boolean[] showBrick = new boolean[24];
private int paddleX = BOARD_WIDTH/2;
private int paddleY = BOARD_HEIGHT-50;
private int paddleW = 100;
private int paddleH = 10;
boolean paddleLeft = false;
boolean paddleRight = true;
private Timer timer;
private int delay = 50;


public MainWindow(){
    addKeyListener(this);
    //addMouseListener(this);
    setFocusable(true);
    dimension = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
    setBackground(Color.BLACK);

    int x = 20;
    int y = 20;
    int count = 1;
    for(int i = 0; i < bricks.length; i  ){
        bricks[i] = new Brick(x, y, 50, 10);
        showBrick[i] = true;
        x  = 55;
        if (count%8 == 0){
            x = 20;
            y  = 20;
        }
        count  ;
    }
    timer = new Timer(delay, this);
    timer.start();

    //if (animator == null || !gameRunning) {
      // animator = new Thread(this);
        //animator.start();
    //}
    setDoubleBuffered(true);
}

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.pink);
    g.fillRect(0,0, dimension.width, dimension.height);
    g.setColor(Color.black);

    // for the bricks
    for(int i = 0; i<bricks.length; i  ) {
        if(showBrick[i])
            g.fillRect(bricks[i].getBrickX(), bricks[i].getBrickY(),bricks[i].getBrickW(),bricks[i].getBrickH());

    }
    // for the paddle
    g.fillRect(paddleX, paddleY, paddleW, paddleH);

    // for the ball
    g.fillOval(ballX, ballY, ballSpeedX,ballSpeedY);


    //if (gameRunning){
        //ballX =ballSpeedX;
      //  movingBall();
        //repaint();

    //}
    g.dispose();
}

private void movingBall(){

}

   
@Override
public void keyTyped(KeyEvent e) {
    //if (e.getKeyCode() == KeyEvent.VK_RIGHT){
      //  paddleX =5;
    //}

}

@Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (e.getKeyCode() == KeyEvent.VK_RIGHT){
        if(paddleX >= 385){
            paddleRight = false;
        }
        else {
            //ballX =ballSpeedX;
            paddleRight = true;
            paddleX =5;
            repaint();
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT){
        if(paddleX <= 10){
            paddleLeft = false;
        }
        else {
            paddleLeft = false;
            paddleX-=5;
            repaint();
        }
    }

}

@Override
public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (e.getKeyCode() == KeyEvent.VK_RIGHT){
        paddleRight = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT){
        paddleLeft = false;
    }

}


@Override
public void actionPerformed(ActionEvent e) {
    timer.start();
    if(ballX < 0 || ballX > BOARD_WIDTH){
        ballSpeedX = ballSpeedX * -1;
    }
    if(ballY < 0){
        ballSpeedY = ballSpeedY * -1;

    }
    if(ballY > BOARD_HEIGHT){
        ballX = 20;
        ballY = 200;
        ballSpeedY *= -1;
    }
    if(ballX == paddleX && ballY == paddleY){
        ballSpeedY *= -1;
    }
    if(ballX   ballWidth > paddleX && ballX < paddleX   paddleW && ballY   ballWidth > paddleY && ballY < paddleY   paddleH) {
        ballSpeedY *= -1;
    }
    ballX  = ballSpeedX;
    ballY  = ballSpeedY;
    repaint();
    //System.out.println("its working");
}

}

My Window class:

import javax.swing.*;

public class Window {

public Window() {

    JFrame frame = new JFrame("Brick Builder Game");
    frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
    frame.setSize(500,500);
    frame.add(new MainWindow());
    frame.setVisible(true);
    frame.setResizable(false);

}

public static void main(String[] args){
    new Window();
}
}

My brick class:

public class Brick {

// X-position of brick
private int brickX;
// Y-position of brick
private int brickY;
// width of brick
private int brickW;
// height of brick
private int brickH;

public Brick(int brickX, int brickY, int brickW, int brickH) {
    this.brickX = brickX;
    this.brickY = brickY;
    this.brickW = brickW;
    this.brickH = brickH;
}

public int getBrickX(){
    return brickX;
}

public int getBrickY(){
    return brickY;
}
public int getBrickW(){
    return brickW;
}
public int getBrickH(){
    return brickH;
}

}

CodePudding user response:

the signature for fillOval is as follows: (x_position, y_position, width, height). And in your signature, you try to use ballSpeedX and ballSpeedY as the width and height.

So when you do ballSpeedX or ballSpeedY *= -1. Swing tries to draw an oval, with negative measures, which is not possible, and thus fails to show the ball

  • Related