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