1

I have a ball sprite that is generated via generateSprite(); and also a platform sprite that is generated the same way. I need to make the ball bounce off the platform. This is a breakout style game so I have rectangles that are NOT sprites that the ball needs to hit, bounce off of and make them disappear. How would I do this?

To make the ball bounce off the platform this is what I tried:

 ballvx = 2;
    ballvy = 2;
    ballx += ballvx;
    bally += ballvy;
   if (ballx > platformx && ballx < platformx + 200 && bally > platformy) {
    if (bally < platformy + 20) {
      ballvy *= -1;
    } else {
      ballvx *= -1;
    }
  }
    if (ballx < 0 || ballx > width) {
      ballvx *= -1;
    }
    if (bally < 0 || bally > height) {
      state=LOSE;
    }

The problem here is that the ball simply goes straight through the platform instead of bouncing off of it.

For the rectangles I have no idea how to make them both disappear and have the ball bounce off them at the same time. Here is the code for the rectangles:

fill(r, g, b);
    rect(width*.01, height/28, w, h);
    fill(r, g, b);
    rect(width*.128, height/28, w, h);
    fill(r, g, b);
    rect(width*.25, height/28, w, h);
    fill(r, g, b);
    rect(width*.35, height/28, w, h);
    fill(r, g, b);
    rect(width*.45, height/28, w, h);
    fill(r, g, b);
    rect(width*.55, height/28, w, h);
    fill(r, g, b);
    rect(width*.65, height/28, w, h);
    fill(r, g, b);
    rect(width*.75, height/28, w, h);
    fill(r, g, b);
    rect(width*.85, height/28, w, h);
    fill(r, g, b);
    rect(width*.95, height/28, w, h);
    fill(r, g, b);
    rect(width*.01, height/4, w, h);
    fill(r, g, b);
    rect(width*.128, height/4, w, h);
    fill(r, g, b);
    rect(width*.25, height/4, w, h);
    fill(r, g, b);
    rect(width*.35, height/4, w, h);
    fill(r, g, b);
    rect(width*.45, height/4, w, h);
    fill(r, g, b);
    rect(width*.55, height/4, w, h);
    fill(r, g, b);
    rect(width*.65, height/4, w, h);
    fill(r, g, b);
    rect(width*.75, height/4, w, h);
    fill(r, g, b);
    rect(width*.85, height/4, w, h);
    fill(r, g, b);
    rect(width*.95, height/4, w, h);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Reed
  • 21
  • 1
  • 1
    There's multiple ways to do this and it depends a bit on your level of comfort with different programming concepts. The simplest I can think of is if you're familiar with arrays, for loops and functions (or better yet classes). The main issue is you're using hardcoded coordinates to simply render the blocks. What you want to do is not just render the block but store each block's state data (is it hit or not, it's coordinates and dimensions). You can then read that data (e.g. to render on screen), check for intersections, then write that data (e.g. change the state from not hit to hit)... – George Profenza Dec 16 '22 at 21:42
  • 1
    An alternative to the static array is using a dynamic array (`ArrayList` in Processing/Java) where you could store the coordinates of each block for example and on collision, remove that block from the list. Regarding the collisions itself, you could uses chain of if/else checking bounds encapsulated into a function: look at [`isOverRect()`](https://processing.org/examples/button.html). If you're already tried to check for collisions yourself you should update your answer to reflect that, or clarify what you're comfortable using at the moment (e.g. arrays/classes/etc.). – George Profenza Dec 16 '22 at 21:44
  • Duplicate post of https://stackoverflow.com/questions/74819891/ball-sprite-wont-bounce-off-rectangles-need-to-know-how-to-make-rectangles-dis – apodidae Dec 19 '22 at 17:10

0 Answers0