Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Deleting/Duplicating an instance of a class when there are many

I am trying to delete the dots outside of the safe zone but so far the only solution I have found is to not draw them, which is not what I want since using this method they still exist. Is there anyway to delete the specific dots outside of this region? Is there also a way to create a duplicate of an instance?

import pygame
import sys
import random
pygame.init()
win = pygame.display.set_mode((800,600))
pygame.display.set_caption("Simulation")

class Dot:
    def __init__(self):
        self.spawnX = random.randrange(0, 800)
        self.spawnY = random.randrange(0, 600)
        self.r = random.randrange(0, 256)
        self.g = random.randrange(0, 256)
        self.b = random.randrange(0, 256)

        self.vel = [None] * 0
        self.spd = random.random()
        self.vel.append(self.spd)
        self.vel.append(self.spd * -1)
        self.fertility = random.randrange(0, 3)

def safeZone():
        #Draws a top rectangle
    pygame.draw.rect(win, (50,205,50), (0, 0, 800, 100))
        
def drawdot(dot):
    width = 10
    height = 10
    pygame.draw.rect(win, (dot.r, dot.g, dot.b), (dot.spawnX, dot.spawnY, width, height))

def population(dots):
    for dot in dots:
        dot.spawnX += random.choice(dot.vel)
        dot.spawnY += random.choice(dot.vel)
        if dot.spawnX >= 800:
            dot.spawnX -= 5
        if dot.spawnY >= 600:
            dot.spawnY -= 5
        if dot.spawnX <= 0:
            dot.spawnX += 5
        if dot.spawnY <= 0:
            dot.spawnY += 5
        if dot.spawnX >= 0 and dot.spawnY >= 100:
            pass  #Here I want to delete the objects outside of this region
        drawdot(dot)

alldots = [Dot() for _ in range(1000)]

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    win.fill((255, 255, 255))
    safeZone() # Always draw dots after safe zone
    population(alldots)
    pygame.display.update()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"not draw them" just means removing them from the container they are in. Use pygame.Rect.colliderect or pygame.Rect.contains to test whether a dot object is in the safe zone. Remove the dots form the list alldots that are not in the safe zone (see How to remove items from a list while iterating?):

def population(dots):

    safe_zone_rect = pygame.Rect(0, 0, 800, 100)
    
    for dot in dots:
        dot.spawnX += random.choice(dot.vel)
        dot.spawnY += random.choice(dot.vel)
        if dot.spawnX >= 800:
            dot.spawnX -= 5
        if dot.spawnY >= 600:
            dot.spawnY -= 5
        if dot.spawnX <= 0:
            dot.spawnX += 5
        if dot.spawnY <= 0:
            dot.spawnY += 5
        if dot.spawnX >= 0 and dot.spawnY >= 100:
            pass  #Here I want to delete the objects outside of this region

        dot_rect = pygame.Rect(dot.spawnX, dot.spawnY, 10, 10)
        # if not safe_zone_rect.colliderect(dot_rect):
        if not safe_zone_rect.contains(dot_rect):
            dots.remove(dot)

        drawdot(dot)

pygame.Rect.colliderect tests whether 2 rectangles intersect. pygame.Rect.contains
tests whether a rectangle lies completely within another rectangle.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading