Why my program doesnt get delayed for 1 second?

Viewed 83

Im doing a random walker program in python and using Pygame for the graphics.

The program is working fine, however i want that in every for loop at the end of the code it should delay the program for 1 second, so the program completes itself in a matter of seconds, but rather i can see the continous random walk.

So when i add time.sleep(1) at the end of the code, it instead makes the program run slower and it doesnt draw a circle every second:

import pygame
import random
import time


#Initialize the pygame
pygame.init()

#create the screen
screen = pygame.display.set_mode((800,800)) # Width and Height 

startX = 400
startY = 400


def returnRandomNumber():
    options = [1,-1]
    
    random_sample = random.choice(options)

    return random_sample

def returnRandomInt():
    random_sample = random.randint(0,255)

    return random_sample


running = True
while running:
    #update display
    pygame.display.update()


    for x in range(10000):
        pygame.draw.circle(screen, (returnRandomInt(),returnRandomInt(),returnRandomInt()), (startX, startY), 2, 2)
        if startX <= 0:
            startX += 1
        elif startX >= 800:
            startX += -1
        else:
            startX += returnRandomNumber()
        
        if startY <= 0:
            startY += 1
        elif startY >= 800:
            startY += -1
        else:
            startY += returnRandomNumber()
    time.sleep(1)

Why is this ocurring? or how can i make the program to draw a circle and then delay for x seconds and continue running?

2 Answers

Try inserting update and sleep inside your for loop, so your display would update after every iteration:

while running:
#update display
pygame.display.update()


for x in range(10000):
    pygame.draw.circle(screen, (returnRandomInt(),returnRandomInt(),returnRandomInt()), (startX, startY), 2, 2)
    if startX <= 0:
        startX += 1
    elif startX >= 800:
        startX += -1
    else:
        startX += returnRandomNumber()
    
    if startY <= 0:
        startY += 1
    elif startY >= 800:
        startY += -1
    else:
        startY += returnRandomNumber()
    pygame.display.update()
    time.sleep(1)

The time.sleep (1) needs to swap places with the pygame.display.update ()

Code: import pygame import random import time

Initialize the pygame

pygame.init()

create the screen

screen = pygame.display.set_mode((800, 800)) # Width and Height

startX = 400 startY = 400

def returnRandomNumber(): options = [1, -1]

random_sample = random.choice(options)

return random_sample

def returnRandomInt(): random_sample = random.randint(0, 255)

return random_sample

running = True while running: # update display time.sleep(1)

for x in range(10000):
    pygame.draw.circle(screen, (returnRandomInt(), returnRandomInt(), returnRandomInt()), (startX, startY), 2, 2)
    if startX <= 0:
        startX += 1
    elif startX >= 800:
        startX += -1
    else:
        startX += returnRandomNumber()

    if startY <= 0:
        startY += 1
    elif startY >= 800:
        startY += -1
    else:
        startY += returnRandomNumber()
pygame.display.update()
Related