-2

I'm trying to make a text based game, and to make my fighting mechanics I have to repeat an if statement until you or the monster dies.

Here it is --
Ph would stand for Player health, str is player strength, mdam is monster damage, mstr is monster strength, monsterh is monsterhealth, the random.random() is the attack bonus you receive (Adding onto this, is there any way to round the number to one or no decimal places?) Thank you! :D

if fight == "A" and ph > 0:
    damage = (str) + (random.random())
    monsterh = 6
    mstr = 0.9
    monsterh = (monsterh) - (damage)
    print(damage)
    print(damage - str)
    print('You attack the monster. You deal %s damage.' % damage ) 
    time.sleep(3)
    if monsterh > 0:
        mstr = 0.9
        mdam = (mstr) + (random.random())
        print('The monster attacks you!')
        ph = ph - mstr
        print("You get hit %s" % mdam )
    else:
        xpgain = damage + 5
        print("You won! You've gained %s XP!" % xpgain)
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • 1
    You want a `while` loop. Check out https://docs.python.org/2/tutorial/controlflow.html?highlight=flow%20control. – g.d.d.c Dec 17 '14 at 22:46
  • You could put the whole thing in a loop: `while i_am_not_dead and monster_is_not_dead: ...` – mgilson Dec 17 '14 at 22:46

2 Answers2

0

To round the value you get from

random.random() 

you can do

round(random.random(),1) 

to round to 1 decimal place, or if you want to get rid of decimals, replace

random.random() 

with

int(random.random())

As for repeating the if statement, you can put it into a loop which will break when a condition is met

while true:
    if fight == "A" and ph > 0:
        damage = (str) + (int(random.random()))
        monsterh = 6
        mstr = 0.9
        monsterh = (monsterh) - (damage)
        print(damage)
        print(damage - str)
        print('You attack the monster. You deal %s damage.' % damage ) 
        time.sleep(3)
    elif ph<=0 or fight!="A":
        print("You lose!")
        break
    if monsterh > 0:
        mstr = 0.9
        mdam = (mstr) + (int(random.random()))
        print('The monster attacks you!')
        ph = ph - mstr
        print("You get hit %s" % mdam )
    else:
        xpgain = damage + 5
        print("You won! You've gained %s XP!" % xpgain)
        break

Although you wouldn't need the

fight!="A"
since I don't think you change the value of fight at all.

Also you don't have to do

mdam = (mstr) + (int(random.random()))

You can just do

mdam = mstr+int(random.random())

This above ^ applies to other places in your code as well. If you have any questions please feel free to ask.

CyanogenCX
  • 414
  • 6
  • 17
0

I would wrap your combat seen in a while loop. For example:

while (ph > 0 or monsterh > 0)
    # combat rules

# Check who died and move on from there
if (ph > 0)
    # next thing the player does
else
    # print "You died"

In regards to rounding, there are multiple options depending on exactly what you want to do: python: getting only 1 decimal place

Community
  • 1
  • 1