0
votes

I have a program like this. It works well bout there is a problem! By pressing the 'Down' key it shows a circle on the screen for 5 seconds and also by 'Space-down' and 'Space-up' does something else (calculates pressing time). But I don't want it to 'calculate' when is showing the circle. in the other words I don't want it to get keyboard events (or does nothing if gets keyboard events) when is showing the circle. for this purpose I added the 'p' as a condition. it somehow works! I mean during the circle show the program doesn't calculate even if you press 'Space' key. But the problem is when the circle show gets over. It does calculations just as like as you pressed the 'Space' key after circle show time. I mean if you press the 'Space' key during circle show time, it does calculations immediately after when the circle show gets over. Its like that the program gets key events even during the circle show, but waits until when the condition (p == 0 ) becomes true. Please help me to fix this situation.

p = 0   
gameloop = True
while gameloop:

for event in pygame.event.get():

    if (event.type == pygame.QUIT):
            gameloop = False

    if event.type == pygame.KEYDOWN:
        if (event.key == pygame.K_DOWN)  :

                p += 1
                start_time = time.time()
                end_time = start_time + 5
                while end_time > time.time():
                    # showing a circle on display 
                p -= 1 

    if (event.type == pygame.KEYDOWN):
        if (event.key == pygame.K_SPACE)  and (p == 0)  :

            # Do something

    if (event.type == pygame.KEYUP)  :
        if (event.key == pygame.K_SPACE) and (p == 0) :

            # Do somthing
1
What you mean by "showing sth on display"? - Nitheesh MN
showing something, I mean for example drawing a circle. - Ali BH

1 Answers

0
votes

I found out that if you press 'Space' key during circle show time it doesn't operates in that moment but after when the circle show gets over it operates immediately. It likes that you press 'Space' instantly after circle show time. therefore it takes just as long as system operate time (as I said this program calculates 'Space' key pressing time : space_up_time - space_down_time) and when you press 'Space' key during circle show time, every time it returns a time about 0.002 second which is system operate time. So to solve my problem I added another condition : h + 0.01 < time.time(). Now it works perfectly! But let me know if there is another way because one could press keys very fast maybe faster than 0.002 second !!!

start_time = time.time()
end_time = start_time + ex
while end_time > time.time():
        # Drawing a circle
h = time.time()

      if (event.type == pygame.KEYDOWN):

        if (event.key == pygame.K_SPACE)  and ( h+0.01 < time.time())  :
               # Do calculations