在pygame中绘制多行多列的矩形可以通过以下步骤实现:
import pygame
import sys
pygame.init()
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("绘制多行多列的矩形")
rows = 5
cols = 5
rect_width = width // cols
rect_height = height // rows
rectangles = []
for row in range(rows):
row_rects = []
for col in range(cols):
rect = pygame.Rect(col * rect_width, row * rect_height, rect_width, rect_height)
color = (255, 255, 255) # 设置矩形的颜色,这里使用白色
row_rects.append((rect, color))
rectangles.append(row_rects)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0, 0, 0)) # 清空屏幕
# 绘制矩形
for row_rects in rectangles:
for rect, color in row_rects:
pygame.draw.rect(screen, color, rect)
pygame.display.flip() # 更新屏幕显示
通过以上步骤,就可以在pygame中绘制多行多列的矩形了。你可以根据实际需求调整行数、列数、矩形的宽度和高度,以及矩形的颜色。
领取专属 10元无门槛券
手把手带您无忧上云