0% found this document useful (0 votes)
7 views2 pages

Game Py

This document contains a Python script for a simple text-based combat game. It defines a Character class with methods for attacking and taking damage, and a combat function that manages the battle between a player and an enemy. The main function initializes a player and an enemy, and starts the combat sequence.

Uploaded by

std6cce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Game Py

This document contains a Python script for a simple text-based combat game. It defines a Character class with methods for attacking and taking damage, and a combat function that manages the battle between a player and an enemy. The main function initializes a player and an enemy, and starts the combat sequence.

Uploaded by

std6cce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

# Character class to hold stats


class Character:
def __init__(self, name, health, attack, defense):
self.name = name
self.health = health
self.attack = attack
self.defense = defense

def is_alive(self):
return self.health > 0

def take_damage(self, damage):


self.health -= damage
if self.health < 0:
self.health = 0

def attack_enemy(self, enemy):


damage = random.randint(self.attack - 2, self.attack + 2) - enemy.defense
if damage < 0:
damage = 0
print(f"{self.name} attacks {enemy.name} for {damage} damage!")
enemy.take_damage(damage)

# Function for combat


def combat(player, enemy):
print(f"Battle begins! {player.name} vs. {enemy.name}")
while player.is_alive() and enemy.is_alive():
action = input("Do you want to (A)ttack or (D)efend? ").lower()
if action == 'a':
player.attack_enemy(enemy)
elif action == 'd':
print(f"{player.name} defends!")
else:
print("Invalid action.")
continue

if enemy.is_alive():
enemy.attack_enemy(player)
else:
print(f"{enemy.name} has been defeated!")

if player.is_alive():
print(f"{player.name}'s Health: {player.health}")
print(f"{enemy.name}'s Health: {enemy.health}")
else:
print(f"{player.name} has been defeated!")

# Main game
def main():
# Creating a player and enemy
player = Character("Hero", 50, 10, 3)
enemy = Character("Dragon", 100, 15, 5)

# Starting the combat


combat(player, enemy)

# Start the game


if __name__ == "__main__":
main()

You might also like