简体   繁体   中英

How to slow down the speed exec of a func

When I apply my code the "attaquedeplac" func is running too fast: I used the after func but "attaquedeplac" ran 8th times, stop and wait 1000ms and ran 8th times again and again. Here the complete code, but my problem is in this part :

            for a,b in attaque.items():
                #a = nom de la variable , b = objet Tkinter
                x = liste[1]
                y = liste[2]
                ajoutx = listedeco[0]
                ajouty = listedeco[1]
                compteur = 0
                def attaquedeplac():
                    global x,y,ajoutx,ajouty,compteur
                    x =x + (compteur * ajoutx)
                    y =y + (compteur * ajouty)
                    Fond.coords(b, x , y , x+ajoutx, y+ajouty)
                    compteur +=1
                    print("Tout vas bien {}".format(compteur))

                while x>40 and x<980 and y > 40 and y < 680:
                        attaquedeplac()
                        fenetre.after(1000,attaquedeplac)

Output:

Tout vas bien 1
Tout vas bien 2
Tout vas bien 3
Tout vas bien 4
Tout vas bien 5
Tout vas bien 6
Tout vas bien 7
Tout vas bien 8
<here a step>
Tout vas bien 9
Tout vas bien 10
Tout vas bien 11
Tout vas bien 12
Tout vas bien 13
Tout vas bien 14
Tout vas bien 15
Tout vas bien 16
<the other step>
Tout vas bien 17
Tout vas bien 18
Tout vas bien 19
Tout vas bien 20
Tout vas bien 21
Tout vas bien 22
Tout vas bien 23
Tout vas bien 24
<the other step>
Tout vas bien 25
Tout vas bien 26
Tout vas bien 27
Tout vas bien 28

I tried to make the same concept of the arrow like in Zelda 1 with slow progress, which is visible to the human eyes).

Try this

import time
time.sleep(1)

Where 1 = 1 second = 1000ms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM