Drawing Rectangles in Pygame
In game development, drawing rectangles is a fundamental skill. In Python, using the Pygame library, drawing rectangles is made straightforward.
Solution
To draw a rectangle in Pygame (3.2), follow these steps:
<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">pygame.display.update()</code>
The pygame.draw.rect function takes the following parameters:
In this example, a blue rectangle with a width of 100 and a height of 50 is drawn at the coordinates (200, 150) on a white display surface.
Additional Resources
For more information on drawing rectangles with Pygame, refer to the official Pygame documentation: https://www.pygame.org/docs/ref/draw.html#pygame.draw.rect
The above is the detailed content of How to Draw a Rectangle in Pygame?. For more information, please follow other related articles on the PHP Chinese website!