pythonpygamecollision

rect.collisionrect not working between two rects


I'm having trouble getting my collision to work in my game, I am using two rects, one acting as a paddle and one as a ball. I am using the normal pygame statement, rect1.colliderect(rect2), but for some reason it is not working.

Here is the line of code for the rects collision

    #collision of ball with paddle
    if (paddle.colliderect(ball)):
        ball_x = 10
        ball_y = 10

Not sure what's wrong. Here is the full coding if you wanna run it.

#December 16, 2019
#Final Project - Breakout

#IMPORTING LIBRARIES-----
import pygame
import sys
import time

#INITIALIZING SCREEN SIZE-----
pygame.init()
screen_size = (700, 750)
screen = pygame.display.set_mode((screen_size),0)
pygame.display.set_caption("BREAKOUT")

#retrieve screen measurements
screen_w = screen.get_width()
screen_h = screen.get_height()

#retrieve position of center of screen
center_x = int(screen_w/2)
center_y = int(screen_h/2)

#COLOURS-----
WHITE = (255,255,255)
BLACK = (0, 0, 0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
PURPLE = (154, 136, 180)

#BACKGROUND-----
screen.fill(BLACK)
pygame.display.update()

#SPEED-----
clock = pygame.time.Clock()
FPS = 60 #set frames per second
speed = [4,4]
paddle_speed = 6

#VARIABLES-----

#paddle
paddle_w = 100
paddle_h = 10
paddle_x = 500
paddle_y = 670

paddle_dx = 0
paddle_dy = 0

#ball
ball_w = 10
ball_h = 10
ball_x = center_x
ball_y = center_y


#RECTS-----
paddle = pygame.Rect(paddle_x, paddle_y, paddle_w, paddle_h)
ball = pygame.Rect(ball_x, ball_y, ball_w, ball_h)

#LOOPS-----
game = False


#loop for game
game = True
while game:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            game = False
            pygame.quit()
            sys.exit()
        #moving paddle with keys
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                paddle_dx = -paddle_speed
            elif event.key == pygame.K_RIGHT:
                paddle_dx = paddle_speed

    if event.type == pygame.KEYUP:
        paddle_dx = 0

    #constrain this loop to the specified FPS
    clock.tick(FPS)

    #PADDLE EVENTS-----

    #store old paddle positions
    old_paddle_x = paddle.x
    old_paddle_y = paddle.y

    #moving the paddle rect
    paddle.move_ip(paddle_dx, paddle_dy)

    #check to see if rect has left screen
    if paddle.left < 0 or paddle.right > screen_w:
        paddle.x = old_paddle_x

    #BALL EVENTS-----

    #moving ball
    ball = ball.move(speed)

    #collision left & right
    if ball.left < 0 or ball.right > screen_w:
        speed[0] = -speed[0]

    #collision top
    if ball.top < 0 or ball.bottom > screen_h:
        speed[1] = -speed[1]

    #collision of ball with paddle
    if (paddle.colliderect(ball)):
        ball_x = 10
        ball_y = 10

    #removes screen trail
    screen.fill(BLACK)

    #drawing paddle/ball inside rect
    pygame.draw.rect(screen,PURPLE,paddle,0)
    pygame.draw.rect(screen,WHITE,ball,0)

    #updating the screen
    pygame.display.update()

pygame.quit()
sys.exit()

Solution

  • The position of the ball is defined by the pygame.Rect object ball. ball_x and ball_y is just used to initialize ball.
    You have to set ball.x = 10 and ball.y = 10 rather than ball_x = 10 and ball_y = 10:

    if paddle.colliderect(ball):
        ball.x = 10
        ball.y = 10
    

    To make the ball bounce of the paddle you have to invert speed[1] rather than changing the position of the ball by ball.x = 10 and ball.y = 10:

    if paddle.colliderect(ball):
        speed[1] = -speed[1]