Skip to content

Commit 26b86fe

Browse files
authored
Create chad_demo_rpg.py
1 parent df31fac commit 26b86fe

File tree

1 file changed

+232
-0
lines changed

1 file changed

+232
-0
lines changed

TLG/rpg/chad_demo_rpg.py

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#!/usr/bin/python3
2+
from random import randint
3+
import dice
4+
import sys
5+
import os
6+
'''Chad's Ongoing RPG Game'''
7+
8+
bestiary = [{'name' : 'goblin', 'health' : 10, 'damage' : '1d5'},
9+
{'name' : 'orc', 'health' : 15, 'damage' : '1d8'},
10+
{'name' : 'ogre', 'health' : 20, 'damage' : '1d12'}]
11+
armory = {'sword': {'damage': '1d12'}}
12+
spell_lookup = {'fireball': {'damage': '4d6'}}
13+
player_health = 20
14+
inventory = []
15+
spellbook = []
16+
17+
def combat():
18+
monster_ID= randint(0,2)
19+
20+
global player_health, inventory, armory, bestiary
21+
round = 1
22+
monster_health = bestiary[monster_ID]['health']
23+
24+
print(f"A ferocious {bestiary[monster_ID]['name']} approaches! COMBAT HAS BEGUN!\n")
25+
while True:
26+
print(f"ROUND {round}")
27+
print("Player Health: [" + str(player_health) + "]")
28+
print("Monster Health: [" + str(monster_health) + "]")
29+
30+
print("Type: RUN, CAST [spell], or USE [weapon]") # gotta write code for cast
31+
move = input().lower().split() # converts move into a lower-case list to deal with each item in list separately
32+
monster_damage = sum(dice.roll(bestiary[monster_ID]['damage']))
33+
print("\n=========================")
34+
35+
36+
if move[0] == 'use': #
37+
if move[1] in inventory: # checks if weapon is in your inventory
38+
player_damage = dice.roll(armory[move[1]]['damage'])
39+
print(f"You hit a {bestiary[monster_ID]['name']} for {player_damage} damage!")
40+
if move[1] not in inventory:
41+
print(f"There is no {move[1]} in your inventory!")
42+
43+
if move[0] == 'cast': #
44+
if move[1] in spellbook: # checks if spell is in your spellbook
45+
if move[1].lower() == 'fireball':
46+
player_damage = sum(dice.roll(spell_lookup[move[1]]['damage']))
47+
print(f"Summoning eldritch forces, you scorch the {bestiary[monster_ID]['name']} for {player_damage} damage!")
48+
if move[1] not in spellbook:
49+
print(f"You don't know the {move[1]} spell!")
50+
51+
if move[0] == 'run': #
52+
escape_chance= randint(1,10) #+ player_speed # if I set this variable later, here's where it would work
53+
54+
if escape_chance >= 10:
55+
print("You make a flawless escape!")
56+
break
57+
if escape_chance >= 5:
58+
print("You expose your back as you turn and flee- the monster takes advantage.")
59+
print(f"A {bestiary[monster_ID]['name']} hits you for {monster_damage} damage!")
60+
player_health -= int(monster_damage)
61+
if player_health >= 1:
62+
print("You managed to escape.")
63+
break
64+
if player_health < 1:
65+
print("You have been slain.")
66+
print("\nGAME OVER")
67+
sys.exit()
68+
if escape_chance >= 0:
69+
print("The monster out-maneuvers you and attacks! You do not escape.")
70+
71+
try:
72+
monster_health -= int(player_damage)
73+
except:
74+
pass
75+
if monster_health <= 0:
76+
print(f"The {bestiary[monster_ID]['name']} lies dead. You are victorious!\n")
77+
break
78+
79+
print(f"A {bestiary[monster_ID]['name']} hits you for {monster_damage} damage!")
80+
print ("=========================\n")
81+
round += 1
82+
player_health -= int(monster_damage)
83+
84+
if player_health <= 0:
85+
print("You have been vanquished! You are dead.")
86+
sys.exit()
87+
88+
def showInstructions():
89+
print('''
90+
CHAD'S RPG GAME
91+
OBJECTIVE: Collect spells and weapons- fight and survive!
92+
--------
93+
Actions:
94+
GO [north, south, east, west, up, down]
95+
GET [item, spell]
96+
USE [item, spell]
97+
LOOK
98+
INV/INVENTORY
99+
100+
Type 'help' at any time! Type 'q' to quit!''')
101+
102+
def playerinfo():
103+
# print('')
104+
# print('YOU ARE IN THE ' + currentRoom + '.')
105+
print('=================================')
106+
print('Inventory :', str(inventory))
107+
print('Spells :', str(spellbook))
108+
print('=================================')
109+
110+
111+
def showStatus(): # display the player's status
112+
# if 'desc' in rooms[currentRoom]:
113+
# print(rooms[currentRoom]['desc'])
114+
if 'item' in rooms[currentRoom]:
115+
print('You see a ' + rooms[currentRoom]['item'] + rooms[currentRoom]['item_status'] + '.')
116+
if 'spell' in rooms[currentRoom]:
117+
print('You see a magic scroll. On the ribbon it says "' + rooms[currentRoom]['spell'] + '".')
118+
# print('=================')
119+
120+
def spellreceive(incantation):
121+
# print("You received a new spell scroll. Be careful... magic is dangerous!")
122+
# incantation = input("Create the magic word to summon your spell! >")
123+
# if incantation not in spells:
124+
print("\nA pentagram illuminates beneath your feet as an unnatural wind sweeps your hair.")
125+
print("The spell has been successfully added to your spellbook. Be careful... magic is dangerous!")
126+
127+
def random_encounter():
128+
if ((int(rooms[currentRoom]['randenc'])) + 5) >= 10:
129+
combat()
130+
131+
rooms = {
132+
'HALL' : {
133+
'south' : 'KITCHEN',
134+
'east' : 'DINING ROOM',
135+
'item' : 'sword',
136+
'item_status' : ' inside of a display case. It is unlocked',
137+
'randenc' : '20',
138+
'desc' : 'You are in the hall of a large, decrepit house. The walls are blackened from some ancient fire. You get the feeling you are being watched. To the east is a dusty dining room. South is the kitchen... something is moving there.'
139+
},
140+
'KITCHEN' : {
141+
'north' : 'HALL',
142+
'randenc' : '0',
143+
'down' : 'BASEMENT',
144+
'desc' : 'You are in what was once a kitchen. Nests made of human bones are draped across every countertop. There is a large hole in the floor. Where it leads you have no idea.'
145+
},
146+
'BASEMENT' : {
147+
'spell' : 'fireball',
148+
'desc' : 'You are in a stinking basement with an earthen floor. You can\'t even see your hand in front of your face. You are likely to be eaten by a grue.',
149+
'randenc' : '0',
150+
'up': 'KITCHEN',
151+
},
152+
'DINING ROOM' : {
153+
'west' : 'HALL',
154+
'south' : 'GARDEN',
155+
'north' : 'PANTRY',
156+
'desc' : 'You are in the dining room. The table is set for an elegant party but is covered a blanket of dust. Sleeping bats cling to the chandelier. North is a dark pantry. South lies the garden. West returns to the hall.',
157+
'randenc' : '0',
158+
'item' : 'potion',
159+
'item_status' : ' hiding among the bottles of wine. It is cherry red in color'
160+
},
161+
'GARDEN' : {
162+
'north' : 'DINING ROOM',
163+
'spell' : 'fireball',
164+
'randenc' : '0',
165+
},
166+
'PANTRY' : {
167+
'south' : 'DINING ROOM',
168+
'randenc' : '0',
169+
'item' : 'cookie'
170+
}
171+
}
172+
173+
currentRoom = 'HALL' # player start location
174+
175+
os.system('clear') # start game with a fresh screen
176+
showInstructions() # show instructions to the player
177+
178+
while True: # MAIN INFINITE LOOP
179+
playerinfo()
180+
showStatus()
181+
# ask the player what they want to do
182+
move = ''
183+
while move == '':
184+
move = input('>') # so long as the move does not
185+
# have a value. Ask the user for input
186+
187+
move = move.lower().split() # make everything lower case because directions and items require it, then split into a list
188+
os.system('clear') # clear the screen
189+
if move[0] == 'go':
190+
if move[1] in rooms[currentRoom]:
191+
currentRoom = rooms[currentRoom][move[1]]
192+
if 'desc' in rooms[currentRoom]:
193+
print(rooms[currentRoom]['desc'])
194+
random_encounter()
195+
# if YES that direction exists, then assign your new current room to the VALUE of the key the user entered
196+
else:
197+
print("YOU CAN'T GO THAT WAY!")
198+
if move[0] == 'use':
199+
if move[1].lower() == 'potion' and 'potion' in inventory:
200+
print("You drink from the potion. Your health has been restored!")
201+
print("Your potion magically refills itself! Handy!")
202+
player_health = 20
203+
if move[0] == 'get':
204+
if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
205+
inventory += [move[1]] # add item to inv
206+
print(move[1].capitalize() + ' received!') # msg saying you received the item
207+
del rooms[currentRoom]['item'] # deletes that item from the dictionary
208+
elif 'spell' in rooms[currentRoom] and move[1] in rooms[currentRoom]['spell']:
209+
spellreceive('spell')
210+
spellbook += [move[1]] # add spell to spells
211+
del rooms[currentRoom]['spell']
212+
213+
else:
214+
print('YOU CANNOT GET ' + (move[1].upper()) + '!')
215+
216+
if move[0] == 'look':
217+
if 'desc' in rooms[currentRoom]:
218+
print(rooms[currentRoom]['desc']) # print the look description
219+
else:
220+
print('You can\'t see anything.')
221+
222+
elif move[0] == 'help':
223+
showInstructions()
224+
225+
elif move[0] in ['q', 'quit]']:
226+
print("Are you sure you want to quit? Yes/No")
227+
quit_query = input('>')
228+
if quit_query.lower() in ['y', 'yes']:
229+
print("Thanks for playing!")
230+
sys.exit()
231+
else:
232+
pass

0 commit comments

Comments
 (0)