forked from solygambas/python-openai-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26-monster-slayer.py
123 lines (101 loc) · 3.7 KB
/
26-monster-slayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
HEART_EMOJI = "❤️"
# Welcome message
def welcome_message():
print("Welcome to Monster Slayer!")
# Get username from user
def get_username():
username = input("Enter your username: ")
return username
# Get difficulty level from user
def get_difficulty():
print("\nChoose a difficulty level:")
print("1. Easy")
print("2. Normal")
print("3. Hard")
while True:
difficulty = input(
"Enter the number corresponding to your chosen difficulty (1-3): "
)
if difficulty in ["1", "2", "3"]:
break
else:
print("Invalid difficulty level. Please choose again.")
return difficulty
# Initialize player and monster health
def initialize_health():
player_health = 100
monster_health = 100
return player_health, monster_health
# Main game logic
def play_game():
welcome_message()
username = get_username()
difficulty = get_difficulty()
player_health, monster_health = initialize_health()
# Game rounds loop
round_count = 1
player_turn = True
while player_health > 0 and monster_health > 0:
print(f"\n----- Round {round_count} -----")
print(
f"{username}'s health: {HEART_EMOJI} {player_health}\tMonster health: {HEART_EMOJI} {monster_health}\n"
)
# Player's turn
if player_turn:
print("Your turn:")
print("Choose your action:")
print("1. Regular Attack")
if round_count == 1 or round_count % 3 == 0:
print("2. Strong Attack")
if round_count == 1 or round_count % 5 == 0:
print("3. Heal")
while True:
action = input("Enter the number corresponding to your action (1-3): ")
if action in ["1", "2", "3"]:
break
else:
print("Invalid action. Please choose again.")
if action == "1":
damage = random.randint(10, 15)
monster_health -= damage
print(f"You dealt {damage} damage to the monster!")
elif action == "2" and (round_count == 1 or round_count % 3 == 0):
damage = random.randint(20, 25)
monster_health -= damage
print(f"You dealt {damage} damage to the monster with a strong attack!")
elif action == "3" and (round_count == 1 or round_count % 5 == 0):
healing = random.randint(15, 20)
player_health += healing
print(f"You healed for {healing} health points!")
else:
print("Invalid action. Skipping your turn...")
# Monster's turn
else:
print("Monster's turn:")
damage = random.randint(10, 20)
player_health -= damage
print(f"The monster attacked you and dealt {damage} damage!")
player_turn = not player_turn
round_count += 1
# Check if game is over
if player_health <= 0:
print("You have been defeated! The monster wins!")
break
elif monster_health <= 0:
print("Congratulations! You have slain the monster!")
break
# Manage high score
with open("scores/high_score.txt", "a") as file:
file.write(f"{username}: {round_count-1} rounds\n")
print("\nHigh Scores:")
with open("scores/high_score.txt", "r") as file:
high_scores = file.read()
print(high_scores)
# Ask for a new game
play_again = input("Do you want to play again? (yes/no): ")
if play_again.lower() == "yes":
play_game()
else:
print("Thank you for playing Monster Slayer!")
play_game()