Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.0 kB
3
Indexable
Never
import random

# Definerer en klasse for en spiller
class Player:
    def __init__(self, name):
        self.name = name
        self.health = 100
        self.damage = 10

    def attack(self, other_player):
        damage_dealt = random.randint(1, self.damage)
        other_player.health -= damage_dealt
        print(f"{self.name} angriber {other_player.name} og forårsager {damage_dealt} skade.")

# Opretter to spillere
player1 = Player("Spiller 1")
player2 = Player("Spiller 2")

# Simulerer kampen mellem spillere
rounds = 1
while player1.health > 0 and player2.health > 0:
    print(f"Runde {rounds}")
    player1.attack(player2)
    player2.attack(player1)
    print(f"{player1.name} har {player1.health} helbred tilbage.")
    print(f"{player2.name} har {player2.health} helbred tilbage.")
    rounds += 1

# Afgør vinderen
if player1.health > 0:
    print(f"{player1.name} vandt kampen!")
elif player2.health > 0:
    print(f"{player2.name} vandt kampen!")
else:
    print("Kampen endte uafgjort.")
Leave a Comment