Milo Robson
Milo Robson

Reputation: 21

Updating a value of an integer (I think)

I am new to python. at the moment I am coding a game with a friend. we are currently working on a combat system the only problem is we don't know how to update the the enemy's health once damage has been dealt. The code is as following.

enemy1_health = 150

broadsword_attack = 20
rusty_knife = 10.5 

attacks = ["broadsword swing " +  str(broadsword_attack), "rusty knife jab " + str(rusty_knife) ]



while enemy1_health > 0: 
  while player_health > 0:
    enemy1_health = 150
    print(attacks)
    attackchoice = input("Choose an attack: ")
    if attackchoice == ("broadsword swing"):
      print (int(enemy1_health - 20))
    if attackchoice == ("rusty knife jab"):
      print (int(enemy1_health - 10.5))
  print("you died")
  quit()   
print("you cleared the level")```

Upvotes: 2

Views: 57

Answers (3)

this is a good name
this is a good name

Reputation: 63

You need to change the enemy health outside the print statement. do:

 if attackchoice == ("rusty knife jab"):
    enemy1_health = enemy1_health - 10.5
    print(enemy1_health)

and you can do the same for the other attacks.

You also have enemy health defined in the while loop. you need to define it outside of the loop.

Upvotes: 0

Ryan Laursen
Ryan Laursen

Reputation: 647

You need to change the enemy health outside of the print statement with a statement like this:

enemy1_health = enemy1_health - 20

or like this, which does the same thing:

enemy1_health -= 20

You also reset enemy1_health every time the loop loops, remove that.

You don't define player_health, define that.

Your loop goes forever until you die.

So your code should end up looking more like this:

enemy1_health = 150
broadsword_attack = 20
rusty_knife = 10.5
player_health = 100

attacks = ["broadsword swing " + str(broadsword_attack), "rusty knife jab " + str(rusty_knife)]

while enemy1_health > 0:
    print(attacks)
    attackchoice = input("Choose an attack: ")
    if attackchoice == ("broadsword swing"):
        enemy1_health -= 20
    if attackchoice == ("rusty knife jab"):
        enemy1_health -= 10.5
    print(enemy1_health)
    if player_health <= 0:
        print("you died")
        quit()
print("you cleared the level")

This still requires quite a bit of tweaking, it'd be a complete working game if it was like this (basically, you win if you spam broadsword attacks because they do more damage):

enemy1_health = 150
enemy1_attack = 10
player_health = 100
broadsword_attack = 20
rusty_knife = 10.5

attacks = ["broadsword swing " + str(broadsword_attack), "rusty knife jab " + str(rusty_knife)]

while enemy1_health > 0:
    print(attacks)
    attackchoice = input("Choose an attack: ")
    if attackchoice == ("broadsword swing"):
        enemy1_health -= broadsword_attack
    if attackchoice == ("rusty knife jab"):
        enemy1_health -= rusty_knife
    print(f'A hit! The enemy has {enemy1_health} health left.')
    if enemy1_health > 0:
        player_health -= enemy1_attack
        print(f'The enemy attacks and leaves you with {player_health} health.')
    if player_health <= 0:
        print("you died")
        quit()
print("you cleared the level")

Upvotes: 3

SaGaR
SaGaR

Reputation: 542

You have to define the health above the while loops like this :

broadsword_attack = 20
rusty_knife = 10.5
attacks = ["broadsword swing " +  str(broadsword_attack), "rusty knife jab " + str(rusty_knife) ]
enemy1_health = 150


while enemy1_health > 0: 
  while player_health > 0: 
    print(attacks)
    attackchoice = input("Choose an attack: ")
    if attackchoice == ("broadsword swing"):
      enemy1_health -=20
      print (int(enemy1_health))
    if attackchoice == ("rusty knife jab"):
      enemy1_health -=10.5
      print (int(enemy1_health))
  print("you died")
  quit()   
print("you cleared the level")

To avoid the health to be redefined to 150 in every iteration of the loop. And also assigning the values to health not just print it. Which means that the enemy never losses its health no matter what you do. Also there is also another problem as you are never decreasing players health which will result in an infinite loop

Upvotes: 2

Related Questions