# How to get Blocks to Disappear in Paddle Ball Game and Help with Paddle Collision

Hello. I am working on this assignment where I have to make a simple paddle ball game where when the ball hits the blocks they disappear. The hint we were given was that we had to use Boolean expressions, but I am completely lost. My ball is just reflecting off the sides of the screen and goes through the blocks instead of where it is supposed to hit them and reflect back. Additionally, I have been able to figure out how to get the ball to reflect off of the paddle.

I did:

`````` if y_pos <= 50:
y_speed *= -1
elif (y_pos == rect_transy - 50) and (x_pos <= mouseX + 60) and (x_pos >= mouseX - 60):
y_speed *= -1

``````

This part of my code worked a couple times when I played it, however, after doing some other changes to my code it will not function with the above code and the ball goes through my paddle. Instead, my code only responds to:

`````` if y_pos <= 50:
y_speed *= -1
elif (y_pos >= rect_transy - 50) and (x_pos <= mouseX + 60) and (x_pos >= mouseX - 60):
y_speed *= -1

``````

I have no idea what has gone wrong.

Here is my complete code. I am a beginner, so I am just learning as I go along with the assignments. Any suggestions would be much appreciated!

``````# global variables
x_pos = 100
y_pos = 50
x_speed = 2
y_speed = 4
rect_transy = 550
rect_posx = 0
rect_posy = 0
rect_width = 100
rect_height = 20
block_height = [100, 100, 100, 100]
block_width = [200, 200, 200, 200]
block_posx = [0, 200, 400, 600]
block_posy = [0, 0, 0, 0]

def setup ():
size (600, 700)

def draw ():
global x_pos, y_pos, x_speed, y_speed, rect_transy, rect_width, rect_height, rect_posx, rect_posy, block_height, block_width
background (0)

for i in range (3):
rect (block_posx[i], block_posy[i], block_width[i], block_height[i])

circle (x_pos, y_pos, 50)

x_pos += x_speed
y_pos += y_speed

translate (mouseX, rect_transy)
translate (-50, -20)
rect (rect_posx, rect_posy, rect_width, rect_height)

if x_pos <= 50:
x_speed *= -1
elif x_pos >= 550:
x_speed *= -1

if y_pos <= 50:
y_speed *= -1
elif (y_pos >= rect_transy - 50) and (x_pos <= mouseX + 60) and (x_pos >= mouseX - 60):
y_speed *= -1

``````
1 Like

Before you can remove any brick or rebound off it, you need to establish if the ball has collided with it. You can make the following changes to your `for` loop. If any ball is positioned over a brick, that brick is filled red.

``````    ...

for i in range (3):
fill('#FFFFFF')

# brick edges
left_edge = block_posx[i]
right_edge = block_posx[i] + block_width[i]
top_edge = block_posy[i]
bottom_edge = block_posy[i] + block_height[i]

if (x_pos+25 > left_edge and x_pos-25 < right_edge and
y_pos+25 > top_edge and y_pos-25 < bottom_edge):
fill('#FF0000')

rect(block_posx[i], block_posy[i], block_width[i], block_height[i])

...
``````

This is an example of AABB collision detection. You test that the left-edge of the ball is somewhere to the right of the left-edge of the brick; that the right-edge of the ball is to the left of the right-edge of the brick; and so forth. I add/subtract 25 pixels from the `x_pos` and `y_pos` variables as an offset, because a circle is drawn from its center (50 divided by 2 is 25).

I used your complete version of the code and the ball rebounds off the paddle.

1 Like