I have a function and want to use time.sleep(2) everytime an attack happens, but what ends up happening is the time.sleep(2) will work for the first player and enemy attack, then the program will print the rest of the attacks instantly. Does anyone know what I'm doing wrong?

def attack(player, enemy):
    firstAtt = random.randint(1, 2)#Player = 1, Enemy = 2. Checks to see who goes first.
    print("BATTLE!")
    print()
    print("You have {0}HP, while the {1} has {2}HP.".format(player["hp"], enemy["name"], enemy["hp"]))
    if firstAtt == 1:
        time.sleep(2)
        while player["hp"] > 0 and enemy["hp"] > 0:
            dmg = player["att"]()
            enemydmg = enemy["att"]()
            enemy["hp"] = enemy["hp"] - dmg
            print("You have dealt {0} damage to the {1}!".format(dmg, enemy["name"]))
            if enemy["hp"] <= 0:
                print()
                print("You have killed the {0}".format(enemy["name"]))
                print("You have {0}HP left.".format(player["hp"]))
                break

            player["hp"] = player["hp"] - enemydmg
            print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemydmg))
            if player["hp"] <= 0:
                print()
                print("The {0} has killed you!".format(enemy["name"]))
                break

Recommended Answers

All 3 Replies

You call Time.sleep once before the loop. Inside the loop you keep making attacks without calling Time.sleep. For that reason your program will pause once before the loop. Inside the loop it will keep making attacks without pausing.

But time.sleep() is outside the loop, if I try putting it before the if statement it just does the same thing. Even if I put the time.sleep() here, it still prints out the first two attacks, then the program says not responding, after about 5 seconds it all prints out at once.

def attack(player, enemy):
    firstAtt = random.randint(1, 2)#Player = 1, Enemy = 2. Checks to see who goes first.
    print("BATTLE!")
    print()
    print("You have {0}HP, while the {1} has {2}HP.".format(player["hp"], enemy["name"], enemy["hp"]))
    if firstAtt == 1:
        while player["hp"] > 0 and enemy["hp"] > 0:
            dmg = player["att"]()
            enemydmg = enemy["att"]()
            enemy["hp"] = enemy["hp"] - dmg #player["att"]
            print("You have dealt {0} damage to the {1}!".format(dmg, enemy["name"]))
            time.sleep(2)
            if enemy["hp"] <= 0:
                print()
                print("You have killed the {0}".format(enemy["name"]))
                print("You have {0}HP left.".format(player["hp"]))
                break
            player["hp"] = player["hp"] - enemydmg #enemy["att"]
            print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemydmg))
            time.sleep(2)
            if player["hp"] <= 0:
                print()
                print("The {0} has killed you!".format(enemy["name"]))
                break

Threading may solve your problem, here is an example:

'''threading_Timer1_sound.py
threading allows other program segments to run at the same time
threading.Timer() forms an interruptable sleep() function
t = threading.Timer(interval, function, args=[], kwargs={})
t.start()   starts the thread
t.cancel()  stops the thread
t.isAlive() checks active thread
winsound.Beep(frequency, duration) needs Windows OS
'''

import threading
import winsound

def action1():
    print("We are being attacked!")

def alarm():
    # 500 hz sound for 9 seconds
    winsound.Beep(500, 9000)

def action2():
    print("Wake up our troops!")

def action3():
    print("Our troops are ready!")

# after 0.1 seconds do action1
t1 = threading.Timer(0.1, action1)
# after 3.5 seconds do action2
# and after 10.0 seconds do action3
t2 = threading.Timer(3.5, action2)
t3 = threading.Timer(10.0, action3)

# this will wake the troops after action2 has been called
t4 = threading.Timer(4.5, alarm)

t1.start()
t2.start()
t3.start()
t4.start()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.