Pygame Rectangle Collision Example

This article will show you an example of how to detect the collision between rectangles and points in pygame. You can use the method pygame.Rect.collidepoint(point) to detect whether a rectangle collides a point in pygame, you can use the method pygame.Rect.colliderect(rectangle) to detect whether a rectangle object collides another rectangle object in pygame.

1. Pygame Rectangle Collision Example.

  1. In this example, it will draw 100 random points and 100 random rectangles in this example, all the points and rectangles are blue color by default.
  2. It will also draw a red big rectangle, and you can move this red big rectangle by pressing it with your mouse.
  3. When the red big rectangle collides with the points or other rectangles when you move it, the points and rectangles will change their color to green.
  4. Below is the example source code.
    Created on Feb 23, 2022
    @author: Jerry Zhao
    import pygame, sys, random
    from ctypes.test.test_as_parameter import POINT
    # define the pygame main window size.
    MAIN_WINDOW_SIZE = (600,300)
    # this function will return a tuple with random x, y integer value.
    def random_point(max_x ,max_y):
        point_x = random.randint(0, max_x)
        point_y = random.randint(0, max_y)
        return (point_x, point_y)
    def rectangle_colliding_example():
        # initialize pygame application.
        # create the pygame application main window surface object.
        main_window_screen = pygame.display.set_mode(MAIN_WINDOW_SIZE)
        # set the main window title.
        pygame.display.set_caption('Pygame Rectangle Colliding Example.')
        # define the rectangle's top left point coordinate.
        rect_left = 100    
        rect_top = 100
        # calculate the rectangle object's width and height.
        rect_width = 100
        rect_height = 100
        # create the pygame.Rect object with the above values.
        rect_object = pygame.Rect(rect_left, rect_top, rect_width, rect_height)
        moving_by_mouse = False
        # declare the point list to save all random generated points coordinates.
        point_list = []
        # define the created random points number.
        random_point_number = 100
        # create 100 points and add them to the points list.
        for i in range(random_point_number):
            # create a point with random coordinate values.
            point_tmp = random_point(MAIN_WINDOW_SIZE[0], MAIN_WINDOW_SIZE[1])
            # add the point to the list.
        # declare the rectangle list to save all random generated rectangle objects.
        rectangle_list = []
        # it will create 100 random rectangles.
        random_rectangle_number = 100
        # create 100 rectangle and add them to the rectangles list.
        for i in range(random_rectangle_number):
            # create a point coordinate.
            point_tmp = random_point(MAIN_WINDOW_SIZE[0], MAIN_WINDOW_SIZE[1])
            # create a pygame.Rect object use the above point coordinate.
            rectangle_tmp = pygame.Rect(point_tmp, (50,50))
            # add the pygame.Rect object to the list.
        # define the main loop.
        while True:
            # fill the main window background color to gray.
            # draw the big red rectangle on the main window screen. 
            pygame.draw.rect(main_window_screen, pygame.Color('red'), rect_object, 2)
            # process events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                # the below code will move the rectangle by mouse.    
                # if user press down the mouse key.   
                if event.type == pygame.MOUSEBUTTONDOWN:
                    # if the user click the rectangle.
                    if rect_object.collidepoint(event.pos):
                        # set the moving_by_mouse to True.
                        moving_by_mouse = True
                # if user release the mouse key.    
                if event.type == pygame.MOUSEBUTTONUP:
                    # then set the moving_by_mouse to False.  
                    moving_by_mouse = False                    
                # if user move the mouse and the moving_by_mouse is True.
                if event.type == pygame.MOUSEMOTION and moving_by_mouse:
                    # then move the rectangle object.    
            # define the point circle radius.
            point_radius = 6
            # draw each point in the point_list.
            for point in point_list:
                # if the big red rectangle collides the point.
                if rect_object.collidepoint(point):
          , pygame.Color('green'), point, point_radius, 0)
          , pygame.Color('blue'), point, point_radius, 0)
            # draw each small rectangle in the rectangle_list.
            for rectangle in rectangle_list:
                # if the big red rectangle collides the small rectangle.
                if rect_object.colliderect(rectangle):
                    pygame.draw.rect(main_window_screen, pygame.Color('green'), rectangle, 2)
                    pygame.draw.rect(main_window_screen, pygame.Color('blue'), rectangle, 1)        
    if __name__ == '__main__':

2. The Pygame Rectangle Collision Example Demo Video.

  1. The demo video URL is

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.