Reputation: 73
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?
Upvotes: 2
Views: 89
Reputation: 32502
If you want it scalable, probably some state machine implementation that you can populate via some externally loaded text file would be preferable. These threads might contain some pointers in this direction:
If you want to keep it simple, make your map a dictionary. There you can also contain the state transitions. Optionally, you can add a sub-dictionary that contains possibly state-specific available actions and the corresponding state transitions.
Untested example:
map = {
'room0': {'next': 'room1', 'prev': 'room4', 'description': '...'},
'room1': {'next': 'room2', 'prev': 'room0', 'description': '...'},
'room2': {'next': 'room3', 'prev': 'room1', 'description': 'The entrance'},
'room3': {'next': 'room4', 'prev': 'room2', 'description': '...'},
'room4': {'next': 'room0', 'prev': 'room3', 'description': '...'},
}
current_state = 'room2'
# transitions be like
next_state = map[current_state]['next']
prev_state = map[current_state]['prev']
Upvotes: 0
Reputation: 1447
Have you tried simply having some sort of index to the players's position? Then if the player is in the first room the index is 0 for example, and you can do:
index = index + 1 if index + 1 < len(gameMap) else index
when going forward and
index = index - 1 if index > 0 else index
when going backwards.
Upvotes: 1
Reputation: 1079
What you can do in this case is have a variable which is used as the point in which the player is at; for example:
current = 0
then do whatever calculations you must and call playerLocation = gameMap[current]
If you wanted to go back a level, you can just do:
current -= 1
playerLocation = gameMap[current]
I'm not sure if I'm missing a part of your question as this seems relatively straight forward; correct me if I misunderstood.
Upvotes: 1