Home > Software design >  Can anyone help me with the python code game?
Can anyone help me with the python code game?

Time:10-27

import random

mobs = {
  "enemys": {
    "Stray Dog": {
      "health": 3,
      "strenth": 2,
      "dexterity": 1,
    },
    "Hobo": {
      "health": 4,
      "strenth": 2,
      "dexterity": 2,
    },
    "Mugger": {
      "health": 5,
      "strenth": 2,
      "dexterity": 2,
    },
    "Cop": {
      "health": 7,
      "strenth": 3,
      "dexterity": 2,
    },
    "Ninja": {
      "health": 9,
      "strenth": 2,
      "dexterity": 4,
    }
  },
  "Player": {
    "phealth": 100,
    "pstrength": 1,
    "pdexterity": 1,
    "pconstitution": 1,
    },
}

levelrn = 3
phealth = mobs["Player"]["phealth"]
pstrength = mobs["Player"]["pstrength"]
pdexterity = mobs["Player"]["pdexterity"]
pconstitution = mobs["Player"]["pconstitution"]
ehealth = 0
estrength = 0
edexterity = 0
curenemy = ""


def level():
  global levelrn
  global pconstitution
  global pstrength
  global pdexterity
  global curenemy
  global ehealth
  global edexterity
  global estrength
  global phealth
  levelrn =3
  print("Level "   str(levelrn)   "\n---------")
  print("Health: "   str(phealth))
  print("Strenth: "   str(pstrength))
  print("Dexterity: "   str(pdexterity))
  print("Constitution: "   str(pconstitution))
  if levelrn == 1:
    curenemy = "Stray Dog"
  elif levelrn == 2:
    curenemy = "Hobo"
  elif levelrn == 3:
    curenemy = "Mugger"
  elif levelrn == 4:
    curenemy = "Cop"
  elif levelrn == 5:
    curenemy = "Ninja"
  estrength = mobs["enemys"][curenemy]["strenth"]
  edexterity = mobs["enemys"][curenemy]["dexterity"]
  ehealth = mobs["enemys"][curenemy]["health"]
  print("You find your self pitted against a "   curenemy   ".")
  print("Choose One\n----------\nAttack!")
  userin = input()
  if userin == "Attack" or "A" or "a" or "attack":
    if pdexterity > edexterity:
      pgdex()
    else:
      pldex()


def pgdex():
  pdamage = random.randrange(1, 3)
  print("You dealt "   str(pdamage)   " damage!")
  global ehealth
  ehealth -= pdamage
  pldex()


def pldex():
  if mobs["enemys"][curenemy]["strenth"] == estrength:
    edamage = random.randrange(estrength - 1, estrength   1)
    print("You took "   str(edamage)   " damage!")
    global phealth
    phealth -= edamage
    print("Hp: "   phealth)
    pgdex()


level()

This is the output of the code:

Level 1
---------
Health: 100
Strength: 1
Dexterity: 0
Constitution: 1
You find yourself pitted against a Stray Dog
Choose One
---------
Attack

It is supposed to run the level at 3 and the "Stray Dog" is supposed to be a "Mugger".

Also the dexterity and constitution and strength arent changing.

CodePudding user response:

There is an error in the last print statement of the code. Should be

print("Hp: "   str(phealth))

I ran the code after fixing that bug, and this is printed:

Level 3
---------
Health: 100
Strenth: 1
Dexterity: 1
Constitution: 1
You find your self pitted against a Mugger.
Choose One
----------
Attack!

which I think is what you want. However, a new issue arises because the game will keep running after reaching negative HP.

CodePudding user response:

First of all, you have to change line 103 to print("Hp: " str(phealth)) or the script won't work.

Then, you have to change functions pgdex and pldex because they invoke each other and create an infinite loop which results in RecursionError.

Besides that your code seems right. I get an output of lvl 3 and fight agains Mugger when I start it.

To handle RecursionError:

def foo1():
  pdamage = random.randrange(1, 3)
  print("You dealt "   str(pdamage)   " damage!")
  global ehealth
  ehealth -= pdamage

def foo2():
  if mobs["enemys"][curenemy]["strenth"] == estrength:
    edamage = random.randrange(estrength - 1, estrength   1)
    print("You took "   str(edamage)   " damage!")
    global phealth
    phealth -= edamage
    print("Hp: "   str(phealth))

def pgdex():
  foo1()
  foo2()

def pldex():
  foo2()
  foo1()
  • Related