Here is the code just for the combat:
enemy_health = 10
player_health = 12 # Starting health for the player
while enemy_health > 0:
print("The goblin attacks! What do you do? (attack/run)")
action = raw_input("> ").lower()
if action == "attack":
print("You hit the goblin!")
enemy_health -= 2 # Simplified damage calculation
# Simulate the goblin fighting back
print("The goblin strikes back!")
player_health -= 2 # Player loses health when attacking
if enemy_health <= 0:
print("You defeated the goblin!")
break # Exit the loop after defeating the goblin
elif action == "run":
print("You run away safely.")
break # Exit the loop
else:
print("Invalid action. The goblin strikes you!")
player_health -= 2 # Damage to the player for an invalid action
if player_health <= 0:
print("You have been defeated by the goblin!")
break # Exit the loop if the player is defeated
# Optionally, display the current health of the player and the goblin
print("Your health:", player_health)
print("Goblin's health:", enemy_health)
Here is a complete minigame:
import random
class Character:
def __init__(self, name, health, attack_power):
self.name = name
self.health = health
self.attack_power = attack_power
def attack(self, other):
damage = random.randint(1, self.attack_power)
other.health -= damage
print "%s attacks %s for %d damage." % (self.name, other.name, damage)
if other.health <= 0:
print "%s has defeated %s." % (self.name, other.name)
def choose_location():
print "You stand at the crossroads. Do you go to the (F)orest or the (C)ave?"
choice = raw_input("> ").lower()
if choice == "f":
return "forest"
elif choice == "c":
return "cave"
else:
print "Confused, you decide to head back home."
return None
def encounter(location):
if location == "forest":
print "You encounter a wild goblin in the forest!"
return Character("Goblin", 30, 5)
elif location == "cave":
print "The cave is quiet and empty. You find a treasure chest."
return None
def game_loop():
player = Character("Hero", 100, 10)
companion = Character("Companion", 80, 8)
enemy = Character("Bandit", 50, 6)
print "Welcome to the Adventure Game!"
location = choose_location()
if location:
adversary = encounter(location)
if adversary:
while player.health > 0 and adversary.health > 0:
player.attack(adversary)
if adversary.health > 0:
adversary.attack(player)
if player.health <= 0:
print "You have been defeated. Game over."
else:
print "You emerge victorious!"
else:
print "You explore the area and find nothing else of interest."
else:
print "Maybe another time then."
game_loop()