#Breakout Game Ball Gets Stuck Bouncing Up and Down or Left to Right
1 messages · Page 1 of 1 (latest)
<@&987246487241105418> please have a look, thanks.
I uploaded your attachments as Gist.
I imagine it has something to do with x or y component being zero. You are basing your ball direction change solely on some multiplication of x or y and because zero times anything is zero, it remains zero.
so do I change the 0's here? if (totalBricks <= 0) {
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.RED);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("You Won!", 260, 300);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press Enter to Restart ", 230, 350);
}
if (ballposY > 570) {
play = false;
ballXdir = 0;
ballYdir = 0;
Was just giving ideas. Possibly here.
ballXdir = (int) Math.round(speed * Math.cos(angle)); // Convert angle to x-direction
ballYdir = (int) Math.round(speed * Math.sin(angle)); // Convert angle to y-direction
which is right after this
double angle = Math.toRadians(Math.random() * 120 + 30); // Get a random angle between 30 and 150 degrees
Seems like you increment position by this ball dir. So if ball dir is zero then that axis stays at the same value.
Maybe don't let ballXdir and ballYdir be zero.