Weird Loop Behaviour

Y

Yigit Turgut

Hi,

In the following code, I am trying to run "black" screen for 3 seconds
and respectively 2 seconds "white" screen. Black routine takes 3
seconds and white 2 seconds, 2 x black + white = 8 seconds which
should be the expected value but when I run it I get black-white-black-
white instead of black-white-black. Couldn't figure out what is
wrong thus sharing the code as well ;

white = False
while(end<8.00):
end = time.time() - start
if white:
screen.fill((255, 255, 255))
time.sleep(2)
else:
screen.fill((0, 0, 0))
time.sleep(3)
white = not white
pygame.display.update()
pygame.quit()
 
M

MRAB

Hi,

In the following code, I am trying to run "black" screen for 3 seconds
and respectively 2 seconds "white" screen. Black routine takes 3
seconds and white 2 seconds, 2 x black + white = 8 seconds which
should be the expected value but when I run it I get black-white-black-
white instead of black-white-black. Couldn't figure out what is
wrong thus sharing the code as well ;

white = False
while(end<8.00):
end = time.time() - start
if white:
screen.fill((255, 255, 255))
time.sleep(2)
else:
screen.fill((0, 0, 0))
time.sleep(3)
white = not white
pygame.display.update()
pygame.quit()

Could it be because you're setting 'end' after testing it?

It might be simpler as:

while time.time() - start < 8:

Also, should it really be sleeping before updating the display? I
would've thought that it should be sleeping _after_ updating the
display.
 
E

Emile van Sebille

On 1/20/2012 12:47 PM Yigit Turgut said...
Hi,

In the following code, I am trying to run "black" screen for 3 seconds
and respectively 2 seconds "white" screen. Black routine takes 3
seconds and white 2 seconds, 2 x black + white = 8 seconds which
should be the expected value but when I run it I get black-white-black-
white instead of black-white-black. Couldn't figure out what is
wrong thus sharing the code as well ;

white = False
while(end<8.00):
end = time.time() - start
you're setting end's value before the display happens, so while tests
the values 0,3,5 before getting after the fourth pass 8. Move this
after to white = note white and I suspect you'll be OK.

HTH,

Emile
 
A

Arnaud Delobelle

Hi,

In the following code, I am trying to run "black" screen for 3 seconds
and respectively 2 seconds "white" screen. Black routine takes 3
seconds and white 2 seconds, 2 x black + white = 8 seconds which
should be the expected value but when I run it I get black-white-black-
white   instead of black-white-black. Couldn't figure out what is
wrong thus sharing the code as well ;

white = False
    while(end<8.00):
     end = time.time() - start
     if white:
      screen.fill((255, 255, 255))
       time.sleep(2)
     else:
       screen.fill((0, 0, 0))
       time.sleep(3)
     white = not white
     pygame.display.update()
    pygame.quit()

This is cryptic. You'd be better off with something like

black = 0, 0, 0
white = 255, 255, 255
for color, wait in (black, 3), (white, 2), (black, 3):
screen.fill(color)
pygame.display.update()
time.sleep(wait)
pygame.quit()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top