Skip to content

Commit 2196f0c

Browse files
Add files via upload
1 parent 3cb3b5d commit 2196f0c

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Text-Based Adventure Game.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Importing necessary modules
2+
import random
3+
4+
# Game constants
5+
WIDTH, HEIGHT = 80, 20
6+
PLAYER_HEALTH = 100
7+
MONSTER_HEALTH = 50
8+
9+
# Game classes
10+
class Player:
11+
def __init__(self, name):
12+
self.name = name
13+
self.health = PLAYER_HEALTH
14+
self.inventory = []
15+
16+
def attack(self, monster):
17+
damage = random.randint(10, 20)
18+
monster.health -= damage
19+
print(f"You attack the {monster.name} for {damage} damage!")
20+
21+
def use_item(self, item):
22+
if item in self.inventory:
23+
if item == "healing_potion":
24+
self.health += 20
25+
print("You drink a healing potion and regain 20 health!")
26+
elif item == "sword":
27+
print("You equip the sword and deal more damage!")
28+
self.inventory.remove(item)
29+
30+
class Monster:
31+
def __init__(self, name, health):
32+
self.name = name
33+
self.health = health
34+
35+
def attack(self, player):
36+
damage = random.randint(10, 20)
37+
player.health -= damage
38+
print(f"The {self.name} attacks you for {damage} damage!")
39+
40+
class Room:
41+
def __init__(self, description, items, monsters):
42+
self.description = description
43+
self.items = items
44+
self.monsters = monsters
45+
46+
# Game rooms
47+
rooms = [
48+
Room("You are in a dark forest.", ["healing_potion"], [Monster("Goblin", MONSTER_HEALTH)]),
49+
Room("You are in a cave.", ["sword"], [Monster("Troll", MONSTER_HEALTH * 2)]),
50+
Room("You are in a castle.", [], [Monster("Dragon", MONSTER_HEALTH * 5)]),
51+
]
52+
53+
# Game logic
54+
def game_loop(player):
55+
current_room = 0
56+
while True:
57+
print(rooms[current_room].description)
58+
print("You have the following items:")
59+
for item in player.inventory:
60+
print(item)
61+
print("You see the following monsters:")
62+
for monster in rooms[current_room].monsters:
63+
print(monster.name)
64+
action = input("What do you do? (move, attack, use item) ")
65+
if action == "move":
66+
current_room = (current_room + 1) % len(rooms)
67+
elif action == "attack":
68+
monster = random.choice(rooms[current_room].monsters)
69+
player.attack(monster)
70+
if monster.health <= 0:
71+
rooms[current_room].monsters.remove(monster)
72+
print(f"You killed the {monster.name}!")
73+
elif action == "use item":
74+
item = input("Which item do you want to use? ")
75+
player.use_item(item)
76+
77+
# Game start
78+
player_name = input("What is your name? ")
79+
player = Player(player_name)
80+
game_loop(player)

0 commit comments

Comments
 (0)