Last active
December 5, 2018 16:03
-
-
Save Sryn/b409776eebb9993b2c6bfde140e6a9f7 to your computer and use it in GitHub Desktop.
Jacks_Adventure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sryn - 20181205 | |
# based on https://www.youtube.com/watch?v=8uJFN7OZ2Yo&index=4&list=PLhP5GzqIk6qsYjU_3tod0nqoWGXlq9RvF | |
# which is a solution to '4. TextBased Adventure Game' of 'Five mini programming projects for the Python beginner' | |
# by https://twitter.com/tan_shelly | |
# at https://knightlab.northwestern.edu/2014/06/05/five-mini-programming-projects-for-the-python-beginner/ | |
import random | |
def msg(room): | |
if room['msg'] == '': #There is no custom message | |
return "You have entered the " + room['name'] + "." | |
else: | |
return room['msg'] | |
def get_choice(room, dir): | |
dir = dir.upper() | |
if dir == 'N': | |
choice = 0 | |
elif dir == 'S': | |
choice = 1 | |
elif dir == 'E': | |
choice = 2 | |
elif dir == 'W': | |
choice = 3 | |
elif dir == 'H': | |
return 5 #You stay in the current room | |
else: | |
return -1 | |
if room['directions'][choice] == 0: | |
return 4 | |
else: | |
return choice | |
def giant_moves(room_with_giant, show_giant): | |
choices = [] | |
if room_with_giant['directions'][0] != 0: | |
choices.append(0) | |
if room_with_giant['directions'][1] != 0: | |
choices.append(1) | |
if room_with_giant['directions'][2] != 0: | |
choices.append(2) | |
if room_with_giant['directions'][3] != 0: | |
choices.append(3) | |
choices.append(4) #make Gaint stay in current room | |
giant_choice = random.choice(choices) | |
if giant_choice != 4: | |
new_room_with_giant = room_with_giant['directions'][giant_choice] | |
head_or_still = 'head into' | |
else: | |
new_room_with_giant = room_with_giant | |
head_or_still = 'still be in' | |
if show_giant: | |
print('The Giant will ' + head_or_still + ' the ' + new_room_with_giant['name'] + '.') | |
return new_room_with_giant | |
def get_room_dir_choices(room): | |
choices = [] | |
if room['directions'][0] != 0: | |
choices.append(0) | |
if room['directions'][1] != 0: | |
choices.append(1) | |
if room['directions'][2] != 0: | |
choices.append(2) | |
if room['directions'][3] != 0: | |
choices.append(3) | |
return_string = '' | |
while len(choices) > 0: | |
if return_string != '': | |
if len(choices) > 1: | |
return_string += ', ' | |
elif len(choices) == 1: | |
return_string += ' or ' | |
current_dir = choices.pop(0) | |
if current_dir == 0: | |
return_string += 'N' | |
elif current_dir == 1: | |
return_string += 'S' | |
elif current_dir == 2: | |
return_string += 'E' | |
elif current_dir == 3: | |
return_string += 'W' | |
return return_string | |
def say_rumble(previous_room, current_room, room_with_giant): | |
if previous_room['name'] == room_with_giant['name']: | |
print('You hear thumps behind you!') | |
else: | |
choices = [] | |
if current_room['directions'][0] != 0: | |
choices.append(0) | |
if current_room['directions'][1] != 0: | |
choices.append(1) | |
if current_room['directions'][2] != 0: | |
choices.append(2) | |
if current_room['directions'][3] != 0: | |
choices.append(3) | |
if len(choices) > 0: | |
for x in choices: | |
x_room = current_room['directions'][x] | |
if x_room['name'] == room_with_giant['name']: | |
print('You feel the floor shake underneath you!') | |
break | |
def main(): | |
dirs = (0,0,0,0) #default directions | |
entrance = {'name':'Entrance Way', 'directions':dirs, 'msg':''} | |
antechamber = {'name':'Antechamber', 'directions':dirs, 'msg':''} | |
bedroom = {'name':'Bedroom', 'directions':dirs, 'msg':''} | |
bathroom = {'name':'Bathroom', 'directions':dirs, 'msg':''} | |
study = {'name':'Study', 'directions':dirs, 'msg':''} | |
kitchen = {'name':'Kitchen', 'directions':dirs, 'msg':''} | |
storeroom = {'name':'Storeroom', 'directions':dirs, 'msg':''} | |
diningroom = {'name':'Dining Room', 'directions':dirs, 'msg':''} | |
livingroom = {'name':'Living Room', 'directions':dirs, 'msg':''} | |
#directions are tuples: Rooms to the (N,S,E,W) | |
entrance['directions'] = (kitchen, 0, livingroom, antechamber) | |
antechamber['directions'] = (bedroom, 0, entrance, study) | |
bedroom['directions'] = (0, antechamber, 0, bathroom) | |
bathroom['directions'] = (0, study, bedroom, 0) | |
study['directions'] = (bathroom, 0, antechamber, 0) | |
kitchen['directions'] = (storeroom, entrance, diningroom, 0) | |
storeroom['directions'] = (0, kitchen, 0, 0) | |
diningroom['directions'] = (0, livingroom, 0, kitchen) | |
livingroom['directions'] = (diningroom, 0, 0, entrance) | |
#rooms where the Golden Goose might be | |
rooms = [antechamber, bedroom, bathroom, study, kitchen, storeroom, diningroom, livingroom] | |
room_with_golden_goose = random.choice(rooms) | |
golden_goose_taken = False | |
giant_awake = False | |
room_with_giant = room_with_golden_goose | |
show_where_giant_is = False | |
room = entrance | |
previous_room = room | |
gist_console_rerun = '\n\nType \'run jacks_adventure\' in the line below and press ENTER to play again.' | |
print('''\n\tWelcome to the Giant\'s Castle, Jack. Your mission, should you choose to | |
accept it, is to steal the Golden Goose and make your way back to the | |
entrance before the giant wakes up and catches you. Should you escape | |
successfully, the Giant will rampage all over SG!\n''') | |
have_escaped = False | |
while not have_escaped: | |
if giant_awake and room['name'] == room_with_giant['name']: | |
print('The Giant has caught up with you and immediately thumps you to a pulp. ' + | |
'Karma has claimed your thievering soul. YOU ARE DEAD.' + | |
gist_console_rerun) | |
break | |
elif giant_awake: | |
room_with_giant = giant_moves(room_with_giant, show_where_giant_is) | |
if golden_goose_taken and room['name'] == 'Entrance Way': | |
print('You\'ve stolen the Golden Goose and returned to the ' + | |
'entrance. You can now leave.\nCONGRATULATIONS!' + | |
gist_console_rerun) | |
have_escaped = True | |
elif not golden_goose_taken and room['name'] == room_with_golden_goose['name']: | |
golden_goose_taken = True | |
print(msg(room) + ' There\'s the Golden Goose and the Giant is ' + | |
'sleeping right next to it! You quietly clamp your hand ' + | |
'on the Golden Goose\'s beak, take the Golden Goose, hold ' + | |
'it tightly against your body and make a run for it.') | |
giant_awake = True | |
room['msg'] = ('You are back in the ' + room['name'] + | |
'! You\'ve already taken the Golden Goose. Get out of ' + | |
'here before the Giant catches you!') | |
stuck = True | |
else: | |
print(msg(room)) | |
if room['msg'] == '': | |
room['msg'] = 'You are back in the ' + room['name'] | |
stuck = True | |
stuck_once = False | |
while stuck: | |
if(stuck_once): | |
say_room = 'You are in the ' + room['name'] + '. ' | |
else: | |
say_room = '' | |
stuck_once = True | |
room_dir_choices = get_room_dir_choices(room) | |
if giant_awake: | |
say_rumble(previous_room, room, room_with_giant) | |
dir = raw_input(say_room + "Which direction do you want to go: " + room_dir_choices + "? ") | |
if dir == 'SG': #to toggle message to tell user which room the giant is in currently | |
show_where_giant_is = not show_where_giant_is | |
elif dir.upper() == 'Q': | |
print('You have decided to chicken out.' + gist_console_rerun) | |
have_escaped = True | |
break | |
choice = get_choice(room, dir) | |
if choice == -1: | |
print("Please enter " + room_dir_choices + ": ") | |
elif choice == 4: | |
print("You cannot go in that direction.") | |
elif choice == 5: #choice H | |
print("You decided to stay right where you are.") | |
stuck = False | |
else: | |
previous_room = room | |
room = room['directions'][choice] | |
stuck = False | |
main() |
- made the gist_console_rerun
- commented in credits at the top
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First Commit