【问题标题】:How can I delete drawn rectangles in pygame?如何在pygame中删除绘制的矩形?
【发布时间】:2020-09-02 16:03:04
【问题描述】:

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

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

【问题讨论】:

    标签: python pygame destroy


    【解决方案1】:

    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()
    

    【讨论】:

    • 谢谢!那解决了它。我也遇到了运行时问题,因为它运行不顺畅,我想知道为什么。所以你基本上帮助了我解决我的两个困难。再次感谢。
    • 对了,你为什么要乘以 1000?
    • @GeorgeAvrabos 1 秒 == 1000 毫秒。 pygame.time.get_ticks() 返回毫秒数(答案中提到)
    • 啊好吧我没看到毫秒抱歉。
    【解决方案2】:

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

    【讨论】:

    • 是的,但这不会超载吗?因为那样我的屏幕上就会出现数百个矩形。
    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多