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

2024-05-16 01:55:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在屏幕上随机绘制矩形,但是如果已经绘制的矩形数量超过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

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


Tags: importself屏幕timeinitdisplaysq绘制
2条回答

存储当前绘制的所有矩形的队列,每当列表长度超过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.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()

相关问题 更多 >