Sunday, May 4, 2014

Day 29

Explored some Python at Gork Learning. Worked on, Hour of Code: The Dark Tunnel – adventure game! Spent more time on the while function and expanding on the code that tracks which room the player is in so that the game ends when the player gets to the "end" room.

The code I used:
room = 1
end = False
while not end:

# start room
  if room == 1:
    print("Actions available: look, forward")
    cmd = input("> ")
    if cmd == 'look':
        print("You are standing at the entrance to a long tunnel.")
    elif cmd == 'forward':
      room = 2

  # middle room
  elif room == 2:
    if not end:
      print("Actions available: look, forward, backward")
      cmd = input("> ")
      if cmd == 'look':
        print("You are in the depths of the tunnel.")
      elif cmd == 'forward':
        room = 3
      elif cmd == 'backward':
        room = 1

  # exit room
  else:
    print("You have escaped the tunnel. You win!")
    end = True


The interaction looked like this:
Actions available: look, forward
> backward
Actions available: look, forward
> look
You are standing at the entrance to a long tunnel.
Actions available: look, forward
> forward
Actions available: look, forward, backward
> look
You are in the depths of the tunnel.
Actions available: look, forward, backward
> backward
Actions available: look, forward
> look
You are standing at the entrance to a long tunnel.
Actions available: look, forward
> forward
Actions available: look, forward, backward
> forward
You have escaped the tunnel. You win!

No comments:

Post a Comment