samedi 5 février 2022

Creating a continuous but controlled random variable that can be used in a while loop

I have the following code to create traffic lights/stop lights in pygame. I want the time at which the lights stay green and red to be random, changing every time the lights have done a full rotation of green, amber and red. Here's the code I have so far:

count = 0
iteration = 0

greenTime = random.randint(2,4)
redTime = greenTime + 1 + random.randint(1,4)

running = True
while running:
    greenColor = brightGreen
    amberColor = dimAmber
    redColor = dimRed
    if count >= greenTime:
        greenColor = dimGreen
        amberColor = brightAmber
        redColor = dimRed
    if count >= greenTime + 1:
        greenColor = dimGreen
        amberColor = dimAmber
        redColor = brightRed
    if count >= redTime:
        count = 0
        iteration += 1
    count += 0.0006

Does anybody know how I would randomise greenTime and redTime value with every light rotation? I have tried including the values within the while loop, but that just produces a continous random value that causes a strobe effect of the lights.




Aucun commentaire:

Enregistrer un commentaire