Skip to content Skip to sidebar Skip to footer

Making A D&d Game Running Into Issues

I have been learning Python for a while and I decided to make a D&D type game on my own. I am trying to make a user input to choose out of 3 characters each with their own hit

Solution 1:

You are running into issues because you are trying to access the 'HP'th index of your input string. Python doesn't understand this command because it doesn't make sense.

An alternative to consider:

Choose_Character = input('Choose a character:\n1.Warrior\n2.Wizard\n3.Elf\n')

if Choose_Character == '1':
    character = {'Class': 'Warrior', 'HP': 140, 'Magic': 23}
elif Choose_Character == '2':
    character = {'Class': 'Wizard', 'HP': 123, 'Magic': 12}
elif Choose_Character == '3':
    character = {'Class': 'Elf', 'HP': 123, 'Magic': 12}

print('Here are your character stats:\nHit Points: ' + str(character['HP']) + '\nMagic Points: ' + str(character['Magic']))

fox = {'HP': 123, 'Magic': 12}

print('You encounter a fox he bites you. you lode 20 Hit Points')

new_stats = int(character['HP'] - 20)
print(f'your Hit Points are now:{new_stats}')

As an additional suggestion, I would recommend putting your characters, monsters, and items into a class system (as in the programming 'class' and not the RPG 'class'). This will allow you to create copies of them, which allows you to fight the same type of monster more than once without manually resetting a bunch of stuff. It also allows you to implement interesting behavior for monsters. For instance, you could have a class function on_die that could specify custom behavior when a monster's HP reaches zero. For slimes, this function could be used to spawn more slimes. In summary, switching to a class structure would benefit your project a lot.

Solution 2:

Choose_Character is either '1','2' or '3' - which is string thus you get the error you report. As you are trying to access string with ['HP'] but string can be only addressed by integers.

What you actually want to do is something like this

  Choose_Character = input('Choose a character:\n1.Warrior\n2.Wizard\n3.Elf\n')

  if Choose_Character == '1':
      hero = {'HP':140,'Magic':23,'Class':'Warrior'} # CHANGED HERE - moved name of class into dictif Choose_Character == '2':
      hero = {'HP':123,'Magic':12,'Class':'Wizard'} # CHANGED HERE - moved name of class into dictif Choose_Character == '3':
      hero = {'HP':123,'Magic':12,'Class':'Elf'} # CHANGED HERE - moved name of class into dictprint(f"Here are your {hero['Class']} stats:\nHit Points: {str(hero['HP'])} \nMagic Points: {str(hero['Magic'])}")

  monster = {'HP':123,'Magic':12, 'Name': 'Fox'}

  print(f"You encounter a {monster['Name']} he bites you. you lose 20 Hit Points")

  hero['HP'] = hero['HP'] - 20print(f"your Hit Points are now:{hero['HP']}")

Or something along this lines... don't give up :D

Post a Comment for "Making A D&d Game Running Into Issues"