Home >Backend Development >Python Tutorial >How to Draw Rectangles in Pygame Using `pygame.draw.rect`?
In Python's Pygame library, creating rectangles is an essential task for developing games.
For Pygame versions like 3.2, drawing rectangles involves using the pygame.draw.rect function. Here's how you can achieve this:
<code class="python">import pygame, sys from pygame.locals import *</code>
<code class="python">pygame.init()</code>
<code class="python">DISPLAY=pygame.display.set_mode((500,400),0,32)</code>
<code class="python">WHITE=(255,255,255) BLUE=(0,0,255)</code>
<code class="python">DISPLAY.fill(WHITE)</code>
<code class="python">pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))</code>
<code class="python">while True: # Handle events for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() # Update the display pygame.display.update()</code>
This sample code creates a white window with dimensions of 500 pixels wide and 400 pixels high and places a blue rectangle with coordinates (200, 150) and dimensions of 100 pixels wide and 50 pixels high.
The above is the detailed content of How to Draw Rectangles in Pygame Using `pygame.draw.rect`?. For more information, please follow other related articles on the PHP Chinese website!