What a rect is and why you need one

A rect in Pygame is a rectangular shape object that stores four pieces of information: the x position, y position, width, and height. Pygame uses rects for two main reasons: to draw visible rectangles on your screen, and to track where things are so you can detect collisions, clicks, and other interactions.

When you create a rect, you are creating a data container that Pygame can use to position and draw shapes. The rect itself does not appear on screen until you tell Pygame to draw it. This separation between the data (the rect) and the visual output (the drawn rectangle) is what makes rects so useful — you can move a rect around, check if it overlaps with other rects, and only draw it when you need to see it.

Key Takeaways

  • Create a rect using pygame.Rect(x, y, width, height), where x and y are the top-left corner position and width and height are in pixels.
  • Draw a rect to the screen with pygame.draw.rect(surface, color, rect), where surface is your game window and color is an RGB tuple like (255, 0, 0) for red.
  • Change a rect's position by modifying its x and y attributes directly, or use built-in methods like rect.center = (500, 300) to position by center instead of corner.
  • Check if two rects overlap using rect1.colliderect(rect2), which returns True or False without needing to draw anything.
  • A rect exists in your code whether or not you draw it, so you can track invisible rects for game logic like detecting when the player clicks a button.

Creating your first rect

To create a rect, use the pygame.Rect() function with four numbers: x position, y position, width, and height. The x and y are measured in pixels from the top-left corner of your game window. Width and height are also in pixels.

Here is a working example:

import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) player_rect = pygame.Rect(100, 150, 50, 75)

This creates a rect named player_rect that starts at pixel position 100 from the left and 150 from the top, with a width of 50 pixels and a height of 75 pixels. The rect now exists in your code, but nothing appears on screen yet because you have not drawn it.

Drawing the rect on screen

To make the rect visible, use pygame.draw.rect() inside your game loop. This function takes three required arguments: the surface to draw on (usually your screen), the color, and the rect object itself.

Colors in Pygame are RGB tuples — three numbers from 0 to 255 representing red, green, and blue. For example, (255, 0, 0) is pure red, (0, 255, 0) is pure green, and (255, 255, 255) is white.

import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() running = True player_rect = pygame.Rect(100, 150, 50, 75) while running:   for event in pygame.event.get():     if event.type == pygame.QUIT:       running = False   screen.fill((0, 0, 0))   pygame.draw.rect(screen, (255, 0, 0), player_rect)   pygame.display.flip()   clock.tick(60) pygame.quit()

The screen.fill((0, 0, 0)) line clears the screen to black each frame. Without it, every rect you draw stays on screen and creates a trail. The pygame.draw.rect() line draws your red rectangle. The pygame.display.flip() updates the window to show what you just drew. The clock.tick(60) limits the loop to 60 frames per second.

Moving a rect by changing its position

Once you have created a rect, you can move it by changing its x and y attributes. These represent the pixel position of the top-left corner. You can also use built-in position attributes like center, topleft, midtop, and others to position the rect differently.

player_rect.x = 200 player_rect.y = 250

This moves the rect so its top-left corner is at pixel 200 from the left and 250 from the top. If you want to position the rect by its center instead, use:

player_rect.center = (400, 300)

This places the center of the rect at pixel position (400, 300). Other useful position attributes include topleft, topright, bottomleft, bottomright, midtop, midbottom, midleft, and midright. Each one positions the rect by a different point on its edge or corner.

Detecting collisions between rects

One of the most useful things about rects is collision detection. The colliderect() method checks whether two rects overlap. It returns True if they touch or overlap, and False if they do not.

player_rect = pygame.Rect(100, 100, 50, 50) enemy_rect = pygame.Rect(120, 110, 40, 40) if player_rect.colliderect(enemy_rect):   print("Collision detected!")

You can use this inside your game loop to check collisions every frame. For example, if the player rect collides with an enemy rect, you might subtract health, end the game, or trigger a sound. Collision detection works on invisible rects too — you do not have to draw a rect to check if it overlaps with another rect.

Other collision methods include collidepoint(), which checks if a single pixel position (like a mouse click) is inside the rect, and colliderect() for rect-to-rect collisions. There is also collidelist() and collidelistall() if you need to check one rect against many rects at once.

Common mistakes and how to fix them

The most common mistake is forgetting to draw the rect inside the game loop. If you create a rect before the loop and draw it once before the loop starts, it will disappear as soon as the loop runs because screen.fill() clears everything. Always call pygame.draw.rect() inside the loop, after screen.fill() and before pygame.display.flip().

Another mistake is confusing the rect object with the drawn rectangle. The rect is just data — four numbers. Drawing it is a separate action. You can have a rect that you never draw, and you can draw the same rect multiple times in different colors or positions by copying it.

A third mistake is using the wrong color format. Pygame expects RGB tuples with three integers from 0 to 255. If you use a string like "red" or a single number, Pygame will crash. Always use a tuple: (red, green, blue).

Frequently Asked Questions

Can I draw a rect with a border instead of filled?

Yes. Add a fifth argument to pygame.draw.rect() called width. If width is 0 or omitted, the rect is filled. If width is a number like 2 or 5, only the border is drawn with that thickness in pixels. Example: pygame.draw.rect(screen, (255, 0, 0), player_rect, 2) draws a red border 2 pixels thick.

How do I rotate a rect?

Rects in Pygame cannot be rotated — they are always axis-aligned rectangles. If you need a rotated shape, you must draw a rotated image or use a different shape like a polygon. For most games, axis-aligned rects are sufficient for collision detection even if your visual sprite is rotated.

What is the difference between rect.x and rect.left?

They are the same thing. rect.x and rect.left both refer to the x position of the left edge. Pygame provides both names for convenience. Similarly, rect.y and rect.top are the same, and rect.right is the x position of the right edge.

Can I create a rect from an image?

Yes. When you load an image with pygame.image.load(), you can get its rect by calling .get_rect() on it. Example: image = pygame.image.load("player.png") then image_rect = image.get_rect(). This rect has the same width and height as the image and starts at position (0, 0).

Do I have to draw a rect to use it for collisions?

No. A rect is just data. You can create invisible rects for buttons, invisible walls, or detection zones and check collisions with them without ever drawing them to the screen. This is useful for game logic that does not need to be visible.