I am putting together a very simple text-based game in Python which contains 5 rooms that the player can move between.
The player starts in the central room, gameMap[2].
Below is the code that provides the player's location.
gameMap = ['room0','room1','room2','room3','room4']
playerLocation = gameMap[2]
Now suppose the player wants to move to the left.
This means that I must assign playerLocation to gameMap[1]
.
playerLocation = gameMap[1]
With just 5 rooms, this could be done reasonably easily. But for scaling purposes, I want to be able to assign playerLocation to 'the current list entry -1', assuming this is in range.
This instruction would make assignments as follows:
if playerLocation is gameMap[4], playerLocation = gameMap[3]
if playerLocation is gameMap[1], playerLocation = gameMap[0]
I have considered using next(gameMap) but this seems to present 2 issues:
1) It does not allow me to reference the player's current location, since it must start at gameMap[0]
2) It does not seem to work in reverse, and there doesn't seem to be a previous() function owing to Python's architecture. (sorry if I've got the wrong terminology here :).
I have looked everywhere but cannot seem to find any way to do this. Is it possible?