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:
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((500,400),0,32)
WHITE=(255,255,255)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
while True:
# Handle events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# Update the display
pygame.display.update()
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3