Containers now work. Discovered issue with loading saved games.
This commit is contained in:
parent
eeab517213
commit
2719075a33
3 changed files with 94 additions and 11 deletions
57
gameshell.py
57
gameshell.py
|
@ -49,6 +49,7 @@ class GameShell(Shell):
|
|||
self.registerCommand('options', self.options)
|
||||
self.registerCommand('colorTest', self.colorTest)
|
||||
self.registerAlias('run', ['go', '-r'])
|
||||
self.gameBase.registerIO('container', self.container)
|
||||
|
||||
# Helper functions
|
||||
|
||||
|
@ -205,6 +206,62 @@ If -l is given, a map legend will be printed under the map."""
|
|||
def inv(self, args):
|
||||
print('\n'.join([i for i in self.gameBase.playerInv]))
|
||||
|
||||
def container(self, inv, cont):
|
||||
"""container IO"""
|
||||
# Pretty print: get length of the longest inventory item's name
|
||||
longestLen = 0
|
||||
longestStr = ""
|
||||
for i in inv:
|
||||
if len(i) > longestLen:
|
||||
longestLen = len(i)
|
||||
longestStr = i
|
||||
if longestLen > 0:
|
||||
print('{{0:<{0}}}{1}'.format(longestLen+2, "Container:").format("Inv:"))
|
||||
i = 0
|
||||
invKeys = tuple(inv.keys())
|
||||
while i < len(invKeys) and i < len(cont):
|
||||
print('{{0:<{0}}}{1}'.format(longestLen+2, cont[i].name).format(invKeys[i]))
|
||||
i += 1
|
||||
while i < len(invKeys):
|
||||
print(invKeys[i])
|
||||
i += 1
|
||||
while i < len(cont):
|
||||
print(' '*(longestLen+2) + cont[i].name)
|
||||
i += 1
|
||||
else:
|
||||
print('Container:')
|
||||
for i in cont:
|
||||
print(i.name)
|
||||
# Now, actually interacting with the container
|
||||
timeSpent = 0.5 # using a container always takes at least 1/2 second, even just opening and closing it again.
|
||||
instr = input("Take, store, or exit: ")
|
||||
while instr != "exit":
|
||||
instr = instr.split()
|
||||
if instr[0] == "take":
|
||||
# take something out of the container
|
||||
if instr[1] == "the":
|
||||
del instr[1]
|
||||
thing = ' '.join(instr[1:])
|
||||
for i in range(len(cont)):
|
||||
if thing == cont[i].name:
|
||||
inv[cont[i].name] = cont[i]
|
||||
del cont[i]
|
||||
timeSpent += 0.5
|
||||
print("{0} taken.".format(thing))
|
||||
break
|
||||
elif instr[0] == "store":
|
||||
# store something in the container
|
||||
if instr[1] == "the":
|
||||
del instr[1]
|
||||
thing = ' '.join(instr[1:])
|
||||
if thing in inv:
|
||||
cont.append(inv[thing])
|
||||
del inv[thing]
|
||||
print("{0} stored.".format(thing))
|
||||
timeSpent += 0.5
|
||||
instr = input("Take, store, or exit: ")
|
||||
return inv, cont, timeSpent
|
||||
|
||||
def update(self):
|
||||
self.gameBase.gameEventLoop()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue