2

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?

yamax87
  • 63
  • 1
  • 7

3 Answers3

1

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.

damaredayo
  • 1,048
  • 6
  • 19
  • Thanks, that works well. I think I got fixated on having the variable current as a list entry, and didn't consider that it could just be an int. – yamax87 Mar 13 '20 at 11:09
  • No problem, if that solves your problem mark it as answered by clicking the tick below the arrows on the answer :) – damaredayo Mar 13 '20 at 11:40
1

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.

chuck
  • 1,420
  • 4
  • 19
0

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']
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • Thanks, makes sense generally. But isn't entering all the possible 'next' and 'prev' states quite hard work? In 2d maps, there would be even more states to add (up/down). Of course, you could do a script to generate the prev/next states, but that's beyond my very simple current grasp of Python. – yamax87 Mar 13 '20 at 11:07
  • 1
    @yamax87 If you can lay out the world on a grid (be it 1d or 2d coordinates), then calculating these state transitions (as the other answers suggest) might actually be the way to go. You can still add more specific transitions this way though (e.g., adding stairs or caves to access another "z level"). – moooeeeep Mar 13 '20 at 11:21