Battle Program Which Keeps track of "Health Points"

I want to write a Python program that will battle a 70 hit point character with an 80 hit point creature.

I can get it to roll once, but want it to keep a tally on hit points until one or the other runs out of points.

Any help is greatly appreciated. I think I am close:

import random

a = 80
b = 70
hit = random.randint(1,20)
alive = True

dmg = random.randint(1,20)
if hit >= 15:
dmg = random.randint(1,8)
print(“You hit”, “for”, dmg)

if hit <15:
print(“You Missed!”)
dmg = 0
mlife = a - dmg
print(mlife, “Hit Points Left”)

dmg = random.randint(1,20)
if hit >= 15:
dmg = random.randint(1,8)
print(“The Creature Hits For”, dmg)

if hit <15:
print(“The Creature Missed!”)
dmg = 0
clife = b - dmg
print(mlife, “Hit Points Left”)

if mlife <= 0:
print(“Creature Dies!”)
if clife <= 0:
print(“Character Dies!”)
if mlife > 0:
print(“Creature Still Alive!”)
if clife > 0:
print(“Character Still Alive!”)

I can see what you are trying to do and I offer you this, as a starter:

# don't import everything, if you only need one function
from random import randint

# option to have a pause, so that the fight lasts a number of seconds
from time import sleep

creatures = {
    1 : ['Golem',    3, 100],
    2 : ['Ogre',     4, 100],
    3 : ['Minotaur', 6, 100]

players = {
    1 : ['Shawn', 0, 100],
    2 : ['Alice', 0, 100],
    3 : ['Bob',   0, 100]

weapons = {
    1 : ['a pointed stick',  1, 10],
    2 : ['a short knife',    3, 10],
    3 : ['a long knife',     4, 10],
    4 : ['a short sward',    5, 10],
    5 : ['a long sward',     6, 10],
    6 : ['a bow and arrows', 8, 10]
# indices
name = 0
damage = 1
health, usage = 2, 2

# get a random player and weapon
player = players.get(randint(1,len(players)))
weapon = weapons.get(randint(1,len(weapons)))
weaponName = weapon[name]
playerDamage = weapon[damage]

# get a random creature
creature = creatures.get(randint(1,len(creatures)))
creatureName = creature[name]
creatureDamage = creature[damage]

print(f"{player[name]} has {weaponName} and will be fighting a {creatureName}")

Don’t Panic! The above code is not as complicated as you may think, at first glance, and is designed to be extendable: you can add players, weapons, and creatures, without any change to the main body of the code; simply add to the dictionaries. The first number is a damage rating and the second number is a health rating. For the weapons, the ‘health rating’ can be seen as a ‘usage’, which should be reduced by one, each time the weapon is used, so each weapon can only be used 10 times; change this as you see fit. e.g: maybe ‘a pointed stick’ should only be used four times.

I think that the ‘hit’ should be a random number between zero and four: if ‘hit’ is zero, then it’s a ‘miss’, any other value being a ‘hit’.

Then, who goes first?

A ‘coin flip’ would be: choose = randint(0,1)

So, we now have the scene set, ready for the fight to commence. I’ll not take this any further for now, because:

  • You may not understand any of this or even like it
  • You may want to develop this in your own way, which would be a better learning experience

You can use any of this, all of this or none of this; it’s your choice. You may even want to wait and see what others have to say. I’m not a coding expert, but this is how I would set out, if I were to start this kind of a project.

Thank you!

No worries. With some thoughtful and careful development, you should be able to move this forward.

I’ve had another thought: maybe each player could have a skill level, which will increase with every win and decrease with every loss. Also, why not have the player damage decrease when a creature attack fails and the same for the creature, when a player attack fails.

Post back if you get stuck and post any code, fenced off, between backtics, like this:

any code

Try to only post code samples, that is to say; only code that you’re having trouble with. From that, you should be able to see that you should develop this in small steps (code blocks), solving each problem, one step at a time.

Do you know how to code a custom function? I ask this, because the way I see this moving forward, you’re very soon going to need that skill.

One last thing (for now): keep in mind that I have done all of this on the fly, so it could very well turn out that I have some design floor which may require a major code change; I hope this is not the case.