In Python, you can draw a rectangle on a Pygame display by utilizing the pygame.draw.rect function.
The syntax for pygame.draw.rect is as follows:
pygame.draw.rect(surface, color, rect, width=0)
where:
Here's an example of how to draw a blue rectangle in a Pygame window:
<code class="python">import pygame # Initialize Pygame pygame.init() # Set up the display DISPLAY = pygame.display.set_mode((500, 400), 0, 32) # Create the colors WHITE = (255, 255, 255) BLUE = (0, 0, 255) # Fill the display with white DISPLAY.fill(WHITE) # Draw the rectangle pygame.draw.rect(DISPLAY, BLUE, (200, 150, 100, 50)) # Update the display pygame.display.update() # Keep the window open until the user quits while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit()</code>
This code creates a 500x400 pixel white display. It then draws a blue rectangle with coordinates (200, 150) and dimensions (100, 50).
The above is the detailed content of How to Draw a Rectangle in Python Using Pygame?. For more information, please follow other related articles on the PHP Chinese website!