226k views
21 votes
Create a while loop to try making a video game turn based battle.

Then have a monster health value and a player health value.
The player can then choose to attack, heal, or do magic.
If they choose to attack, roll one die and take that damage away from the monsters health.
If they choose to heal then add a dice roll to the player's hp.
If they choose magic then take two dice rolls away from the enemy.
The enemy attacks every turn with 1 dice roll.

User NendoTaka
by
5.8k points

1 Answer

10 votes

import random

monster_health = 20

player_health = 20

while player_health >0 and monster_health>0:

player_choice = input("Do you wish to attack, heal, or do magic? (type attack, heal, or magic) ")

if player_choice == "attack":

damage = random.randint(1,6)

monster_health -= damage

print("You attack the monster for",damage,"points of health. The monster has",monster_health,"points of health")

elif player_choice == "heal":

heal = random.randint(1,6)

player_health += heal

if player_health>20:

player_health = 20

print("You heal for",heal,"points of health. Your health is now at",player_health)

elif player_choice == "magic":

total = 0

for x in range(2):

total += random.randint(1,6)

monster_health -= total

print("You attack the monster for",total,"points of health. The monster has",monster_health,"points of health")

if player_health < 1 or monster_health <1:

break

monster_damage = random.randint(1,6)

player_health -= monster_damage

print("The monster attacks your for",monster_damage,"points of health. You have",player_health,"points of health remaining.")

if player_health < 1:

print("You died! Better luck next time!")

elif monster_health < 1:

print("You defeated the monster! Great job!")

I wrote my code in python 3.8. Best of luck.

User Brett Bender
by
6.1k points