Adventure Game 1

About this game…

  • Moving around three locations

Copy the following code into your IDE and run it.

places = ["A clearing in a forest", "An old wooden cabin", "A dark cave"]
moves = [{"n": 1, "s": 2},           {"s": 0},              {"n": 0}]
location = 0

def Main():
    ans = ""
    global location
    print(places[0])

    while ans != "bye":
        ans = input("What now?")
        if ans in moves[location]:
            location = moves[location].get(ans)
            print(places[location])
        else:
            print("I can't move that way")


Main()

Exercise

  1. How do you exit the game?
  2. What sort of data structure has been used to store the places?
  3. What sort of data structure has been used to store the moves?
  4. What does the variable location do?
  5. Give some valid moves.
  6. How does the code check if a move is valid?
  7. Which line of code sets the new location?
  8. Look at the map below. Add the extra locations to the game.
  9. Run the code and check that it works.

Leave a Comment