How To Align Pygame Rectangle Horizontally Or Vertically

This example will tell you how to align a rectangle in the pygame main window screen horizontally or vertically. When you press the different keyboard keys, the rectangle will be aligned to the main window screen’s left, right, top, bottom borders,  center horizontally or center vertically.

1. Align The Rectangle In Pygaem Window Horizontally Or Vertically Example.

  1. Create a pygame.Rect object and draw it to the center of the main window’s screen.
  2. Then you should write source code to catch the user event, and then change the pygame.Rect object’s left, right, top, bottom, centerx or centery attributes value accordingly to align the rectangle to the main window screen.
  3. Create a python file with the name, then copy the below source code into it and save.
    Created on Feb 23, 2022
    @author: Jerry Zhao
    import pygame, sys
    # define the pygame main window size.
    MAIN_WINDOW_SIZE = (600,300)
    In this function, you can align a pygame rectangle to the main window border or center by press the keyboard keys.
    Press the left arrow key: align the rectangle to the main window's left border.
    Press the right arrow key: align the rectangle to the main window's right border.
    Press the up arrow key: align the rectangle to the main window's top border.
    Press the down arrow key: align the rectangle to the main window's bottom border.
    Press the key c: align the rectangle to the main window's horizontal center.
    Press the key m: align the rectangle to the main window's vertical center.
    def align_rectangle_shape_by_keyboard_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('Use Keyboard To Align Rectangle In Pygame Example')
        # define the rectangle's top left point coordinate.
        rect_left = 100    
        rect_top = 100
        # get the main window width and height.
        main_window_width = main_window_screen.get_width()
        main_window_height = main_window_screen.get_height()
        # calculate the rectangle object's width and height.
        rect_width = main_window_width - 2 * rect_left
        rect_height = main_window_height - 2 * rect_top
        # create the pygame.Rect object with the above values.
        rect_object = pygame.Rect(rect_left, rect_top, rect_width, rect_height)
        # the main loop.
        while True:
            for event in pygame.event.get():
                # if close the pygame application window.
                if event.type == pygame.QUIT:
                # if user press the keyboard keys.
                if event.type == pygame.KEYDOWN:
                    # if press the left arrow key.
                    if event.key == pygame.K_LEFT:
                        # set the rectangle object's left attribute to 0.
                        rect_object.left = 0
                    # if press the right arrow key.
                    if event.key == pygame.K_RIGHT:
                        # set the rectangle object's right attribute to main_window_width.
                        rect_object.right = main_window_width
                    # if press the up arrow key.   
                    if event.key == pygame.K_UP:
                        # set the rectangle object's top attribute to 0.
               = 0
                    # if press the down arrow key.
                    if event.key == pygame.K_DOWN:
                        # set the rectangle object's bottom attribute to main_window_height.
                        rect_object.bottom = main_window_height       
                    # if press the c key.
                    if event.key == pygame.K_c:
                        # set the rectangle object's bottom attribute to main_window_height. 
                        rect_object.centerx = main_window_width//2
                    # if press the m key.
                    if event.key == pygame.K_m:
                        # set the rectangle object's bottom attribute to main_window_height.
                        rect_object.centery = main_window_height//2
            # set the main window background color to green.
            main_window_backgroud_color = pygame.Color('green')
            # fill the rectangle color to red.
            rect_color = pygame.Color('red')
            pygame.draw.rect(main_window_screen, rect_color, rect_object)
            # update the screen.
    if __name__ == '__main__':

2. Example Demo Video.

  1. You can see the demo video on the URL

Leave a Comment

Your email address will not be published. Required fields are marked *

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