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

时间:2020-09-02 16:03:04

标签: python pygame destroy

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

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

只需将矩形移出显示屏即可。
例如。如果您的显示尺寸为800x600像素,则将矩形X,Y坐标更改为1000,1000。它在屏幕上将不再可见

相关问题