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

How to store the outcome of a method

I am trying to draw squares in random positions and random rgb values and I want 1000 of them to be created. The problem I’m facing is that everytime the loop for drawing occurs, it randomizes it all again, is there any way to make this not happen

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

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

def population(size):
    for x in range(size):
        dot()

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(1000)
    pygame.display.update()

pygame.quit()


>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

Create a dot collection, then just draw that dot collection. Now you can update the dot positions separately, and they will redraw in the new positions. Here, I’m having each dot move a random amount in every loop.

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)

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.randrange(-3,4)
        dot.spawnY += random.randrange(-3,4)
        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()

A worthwhile modification is to store the whole rectangle in the object:

...
class Dot:
    def __init__(self):
        self.location = [
            random.randrange(0, 800),
            random.randrange(0, 600),
            10, 10
        ]
        self.color = (
            random.randrange(0, 256),
            random.randrange(0, 256),
            random.randrange(0, 256)
        )
    def move(self, dx, dy ):
        self.location[0] += dx
        self.location[1] += dy

def drawdot(dot):
    pygame.draw.rect(win, dot.color, dot.location)

def population(dots):
    for dot in dots:
        dot.move( random.randrange(-3,4), random.randrange(-3,4) )
        drawdot(dot)
...
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