Im making a brick breaker game in pygame(python), problem is when the ball hits the bricks, the bricks dissapears(changes x and y),
but the ball doesnt want to go down when the collision occurs or vice versa if the ball hits the top of the brick the ball needs to go up..
here is the main function.. bunch of variables..
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
p_x_move = -5
elif event.key == pygame.K_RIGHT:
p_x_move = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
p_x_move = 0
elif event.key == pygame.K_RIGHT:
p_x_move = 0
Here is where i draw the screen.
screen.fill(white)
# platform calling
platform.draw()
platform.rules()
# new variables
platform_new_x = platform.move(p_x_move)
ball_new = ball.move(self.y_direction, self.x_direction)
# ball calling
ball.draw()
ball_new = list(ball_new)
# loop if statements
# 0 - y, 1 - x
if ball_new[0] < 5:
self.y_direction = "down"
if ball_new[0] > height:
break #game over
Here is some if statements that works (ignore this)
if ball_new[0]+5 > p_y and ball_new[1] >= platform_new_x and ball_new[1] < platform_new_x+p_width and ball_new[0] <= p_y+p_height:
self.y_direction = "up"
# width = 120
if ball_new[1] < platform_new_x + 40 and ball_new[0] >= p_y:
self.x_direction = "far_left"
if ball_new[1] > platform_new_x + 80 and ball_new[0] >= p_y:
self.x_direction = "far_right"
if ball_new[1] < platform_new_x + 60 and ball_new[1] > platform_new_x + 40 and ball_new[0] >= p_y:
self.x_direction = "short_left"
if ball_new[1] > platform_new_x + 60 and ball_new[1] < platform_new_x + 40and ball_new[0] >= p_y:
self.x_direction = "short_right"
if ball_new[1] > width:
self.x_direction = "far_left"
elif ball_new[1] < 0:
self.x_direction = "far_right"
bricks.draw()
So here i have the if statements I hope you will be able to read them I apologize in advance.. The ball collides with the brick fine, but the problem is that I want to ball to change its Y direction when the collision occurs This doesnt work that i wrote..
# ball collision with the brick
if ball_new[1] > bricks_1[0][0] and ball_new[1] - 100 < bricks_1[0][0] and ball_new[0] - 25 < bricks_1[0][1] and ball_new[0] > bricks_1[0][1]:
bricks_1[0] = (1000, 1000)
if ball_new[1] <= 210:
self.y_direction = "down"
print("down")
if ball_new[1] >= 225:
self.y_direction = "up"
if ball_new[1] > bricks_1[1][0] and ball_new[1] - 100 < bricks_1[1][0] and ball_new[0] - 25 < bricks_1[1][1] and ball_new[0] > bricks_1[1][1]:
bricks_1[1] = (1000, 1000)
if ball_new[1] <= 210:
self.y_direction = "down"
print("down")
if ball_new[1] >= 225:
self.y_direction = "up"
if ball_new[1] > bricks_1[2][0] and ball_new[1] - 100 < bricks_1[2][0] and ball_new[0] - 25 < bricks_1[2][1] and ball_new[0] > bricks_1[2][1]:
bricks_1[2] = (1000, 1000)
if ball_new[1] <= 210:
self.y_direction = "down"
print("true")
if ball_new[1] >= 225:
self.y_direction = "up"
if ball_new[1] > bricks_1[3][0] and ball_new[1] - 100 < bricks_1[3][0] and ball_new[0] - 25 < bricks_1[3][1] and ball_new[0] > bricks_1[3][1]:
bricks_1[3] = (1000, 1000)
if ball_new[1] <= 210:
self.y_direction = "down"
print("down")
if ball_new[1] >= 225:
self.y_direction = "up"
if ball_new[1] > bricks_1[4][0] and ball_new[1] - 100 < bricks_1[4][0] and ball_new[0] - 25 < bricks_1[4][1] and ball_new[0] > bricks_1[4][1]:
bricks_1[4] = (1000, 1000)
if ball_new[1] <= 210:
self.y_direction = "down"
print("down")
if ball_new[1] >= 215:
self.y_direction = "up"
pygame.display.update()
clock.tick(FPS)