This article will show you examples of how to move or inflate the pygame rectangle by pressing keys, mouse, or by itself automatically.
1. Move Pygame Rectangle By Keys Or Mouse Example.
- You can see this example’s demo video from the URL https://youtu.be/-9KVhjMJpEI.
- In this example you can move the red rectangle by pressing the up, down, left, right arrow keys, or using the mouse.
- The method pygame.Rect.move_ip(x, y) can move the pygame rectangle with the provided x-axis offset and y-axis offset.
- Use the condition event.type == pygame.KEYDOWN to check and process the keypress event.
- Use the condition event.type == pygame.MOUSEBUTTONDOWN, event.type == pygame.MOUSEBUTTONUP, event.type == pygame.MOUSEMOTION to check and process mouse events.
- Below is the example source code.
''' Created on Feb 23, 2022 @author: Jerry Zhao ''' import pygame, sys # define the pygame main window size. MAIN_WINDOW_SIZE = (600,300) ''' This function will implement moving the rectangle by pressing keys or mouse. ''' def move_rectangle_by_keys_or_mouse_example(): # initialize pygame application. pygame.init() # 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('Move Pygame Rectangle By Keys Or Mouse 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) moving_by_mouse = False # the main loop. while True: for event in pygame.event.get(): # if close the pygame application window. if event.type == pygame.QUIT: pygame.quit() sys.exit(0) # the below code will move the rectangle by keys. # 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.move_ip(-10, 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.move_ip(10, 0) # if press the up arrow key. if event.key == pygame.K_UP: # set the rectangle object's top attribute to 0. rect_object.move_ip(0, -10) # 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.move_ip(0, 10) # 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. rect_object.move_ip(event.rel) # set the main window background color to green. main_window_backgroud_color = pygame.Color('green') main_window_screen.fill(main_window_backgroud_color) # fill the rectangle color to red. rect_color = pygame.Color('red') pygame.draw.rect(main_window_screen, rect_color, rect_object) # update the screen. pygame.display.flip() if __name__ == '__main__': move_rectangle_by_keys_or_mouse_example()
2. Move Pygame Rectangle Automatically Example.
- You can see this example’s demo video from the URL https://youtu.be/rOKIb7REL1E.
- In this example, the red square will move automatically in a regular direction, when it reaches the window border it will bounce back in the direction and continue to move.
- Below is the example source code.
''' Created on Feb 23, 2022 @author: Jerry Zhao ''' import pygame, sys # define the pygame main window size. MAIN_WINDOW_SIZE = (600,300) ''' This function will implement moving the rectangle automatically feature. ''' def move_rectangle_automatically_example(): # initialize pygame application. pygame.init() # 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('Move Pygame Rectangle Automatically 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 = 50 rect_height = 50 # create the pygame.Rect object with the above values. rect_object = pygame.Rect(rect_left, rect_top, rect_width, rect_height) move_steps = [6, 6] # the main loop. while True: for event in pygame.event.get(): # if close the pygame application window. if event.type == pygame.QUIT: pygame.quit() sys.exit(0) # move the pygame rectangle object. rect_object.move_ip(move_steps) # if the moving rectangle reach the pygame application borders, # then the horizontal or vertical move step will be changed to the opposite direction. if rect_object.left < 0 or rect_object.right > main_window_width: move_steps[0] *= -1 if rect_object.top < 0 or rect_object.bottom > main_window_height: move_steps[1] *= -1 # set the main window background color to green. main_window_backgroud_color = pygame.Color('green') main_window_screen.fill(main_window_backgroud_color) # fill the rectangle color to red. rect_color = pygame.Color('red') pygame.draw.rect(main_window_screen, rect_color, rect_object) clock = pygame.time.Clock() clock.tick(10) # update the screen. pygame.display.update() if __name__ == '__main__': move_rectangle_automatically_example()
3. Inflate Pygame Rectangle By Keys Example.
- This example demo video URL is https://youtu.be/GlI7AY5_Vww.
- In this example, when you press the key p, the red rectangle will grow bigger, when you press the key m, the red rectangle will become smaller.
- Below is the example source code.
''' Created on Feb 23, 2022 @author: Jerry Zhao ''' import pygame, sys # define the pygame main window size. MAIN_WINDOW_SIZE = (600,300) ''' This function will implement inflating the rectangle by pressing keys. ''' def inflate_rectangle_by_keys_example(): # initialize pygame application. pygame.init() # 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('Inflate Pygame Rectangle By Keys 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: pygame.quit() sys.exit(0) # the below code will inflate the rectangle by pressing keys. # if user press the keyboard keys. if event.type == pygame.KEYDOWN: # the below code will inflate the rectangle by keys. # if press the p key. if event.key == pygame.K_p: # inflate up the rectangle size. rect_object.inflate_ip(10, 10) # if press the m key. if event.key == pygame.K_m: # inflate down the rectangle size. rect_object.inflate_ip(-10, -10) # set the main window background color to green. main_window_backgroud_color = pygame.Color('green') main_window_screen.fill(main_window_backgroud_color) # fill the rectangle color to red. rect_color = pygame.Color('red') pygame.draw.rect(main_window_screen, rect_color, rect_object) # update the screen. pygame.display.flip() if __name__ == '__main__': inflate_rectangle_by_keys_example()
4. Inflate Pygame Rectangle Automatically Example.
- The example demo video URL is https://youtu.be/00Ua8G7ZDIk.
- In this example, the red rectangle will inflate larger automatically, when it reaches the game window border, it will inflate smaller, when the rectangle height is small than 10 px, then it will inflate larger again.
- Below is the example source code.
''' Created on Feb 23, 2022 @author: Jerry Zhao ''' import pygame, sys # define the pygame main window size. MAIN_WINDOW_SIZE = (600,300) ''' This function will implement inflating the rectangle automatically feature. ''' def inflate_rectangle_automatically_example(): # initialize pygame application. pygame.init() # 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('Inflate Pygame Rectangle Automatically 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) inflate_steps = [10, 10] # the main loop. while True: for event in pygame.event.get(): # if close the pygame application window. if event.type == pygame.QUIT: pygame.quit() sys.exit(0) # inflate up the rectangle size. rect_object.inflate_ip(inflate_steps[0], inflate_steps[1]) # if the inflated rectangle's width is bigger than the main window width, # or it height is smaller than 10, then inflate to the opposite direction. if rect_object.width > main_window_width or rect_object.height < 10: inflate_steps[0] *= -1 inflate_steps[1] *= -1 # set the main window background color to green. main_window_backgroud_color = pygame.Color('green') main_window_screen.fill(main_window_backgroud_color) # fill the rectangle color to red. rect_color = pygame.Color('red') pygame.draw.rect(main_window_screen, rect_color, rect_object) clock = pygame.time.Clock() clock.tick(10) # update the screen. pygame.display.update() if __name__ == '__main__': inflate_rectangle_automatically_example()