RCRPG/Python: Difference between revisions
m
Fixed syntax highlighting.
(Created page with '{{collection|RCRPG}} This Python 3.1 version of RCRPG uses the standard text interface. All the commands listed on the blog post are implemented, as well…') |
m (Fixed syntax highlighting.) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 4:
All the commands listed on the blog post are implemented, as well as move and unequip.
===Code===
<syntaxhighlight lang="python">directions = {'north':(0,-1,0),▼
▲directions = {'north':(0,-1,0),
'east':(1,0,0),
'south':(0,1,0),
Line 26 ⟶ 23:
class Room:
def __init__(self, items=[]):
self.passages = dict
self.items = items[:]
def describe(self, location):
result = 'You are at '
if
result += roomNames[location]
else:
posStr = ",".join
result += posStr
if
result += '\nOn the ground you can see: '+', '.join(self.items)
result += '\nExits are: '
exits =
exits = ['None']▼
return result
def take(self, target):
results = []
if
results = self.items[:]
del self.items
print('You now have everything in the room.')
elif
results =
print('Taken.')
else:
Line 71 ⟶ 62:
def opposite_dir(direction):
if
return 'south'
elif
return 'north';
elif
return 'east';
elif
return 'west';
elif
return 'down';
elif
return 'up';
else:
Line 87 ⟶ 78:
def make_random_items():
if rand == 0:
return []
elif
return ['sledge']
elif
return ['ladder']
else:
Line 98 ⟶ 90:
class World:
def __init__(self):
self.rooms = {(0,0,0): Room(['sledge'])}
def look(self):
Line 107 ⟶ 100:
def move(self, direction):
if
print("You'll need a ladder in this room to go up.")
return
if
print("That's not a direction.")
return
if
self.currentPos = get_new_coord(self.currentPos, direction)
else:
Line 124 ⟶ 117:
def inventory(self):
if
print('Carrying: '+', '.join(self.inv))
else:
print("You aren't carrying anything.")
if
print('Holding: '+self.equipped)
Line 135 ⟶ 128:
def drop(self, target):
if
self.rooms[self.currentPos].items
del self.inv
print('Everything dropped.')
elif
self.rooms[self.currentPos].items
print('Dropped.')
else:
Line 148 ⟶ 140:
def equip(self, itemName):
if
if
self.unequip()
self.equipped =
print('Equipped '+itemName+'.')
else:
print("You aren't
def unequip(self):
if
print("You aren't equipped with anything.")
else:
self.inv
print('Unequipped '+self.equipped+'.')
self.equipped = ''
Line 170 ⟶ 161:
def dig(self, direction):
if
print("You don't have a digging tool equipped.")
return
if
print("That's not a direction.")
return
if
self.rooms[self.currentPos].passages[direction] = True
joinRoomPos = get_new_coord(self.currentPos, direction)
if
self.rooms[joinRoomPos] = Room(make_random_items())
self.rooms[joinRoomPos].passages[opposite_dir(direction)] = True
Line 188 ⟶ 179:
world = World()
print("Welcome to the dungeon!\nGrab the sledge and make your way to room 1,1,5 for a non-
while True:
print('\n'+world.look())
tokens =
if
break
while
if
tokens = aliases[tokens[0]] + tokens[1:]
continue
Line 207 ⟶ 198:
break
print("Thanks for playing!")</syntaxhighlight>
|