如何删除 pygame 中绘制的矩形?

How can I delete drawn rectangles in pygame?

我想在屏幕上随机绘制矩形,但是如果已经绘制的矩形数量超过 3 个,那么我想开始删除“旧的”,即正在绘制的矩形在屏幕上的时间最多。

import pygame
import random
import time
pygame.init()

x_axis = [500, 650, 350, 400]
y_axis = [100, 50, 450, 300]
sq_width = [10, 15, 20, 25, 30]
sec = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
sq_display = []

class Squares:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = width

    def draw(self):
        a = pygame.draw.rect(window, (255, 0, 0), (random.choice(x_axis), random.choice(y_axis), self.width, self.height))
        sq_display.append(a)
        if len(sq_display) > 3:
            sq_display.remove(sq_display[0])
        time.sleep(random.choice(sec))
        return

我每次都尝试将矩形存储到一个变量中并将其附加到列表中,以为稍后我可以将其删除。好吧,它没有用。所以我想知道有没有办法解决我的问题。

存储所有当前绘制的矩形的队列,只要列表超过长度,比如 3,只需使用存储的坐标绘制一个白色矩形(或任何背景颜色)。

draw方法中移除新方块的生成:

class Squares:
    def __init__(self, x, y, width):
        self.x = x
        self.y = y
        self.width = width
        self.height = width

    def draw(self):
        pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, self.width, self.height))

您必须在应用程序循环的每一帧中重新绘制整个场景。绘制方块前清除显示,然后绘制列表中的所有方块,最后更新显示:

while run:
    # [...]  

    a = Squares(random.choice(x_axis), random.choice(y_axis), 
                random.choice(sq_width), random.choice(sq_width))
    sq_display.append(a)
    if len(sq_display) > 3:
        sq_display.remove(sq_display[0])

    window.fill(0)
    for r in sq_display:
        r.draw()
    pygame.display.flip()

如果要保持应用程序响应,则不能将应用程序循环延迟 time.sleep。使用 pygame.time.get_ticks() 获取自 pygame.init() 以来的当前毫秒数,并在经过随机时间后创建一个新方块:

next_square_time = 0
while run:
    # [...]

    current_time = pygame.time.get_ticks()
    if next_square_time <= current_time:
        next_square_time += random.choice(sec) * 1000 

        # create new square
        # [...]

最小示例:

import pygame
import random
pygame.init()

x_axis = [500, 650, 350, 400]
y_axis = [100, 50, 450, 300]
sq_width = [10, 15, 20, 25, 30]
sec = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
sq_display = []

class Squares:
    def __init__(self, x, y, width):
        self.x = x
        self.y = y
        self.width = width
        self.height = width

    def draw(self):
        pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, self.width, self.height))

window = pygame.display.set_mode((800, 600))

next_square_time = 0
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    current_time = pygame.time.get_ticks()
    if next_square_time <= current_time:
        next_square_time += random.choice(sec) * 1000    
        a = Squares(random.choice(x_axis), random.choice(y_axis), random.choice(sq_width))
        sq_display.append(a)
        if len(sq_display) > 3:
            sq_display.remove(sq_display[0])

    window.fill(0)
    for r in sq_display:
        r.draw()
    pygame.display.flip()