Skip to content

Instantly share code, notes, and snippets.

@FardinBehboudi
Created March 31, 2017 09:22
Show Gist options
  • Save FardinBehboudi/4ff50f00cf5c79d173c394119a420efe to your computer and use it in GitHub Desktop.
Save FardinBehboudi/4ff50f00cf5c79d173c394119a420efe to your computer and use it in GitHub Desktop.
TextBaset Advanture Game in python
# TextBaset Advanture Game
import random
print 'lets start the text based advanture game '
def msg(room):
if room['msg'] == '':
return 'you have entered to : ' + room['name']
else:
return room['msg']
def get_choice(room,walk):
if walk == 'n':
choice = 0
elif walk == 'e':
choice = 1
elif walk == 's':
choice = 2
elif walk == 'w':
choice = 3
else:
return -1
if room['directions'][choice] == 0:
return 4
else :
return choice
def main():
dirs = (0,0,0,0) # defualt Direction
entrance = {'name': 'Entrance Way', 'directions': dirs, 'msg': '' }
livingroom = {'name': 'Living room', 'directions': dirs, 'msg': '' }
hallway = {'name': 'Hallway', 'directions': dirs, 'msg': '' }
kitchen = {'name': 'Kitchen', 'directions': dirs, 'msg': '' }
diningroom = {'name': 'Dining Room', 'directions': dirs, 'msg': '' }
family_room = {'name': 'Family room', 'directions': dirs, 'msg': '' }
#directions are tuple , rooms are like this (N,E,S,W)
entrance['directions'] = (kitchen,livingroom,0,0)
livingroom['directions'] = (diningroom,0,0,entrance)
hallway['directions'] = (0,kitchen,0,family_room)
kitchen['directions'] = (0,diningroom,entrance,hallway)
diningroom['directions'] = (0,0,livingroom,kitchen)
family_room['directions'] = (0,hallway,0,0)
rooms = [livingroom,hallway,kitchen,diningroom,family_room,]
room_with_eggs = random.choice(rooms)
egg_delivered = False
room = entrance
while True:
if egg_delivered and room['name'] == 'Entrance Way':
print ('you have divered the egg and returned to home. now you ccan leave')
break;
elif not egg_delivered and room['name'] == room_with_eggs['name'] :
egg_delivered = True
print msg(room) + 'the egg is there get it and leave'
room['name'] = ('you are backin room' + room['name']+ 'you are done go out')
else:
print msg(room)
room['msg'] = 'you are backin room' + room['name']+ 'you are done go out'
stuck = True
while stuck:
walk = raw_input('which direction you wanna go? n, e, s,w ')
choice = get_choice(room, walk)
if choice == -1 :
print ('yyyyyyyyou should choose the direction? ')
elif choice == 4 :
print ('there is nnnnnnnno way choose again? ')
else :
room = room['directions'][choice]
stuck = False
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment