Threading in a combat test python

I am trying to set up a timer for the player’s input. If they don’t enter in the direction in which to dodge in time, it tells them they were too slow and exits the function. The thread is not starting and I’m not sure why. Thank you for the help

import random
import time
from threading import Thread



attack = 6
health = 5
dodge = False
bhealth = 5
damage = 0
battack = 2
bdodge = False
dodge1=None

def check():
  global dodge1
  print("Hi")
  time.sleep(2)
  if dodge1 != "jump" and dodge1 != "duck" and dodge1 != "roll" and dodge1 != "slide":
    print("You took damage")
    return

answer = None
attackdirection = ["Up", "Down", "Left", "Right"]
dodgedirection = ["jump", "duck", "slide", "roll"]
def criticalhit():
  global health
  global damage
  print("\033[31mCRITICAL HIT!\033[0m")
  damage = random.randint(3, 4)
  health = health - damage
  print("You took", damage, "damage")
def hit():
  global health
  global damage
  print("You were hit by the ogre!")
  damage = random.randint(1, 2)
  print("You took", damage, "damage.")
  health = health - damage
def enemyattack():
  global attackdirection
  global dodge
  global bdodge
  global health
  global battack
  global damage
  global dodgedirection
 
  if bdodge is True:
    bdodge = False
    return
  attackdirection1 = random.choice(attackdirection)
  if attackdirection1 == "Up":
    print("The ogre lifts his weapon\033[31m up\033[0m.")
  if attackdirection1 == "Down":
    print("The ogre swings his weapon in a \033[31m down\033[0mward arc")
  if attackdirection1 == "Left":
    print("You see the ogre's club hurtling towards your\033[31m left\033[0m.")
  if attackdirection1 == "Right":
    print("The ogre lashes out to the\033[31m right\033[0m.")
  print("Up=Duck, Down=Jump, Left=Slide, Right=Roll")
  dodge1 = input("Pick a direction to dodge:").lower()
  timer=Thread(target=check, args=(dodge1,))
  timer.start()
  if dodge1 not in dodgedirection:
    print("I'm sorry I didn't understand that.")
    print("Pick a direction to dodge:")
  if attackdirection1=="Down":
    if dodge1=="jump":
      print("You jumped over the ogre's attack.")
    if dodge1=="duck":
      criticalhit()
    if dodge1=="roll":
      hit()
    if dodge1=="slide":
      hit()
  if attackdirection1=="Up":
    if dodge1=="duck":
      print("You ducked under the ogre's attack.")
    if dodge1=="jump":
      criticalhit()
    if dodge1=="roll":
      hit()
    if dodge1=="slide":
      hit()
  if attackdirection1=="Right":
    if dodge1=="roll":
      print("You rolled over the ogre's attack.")
    if dodge1=="duck":
      hit()
    if dodge1=="jump":
      hit()
    if dodge1=="slide":
      criticalhit()
  if attackdirection1=="Left":
    if dodge1=="slide":
      print("You slid over the ogre's attack.")
    if dodge1=="duck":
      hit()
    if dodge1=="jump":
      hit()
    if dodge1=="roll":
      criticalhit()
    




enemyattack()

Please format your code like this: ```your code here ```. This makes it easier for community members to read your code and notice issues.

1 Like

Hello @IanJones17!

The error is on line 64: timer=Thread(target=check, args=(dodge1,))
If you remove the args=(dodge1,), the code should be fixed, therefore:

import random
import time
from threading import Thread



attack = 6
health = 5
dodge = False
bhealth = 5
damage = 0
battack = 2
bdodge = False
dodge1=None

def check():
  global dodge1
  print("Hi")
  time.sleep(2)
  if dodge1 != "jump" and dodge1 != "duck" and dodge1 != "roll" and dodge1 != "slide":
    print("You took damage")
    return

answer = None
attackdirection = ["Up", "Down", "Left", "Right"]
dodgedirection = ["jump", "duck", "slide", "roll"]
def criticalhit():
  global health
  global damage
  print("\033[31mCRITICAL HIT!\033[0m")
  damage = random.randint(3, 4)
  health = health - damage
  print("You took", damage, "damage")
def hit():
  global health
  global damage
  print("You were hit by the ogre!")
  damage = random.randint(1, 2)
  print("You took", damage, "damage.")
  health = health - damage
def enemyattack():
  global attackdirection
  global dodge
  global bdodge
  global health
  global battack
  global damage
  global dodgedirection

  if bdodge is True:
    bdodge = False
    return
  attackdirection1 = random.choice(attackdirection)
  if attackdirection1 == "Up":
    print("The ogre lifts his weapon\033[31m up\033[0m.")
  if attackdirection1 == "Down":
    print("The ogre swings his weapon in a \033[31m down\033[0mward arc")
  if attackdirection1 == "Left":
    print("You see the ogre's club hurtling towards your\033[31m left\033[0m.")
  if attackdirection1 == "Right":
    print("The ogre lashes out to the\033[31m right\033[0m.")
  print("Up=Duck, Down=Jump, Left=Slide, Right=Roll")
  dodge1 = input("Pick a direction to dodge:").lower()
  timer=Thread(target=check)
  timer.start()
  if dodge1 not in dodgedirection:
    print("I'm sorry I didn't understand that.")
    print("Pick a direction to dodge:")
  if attackdirection1=="Down":
    if dodge1=="jump":
      print("You jumped over the ogre's attack.")
    if dodge1=="duck":
      criticalhit()
    if dodge1=="roll":
      hit()
    if dodge1=="slide":
      hit()
  if attackdirection1=="Up":
    if dodge1=="duck":
      print("You ducked under the ogre's attack.")
    if dodge1=="jump":
      criticalhit()
    if dodge1=="roll":
      hit()
    if dodge1=="slide":
      hit()
  if attackdirection1=="Right":
    if dodge1=="roll":
      print("You rolled over the ogre's attack.")
    if dodge1=="duck":
      hit()
    if dodge1=="jump":
      hit()
    if dodge1=="slide":
      criticalhit()
  if attackdirection1=="Left":
    if dodge1=="slide":
      print("You slid over the ogre's attack.")
    if dodge1=="duck":
      hit()
    if dodge1=="jump":
      hit()
    if dodge1=="roll":
      criticalhit()





enemyattack()

Sure, that code might not have any errors, but it does not accomplish what the OP wants I believe. Please read the question more closely.