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