1

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?

Irfan wani
  • 4,084
  • 2
  • 19
  • 34

2 Answers2

2

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)
Lior Pollak
  • 3,362
  • 5
  • 27
  • 48
  • thanks! that actually worked! however i noticed that after some seconds the program crashes, it also happened before but i thought that it was because of the infinite while loop, any ideas? – Bruno Moreno Feb 21 '21 at 07:21
  • You’re drawing a lot of circles - 10000 per iteration, and it doesn’t stop. So you might want to focus on this piece of code and draw less circles- perhaps stop after a few. – Lior Pollak Feb 21 '21 at 07:24
  • i'd imagined that, however before putting the time.sleep it draw thousands of circles before crashing, but after putting time.sleep(1) it draws like 10 circles and then crashes. – Bruno Moreno Feb 21 '21 at 07:28
  • Maybe python lags, but the important thing is that you’re drawing infinite circles, so you’re crashing the script. – Lior Pollak Feb 21 '21 at 07:30
  • Yeah i dont know if it is a python or pygame issue, thanks for your input it worked. Yet i tried one last thing i added a counter variable to the program so that every for loop it adds 1 and when it reached the same number as the range in the for loop. it will stop the program. So i noticed how it the program crashes and it stops drawing, but after a few seconds it closes and for a brief second i can see the other x circles getting drawn before it closes. – Bruno Moreno Feb 21 '21 at 07:38
  • Still, you’re probably drawing too much circles – Lior Pollak Feb 21 '21 at 07:39
  • If this answer helped you, please upvote and accept it with the green v mark :) – Lior Pollak Feb 21 '21 at 07:40
  • done, thanks buddy. any tips or particular project to get better at programming? – Bruno Moreno Feb 21 '21 at 07:47
  • Practice makes perfect :) – Lior Pollak Feb 21 '21 at 07:50
  • Sorry, but your answer will not work on every system because you do not handle the events in the inner loop. – Rabbid76 Feb 21 '21 at 14:45
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()