【问题标题】:How can i creat a multiple rect by pygame? [duplicate]如何通过 pygame 创建多个矩形? [复制]
【发布时间】:2026-02-20 07:45:01
【问题描述】:

我想创造一个像乒乓球这样的游戏,但只有一个球员来自下边,多个盒子来自上边,当球击中盒子时,盒子会消失。我不记得那个游戏的名字,但我不知道如何创建多个矩形。我可以很容易地画出来

【问题讨论】:

  • 你可以看到这个post

标签: python pygame draw rect


【解决方案1】:

使用pygame.draw.rect画一个矩形:

pygame.draw.rect(screen, color, (x, y, width, height))

使用嵌套循环绘制多个矩形的位置

rectwidth = 40
rectheight = 40
rectdist = 10

block_positions = []
for i in range(10):
    for j in range(2)
        x = 100 + i * (rectdist + rectwidth) 
        y = 100 + j * (rectdist + rectheight)
        block_positions.append((x, y)) 

循环绘制矩形:

for x, y in block_positions:
    pygame.draw.rect(screen, (255, 255, 255), (x, y, rectwidth, rectheight))

【讨论】: