├── LICENSE ├── README.md ├── beginners_python3.pdf ├── formatting.pdf ├── game_01.py ├── game_01_comments.py ├── game_02.py ├── game_02_comments.py ├── game_03.py ├── game_04.py ├── game_05.py ├── game_06.py ├── game_07.py ├── game_07_comments.py ├── game_08.py ├── game_08_comments.py ├── game_09.py ├── game_10.py ├── game_10_with_design.py └── mentor-notes.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Coding Grace 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![alt Coding Grace](https://www.codinggrace.com/static/images/cg-logo.png) 2 | 3 | # PRE-REQUISITES 4 | 5 | Your laptop with 3.6.x (onwards) installed. 6 | 7 | NOTE: Those with Linux and MacOSX would have Python installed by default, no action required. 8 | 9 | Windows: Download the version for your laptop via [https://www.python.org/downloads/](https://www.python.org/downloads/) 10 | 11 | 12 | # NOTES 13 | 14 | - In your preferred editor, make sure indentation is set to "4 spaces". 15 | - There's an [instructor's guide](https://coding-grace-guide.readthedocs.io/en/latest/guide/lessonplans/beginners-python-text-based-adventure.html) that accompany this workshop. 16 | 17 | # REFERENCES 18 | 19 | * Inspired by Learning Python the Hard Way: [http://learnpythonthehardway.org/](http://learnpythonthehardway.org/) (See Chapter 43 on one way of implementing FSM) 20 | * Other resources: [http://www.codinggrace.com/resources/](http://www.codinggrace.com/resources/) 21 | -------------------------------------------------------------------------------- /beginners_python3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codinggrace/text_based_adventure_game/c0944f57d790dcd9be161b04b1372014d2c77d49/beginners_python3.pdf -------------------------------------------------------------------------------- /formatting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codinggrace/text_based_adventure_game/c0944f57d790dcd9be161b04b1372014d2c77d49/formatting.pdf -------------------------------------------------------------------------------- /game_01.py: -------------------------------------------------------------------------------- 1 | def say_hello(): 2 | ''' 3 | Prints hello string when run from command line 4 | ''' 5 | print("hello") 6 | 7 | if __name__ == '__main__': 8 | say_hello() -------------------------------------------------------------------------------- /game_01_comments.py: -------------------------------------------------------------------------------- 1 | # Some notes: 2 | # a) The '#' symbols mean comments and Python will ignore these lines of code 3 | # b) When indenting code, it's always "4 spaces". Some editors allow you to use tabs 4 | # and it converts it to 4 spaces automatically for you. 5 | # 6 | # This program just outputs text (or string) "hello" to the screen 7 | 8 | # This is a function called main 9 | def say_hello(): 10 | ''' 11 | Prints hello string when run from command line 12 | ''' 13 | print("hello") 14 | 15 | # 1. To run this program, open your terminal. 16 | # 2. Type the following: python game_01.py 17 | # 3. Hit return 18 | 19 | # :::: Activities ::: 20 | # Open your Python interpretor by typing python and hit return, and >>> should appear. 21 | # Try using single quotes instead of double quotes 22 | # What happens when you mix quotes. Examples to try:- 23 | # print("hello') 24 | # print('hello") 25 | # print('I'm going outside') 26 | # How will you fix the print statements? 27 | # When Python encounters a quote, it expects it to be closed with the same quote 28 | # e.g. you open with a single quote, you close it with a single quote 29 | # 30 | # Try printing numbers 31 | # 32 | # CONCATENATION - joins up the string 33 | # print("hello, " + "how are you") 34 | # You can't mix numbers and strings, you'll get an error, e.g. print("hello" + 1) 35 | # 36 | # ESCAPING STRINGS - use a \ 37 | # The following example should now work 38 | # print('I\'m going outside') 39 | 40 | 41 | # Reference in docs: https://docs.python.org/3/library/__main__.html 42 | # If you run this python file it will have a standalone application that 43 | # has it's defined entry point and won't execute everything in the Python 44 | # file all at once. 45 | if __name__ == '__main__': 46 | # This calls a function called "main" 47 | say_hello() -------------------------------------------------------------------------------- /game_02.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | ''' 3 | Getting your name using in-built function called input(). 4 | 5 | Prints the players name. 6 | 7 | There will be some editing of the code in this part of the workshop 8 | showing how to declare, assign and call a variable. 9 | ''' 10 | print(input("What's your name? > ")) 11 | 12 | if __name__ == '__main__': 13 | main() -------------------------------------------------------------------------------- /game_02_comments.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | ''' 3 | Getting your name using in-built function called input(). 4 | 5 | Prints the players name. 6 | 7 | There will be some editing of the code in this part of the workshop 8 | showing how to declare, assign and call a variable. 9 | ''' 10 | ## We are going to use raw_input so we can have some interaction with player of game 11 | ## The " > " is just there for decorations, try using other characters. 12 | 13 | ## 1 - Getting your name 14 | print(input("What's your name? > ")) 15 | ## Now run it, after you type your name, it prints it on the next line. 16 | 17 | ## 1.2 - Refined 18 | ## 19 | ## Comment out line 6 by putting # where the code the line starts 20 | ## Uncomment lines 17 and 23 21 | 22 | ## player_name is a variable, it is created to store objects like strings, numbers 23 | ## Remember to give variables memorable names or else when you review your code 24 | ## you don't know what it is used for. 25 | #player_name = input("What's your name? >") 26 | 27 | ## The following is formatted string literal. The {} tells Python that there is something to 28 | ## be substituted. We are using fstrings to substitute the variable player_name. 29 | ## More info: https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals 30 | # 31 | #print(f"Your name is {player_name}") 32 | 33 | ## Now uncomment line 28 34 | ## 35 | ## Some neat tricks with string manipulations:- 36 | ## This turns your string all to uppercase 37 | #print(f"Your name is {player_name.upper()}") 38 | 39 | ## Open your Python interpreter, try the following 40 | ## Remember, in terminal, when you type Python and hit return, you should see >>> 41 | ## >>> player_name = "bob" 42 | ## >>> print(f"Your name is {player_name.upper()}") 43 | ## 44 | ## Try other string built-in functions. 45 | ## Find it in Python docs by 46 | ## 1) https://www.python.org/ 47 | ## 2) Click on Docs 48 | ## 3) Click on "Library Reference" 49 | ## 4) Look for "Text Sequence Type - str" and read up the various functions (or methods) 50 | ## and experiment in your Python interpretor. 51 | ## 52 | ## Don't be afraid to your your Python interpreter, we use it all the time to test and try 53 | ## out everything. Most Python code you right, you should be able to test in the interpreter. 54 | 55 | if __name__ == '__main__': 56 | main() -------------------------------------------------------------------------------- /game_03.py: -------------------------------------------------------------------------------- 1 | def start_adventure(): 2 | ''' 3 | This function starts the adventure by allowing two options for 4 | players to choose from: red or blue door 5 | 6 | Chosen option will print out the door chosen. 7 | ''' 8 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 9 | door_picked = input("Do you pick the red door or blue door? > ") 10 | 11 | # IF STATEMENTS 12 | # door_picked variable contains whatever the player types in. 13 | if door_picked == "red": 14 | print("You picked the red door") 15 | elif door_picked == "blue": 16 | print("You picked the blue door") 17 | else: 18 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 19 | 20 | # Run the program a few times testing out the different answers. 21 | 22 | def main(): 23 | ''' 24 | Gets the players name, print it out and starts the adventure. 25 | ''' 26 | player_name = input("What's your name? >") 27 | print(f"Your name is {player_name}") 28 | 29 | # Calls another function, declare it above. 30 | start_adventure() 31 | 32 | if __name__ == '__main__': 33 | main() -------------------------------------------------------------------------------- /game_04.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # Each door leads to a room and we need to do something, in the red room 3 | # specifically. 4 | 5 | ##### ROOMS ##### 6 | def blue_door_room(): 7 | ''' 8 | This is a blue door room. 9 | 10 | Nothing happens here, let's do one room at the time. :-) 11 | ''' 12 | print("The door knob jiggles but nothing happens.") 13 | return 14 | 15 | def red_door_room(): 16 | ''' 17 | The red door rooom contains a red dragon. 18 | 19 | If a player types "flee" as an answer, player returns to the room with 20 | two doors, otherwise the player dies. 21 | ''' 22 | print("There you see a great red dragon.") 23 | print("It stares at you through one narrowed eye.") 24 | print("Do you flee for your life or stay?") 25 | 26 | next_move = input("> ") 27 | 28 | # Flee to return to the start of the game, in the room with the blue and 29 | # red door or die! 30 | if "flee" in next_move: 31 | start_adventure() 32 | else: 33 | print("It eats you. Well, that was tasty!") 34 | ### END ROOMS ### 35 | 36 | def start_adventure(): 37 | ''' 38 | This function starts the adventure by allowing two options for 39 | players to choose from: red or blue door 40 | 41 | Chosen option will print out the door chosen. 42 | ''' 43 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 44 | door_picked = input("Do you pick the red door or blue door? > ") 45 | 46 | # Pick a door and we go to a room and something else happens 47 | if door_picked == "red": 48 | # This calls a function that contains stuff that happens in red_door_room 49 | red_door_room() 50 | elif door_picked == "blue": 51 | # This calls a function that contains stuff that happens in blue_door_room 52 | blue_door_room() 53 | else: 54 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 55 | 56 | def main(): 57 | ''' 58 | Gets the players name, print it out and starts the adventure. 59 | ''' 60 | player_name = input("What's your name? >") 61 | print(f"Your name is {player_name.upper()}") 62 | 63 | start_adventure() 64 | 65 | if __name__ == '__main__': 66 | main() -------------------------------------------------------------------------------- /game_05.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # We are still in the red room, and we updated this program with the 3 | # function you_died with a given reason. 4 | # 5 | # Run this code a few times and see what happens with different choices. 6 | # It's good to test all options and see if that's what you expected. 7 | 8 | ##### ACTIONS ##### 9 | def you_died(why): 10 | ''' 11 | In: Passing in the string showing player how they dies 12 | 13 | Result: 14 | Prints reason why they player died. 15 | Programme exits without error. 16 | ''' 17 | # You expect a reason why the player died. It's a string. 18 | print(f"{why}. Good job!") 19 | 20 | # This exits the program entirely. 21 | exit(0) 22 | 23 | ### END ACTIONS ### 24 | 25 | ##### ROOMS ##### 26 | def blue_door_room(): 27 | ''' 28 | This is a blue door room. 29 | 30 | Nothing happens here, let's do one room at the time. :-) 31 | ''' 32 | print("The door knob jiggles but nothing happens.") 33 | return 34 | 35 | def red_door_room(): 36 | ''' 37 | The red door rooom contains a red dragon. 38 | 39 | If a player types "flee" as an answer, player returns to the room with two doors, 40 | otherwise the player dies. 41 | ''' 42 | print("There you see a great red dragon.") 43 | print("It stares at you through one narrowed eye.") 44 | print("Do you flee for your life or stay?") 45 | 46 | next_move = input("> ") 47 | 48 | # Flee to return to the start of the game, in the room with the blue and red door or die! 49 | if "flee" in next_move: 50 | start_adventure() 51 | else: 52 | # You call the function you_died and pass the reason why you died as 53 | # a string as an argument. 54 | you_died("It eats you. Well, that was tasty!") 55 | ### END ROOMS ### 56 | 57 | def start_adventure(): 58 | ''' 59 | This function starts the adventure by allowing two options for 60 | players to choose from: red or blue door 61 | 62 | Chosen option will print out the door chosen. 63 | ''' 64 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 65 | door_picked = input("Do you pick the red door or blue door? > ") 66 | 67 | # Pick a door and we go to a room and something else happens 68 | if door_picked == "red": 69 | red_door_room() 70 | elif door_picked == "blue": 71 | blue_door_room() 72 | else: 73 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 74 | 75 | def main(): 76 | ''' 77 | Gets the players name, print it out and starts the adventure. 78 | ''' 79 | player_name = input("What's your name? >") 80 | print(f"Your name is {player_name.upper()}") 81 | 82 | start_adventure() 83 | 84 | if __name__ == '__main__': 85 | main() -------------------------------------------------------------------------------- /game_06.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # We are in the blue room. We find a treasure chest and a sleeping guard in 3 | # front of a door. 4 | # 5 | # It's all about lists in this section 6 | # 7 | # Run this code a few times and see what happens with different choices. 8 | # It's good to test all options and see if that's what you expected. 9 | 10 | ##### ACTIONS ##### 11 | def you_died(why): 12 | ''' 13 | In: Passing in the string showing player how they dies 14 | 15 | Result: 16 | Prints reason why they player died. 17 | Programme exits without error. 18 | ''' 19 | # You expect a reason why the player died. It's a string. 20 | print(f"{why}. Good job!") 21 | 22 | # This exits the program entirely. 23 | exit(0) 24 | 25 | ### END ACTIONS ### 26 | 27 | ##### ROOMS ##### 28 | def blue_door_room(): 29 | ''' 30 | The player finds a treasure chest, options to investigate the treasure 31 | chest or guard. 32 | ''' 33 | # The variable treasure_chest is an object type called a list 34 | # A list maybe empty as well. 35 | # So our treasure_chest list contains 4 items. 36 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 37 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 38 | 39 | # Ask player what to do. 40 | action = input("What do you do? > ") 41 | 42 | # This is a way to see if the text typed by player is in the list 43 | if action.lower() in ["treasure", "chest", "left"]: 44 | print("Oooh, treasure!") 45 | else: 46 | print("The guard is more interesting, let's go that way!") 47 | 48 | 49 | def red_door_room(): 50 | ''' 51 | The red door rooom contains a red dragon. 52 | 53 | If a player types "flee" as an answer, player returns to the room with 54 | two doors, otherwise the player dies. 55 | ''' 56 | print("There you see a great red dragon.") 57 | print("It stares at you through one narrowed eye.") 58 | print("Do you flee for your life or stay?") 59 | 60 | next_move = input("> ") 61 | 62 | # Flee to return to the start of the game, in the room with the blue and 63 | # red door or die! 64 | if "flee" in next_move: 65 | start_adventure() 66 | else: 67 | # You call the function you_died and pass the reason why you died as 68 | # a string as an argument. 69 | you_died("It eats you. Well, that was tasty!") 70 | ### END ROOMS ### 71 | 72 | def start_adventure(): 73 | ''' 74 | This function starts the adventure by allowing two options for 75 | players to choose from: red or blue door 76 | 77 | Chosen option will print out the door chosen. 78 | ''' 79 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 80 | door_picked = input("Do you pick the red door or blue door? > ") 81 | 82 | # Pick a door and we go to a room and something else happens 83 | if door_picked == "red": 84 | red_door_room() 85 | elif door_picked == "blue": 86 | blue_door_room() 87 | else: 88 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 89 | 90 | def main(): 91 | ''' 92 | Gets the players name, print it out and starts the adventure. 93 | ''' 94 | player_name = input("What's your name? >") 95 | print(f"Your name is {player_name.upper()}") 96 | 97 | start_adventure() 98 | 99 | if __name__ == '__main__': 100 | main() -------------------------------------------------------------------------------- /game_07.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # We are still in the blue room. What do we do with the treasure chest? 3 | # New code starts at line 46 4 | # 5 | # Loop and another way of doing if statment 6 | # 7 | # Run this code a few times and see what happens with different choices. 8 | # It's good to test all options and see if that's what you expected. 9 | 10 | ##### ACTIONS ##### 11 | def you_died(why): 12 | ''' 13 | In: Passing in the string showing player how they dies 14 | 15 | Result: 16 | Prints reason why they player died. 17 | Programme exits without error. 18 | ''' 19 | print(f"{why}. Good job!") 20 | 21 | # This exits the program entirely. 22 | exit(0) 23 | 24 | ### END ACTIONS ### 25 | 26 | ##### ROOMS ##### 27 | def blue_door_room(): 28 | ''' 29 | The player finds a treasure chest, options to investigate the treasure chest or guard. 30 | 31 | If player chooses 32 | - Treasure chest: show its contents 33 | - Guard: nothing for now 34 | ''' 35 | # So our treasure_chest list contains 4 items. 36 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 37 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 38 | 39 | # Ask player what to do. 40 | action = input("What do you do? > ") 41 | 42 | # This is a way to see if the text typed by player is in the list 43 | if action.lower() in ["treasure", "chest", "left"]: 44 | print("Oooh, treasure!") 45 | 46 | ### NEW CODE STARTS HERE ### 47 | print("Open it? Press '1'") 48 | print("Leave it alone. Press '2'") 49 | choice = input("> ") 50 | 51 | if choice == "1": 52 | print("Let's see what's in here... /grins") 53 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 54 | print("You find some") 55 | 56 | # FOR LOOP 57 | # for each treasure (variable created on the fly in the for loop) 58 | # in the treasure_chest list, print the treasure. 59 | for treasure in treasure_chest: 60 | print(treasure) 61 | print("The guard is more interesting, let's go that way!") 62 | 63 | def red_door_room(): 64 | ''' 65 | The red door rooom contains a red dragon. 66 | 67 | If a player types "flee" as an answer, player returns to the room with two doors, 68 | otherwise the player dies. 69 | ''' 70 | print("There you see a great red dragon.") 71 | print("It stares at you through one narrowed eye.") 72 | print("Do you flee for your life or stay?") 73 | 74 | next_move = input("> ") 75 | 76 | # Flee to return to the start of the game, in the room with the blue and red door or die! 77 | if "flee" in next_move: 78 | start_adventure() 79 | else: 80 | # You call the function you_died and pass the reason why you died as 81 | # a string as an argument. 82 | you_died("It eats you. Well, that was tasty!") 83 | ### END ROOMS ### 84 | 85 | def start_adventure(): 86 | ''' 87 | This function starts the adventure by allowing two options for 88 | players to choose from: red or blue door 89 | 90 | Chosen option will print out the door chosen. 91 | ''' 92 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 93 | door_picked = input("Do you pick the red door or blue door? > ") 94 | 95 | # Pick a door and we go to a room and something else happens 96 | if door_picked == "red": 97 | red_door_room() 98 | elif door_picked == "blue": 99 | blue_door_room() 100 | else: 101 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 102 | 103 | def main(): 104 | ''' 105 | Gets the players name, print it out and starts the adventure. 106 | ''' 107 | player_name = input("What's your name? >") 108 | print(f"Your name is {player_name.upper()}") 109 | 110 | start_adventure() 111 | 112 | if __name__ == '__main__': 113 | main() -------------------------------------------------------------------------------- /game_07_comments.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to 2 | # choose from. 3 | # We are still in the blue room. What do we do with the treasure chest? 4 | # New code starts at line 33 5 | # 6 | # Run this code a few times and see what happens with different choices. 7 | # It's good to test all options and see if that's what you expected. 8 | 9 | ##### ACTIONS ##### 10 | def you_died(why): 11 | ''' 12 | In: Passing in the string showing player how they dies 13 | 14 | Result: 15 | Prints reason why they player died. 16 | Programme exits without error. 17 | ''' 18 | print(f"{why}. Good job!") 19 | 20 | # This exits the program entirely. 21 | exit(0) 22 | 23 | ### END ACTIONS ### 24 | 25 | ##### ROOMS ##### 26 | def blue_door_room(): 27 | ''' 28 | The player finds a treasure chest, options to investigate the treasure 29 | chest or guard. 30 | 31 | If player chooses 32 | - Treasure chest: show its contents 33 | - Guard: nothing for now 34 | ''' 35 | # The variable treasure_chest is an object type called a list 36 | # A list maybe empty as well. 37 | # So our treasure_chest list contains 4 items. 38 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 39 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 40 | 41 | # Ask player what to do. 42 | action = input("What do you do? > ") 43 | 44 | # This is a way to see if the text typed by player is in the list 45 | if action.lower() in ["treasure", "chest", "left"]: 46 | print("Oooh, treasure!") 47 | 48 | print("Open it? Press '1'") 49 | print("Leave it alone. Press '2'") 50 | choice = input("> ") 51 | 52 | # Try just leaving 1 and 2 as a number 53 | # Change to string and see what happens 54 | if choice == "1": 55 | print("Let's see what's in here... /grins") 56 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 57 | print("You find some") 58 | 59 | # FOR LOOP 60 | # for each treasure (variable created on the fly in the for loop) 61 | # in the treasure_chest list, print the treasure. 62 | for treasure in treasure_chest: 63 | print(treasure) 64 | 65 | # Tip: Type this in your Python interpretor to see how it works. 66 | # >>> treasure_chest = ["diamonds", "gold", "silver", "sword"] 67 | # >>> for treasure in treasure_chest: 68 | # >>> print(treasure) 69 | # 70 | # Things to do while you are in the interpretor 71 | # >>> treasure_chest[0] 72 | # This will print out the first item in the list. 73 | # Remember, in almost all programming languages, everything starts 74 | # at "0". 75 | # Try getting the 2nd, 3rd and 4th item in the list. 76 | # 77 | # >>> treasure_chest[0:2] 78 | # You will see the result printed on the next line 79 | # It gives you the first two items on the list. 80 | # 81 | # Try playing around some more. 82 | # 83 | # More info: https://docs.python.org/3/library/functions.html#slice 84 | 85 | 86 | print("The guard is more interesting, let's go that way!") 87 | 88 | def red_door_room(): 89 | ''' 90 | The red door rooom contains a red dragon. 91 | 92 | If a player types "flee" as an answer, player returns to the room with 93 | two doors, otherwise the player dies. 94 | ''' 95 | print("There you see a great red dragon.") 96 | print("It stares at you through one narrowed eye.") 97 | print("Do you flee for your life or stay?") 98 | 99 | next_move = input("> ") 100 | 101 | # Flee to return to the start of the game, in the room with the blue 102 | # and red door or die! 103 | if "flee" in next_move: 104 | start_adventure() 105 | else: 106 | # You call the function you_died and pass the reason why you died as 107 | # a string as an argument. 108 | you_died("It eats you. Well, that was tasty!") 109 | ### END ROOMS ### 110 | 111 | def start_adventure(): 112 | ''' 113 | This function starts the adventure by allowing two options for 114 | players to choose from: red or blue door 115 | 116 | Chosen option will print out the door chosen. 117 | ''' 118 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 119 | door_picked = input("Do you pick the red door or blue door? > ") 120 | 121 | # Pick a door and we go to a room and something else happens 122 | if door_picked == "red": 123 | red_door_room() 124 | elif door_picked == "blue": 125 | blue_door_room() 126 | else: 127 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 128 | 129 | def main(): 130 | ''' 131 | Gets the players name, print it out and starts the adventure. 132 | ''' 133 | player_name = input("What's your name? >") 134 | print(f"Your name is {player_name.upper()}") 135 | 136 | start_adventure() 137 | 138 | if __name__ == '__main__': 139 | main() -------------------------------------------------------------------------------- /game_08.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # We are still in the blue room. What do we do with the treasure chest? 3 | # 4 | 5 | ##### ACTIONS ##### 6 | def you_died(why): 7 | ''' 8 | In: Passing in the string showing player how they dies 9 | 10 | Result: 11 | Prints reason why they player died. 12 | Programme exits without error. 13 | ''' 14 | print(f"{why}. Good job!") 15 | 16 | # This exits the program entirely. 17 | exit(0) 18 | 19 | ### END ACTIONS ### 20 | 21 | ##### ROOMS ##### 22 | def blue_door_room(): 23 | ''' 24 | The player finds a treasure chest, options to investigate the treasure chest or guard. 25 | 26 | If player chooses 27 | - Treasure chest: show its contents; option to take treasure or ignore it (proceeds to guard) 28 | - Guard: nothing for now 29 | ''' 30 | # So our treasure_chest list contains 4 items. 31 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 32 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 33 | 34 | # Ask player what to do. 35 | action = input("What do you do? > ") 36 | 37 | # This is a way to see if the text typed by player is in the list 38 | if action.lower() in ["treasure", "chest", "left"]: 39 | print("Oooh, treasure!") 40 | 41 | print("Open it? Press '1'") 42 | print("Leave it alone. Press '2'") 43 | choice = input("> ") 44 | 45 | if choice == "1": 46 | print("Let's see what's in here... /grins") 47 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 48 | print("You find some") 49 | 50 | # for each treasure (variable created on the fly in the for loop) 51 | # in the treasure_chest list, print the treasure. 52 | for treasure in treasure_chest: 53 | print(treasure) 54 | 55 | # So much treasure, what to do? Take it or leave it. 56 | print("What do you want to do?") 57 | 58 | # Get number of items in treasure chest with len)) 59 | num_items_in_chest = len(treasure_chest) 60 | 61 | print(f"Take all {num_items_in_chest} treasure, press '1'") 62 | print("Leave it, press '2'") 63 | 64 | treasure_choice = input("> ") 65 | if treasure_choice == "1": 66 | treasure_chest.remove("sword") 67 | print("\tYou take the shinier sword from the treasure chest. It does looks exceedingly shiney.") 68 | print("\tWoohoo! Bounty and a shiney new sword. /drops your crappy sword in the empty treasure chest.") 69 | 70 | temp_treasure_list = treasure_chest[:] 71 | treasure_contents = ", ".join(treasure_chest) 72 | print(f"\tYou also receive {treasure_contents}.") 73 | 74 | # Removing all the rest of the items in the treasure chest 75 | for treasure in temp_treasure_list: 76 | # Use list remove() function to remove each item in the chest. 77 | treasure_chest.remove(treasure) 78 | 79 | # Add the old sword in place of the new sword 80 | treasure_chest.append("crappy sword") 81 | print(f"\tYou close the lid of the chest containing {treasure_chest} for the next adventurer. /grins") 82 | print("Now onward to get past this sleeping guard and the door to freedom.") 83 | elif treasure_choice == "2": 84 | print("It will still be here (I hope), right after I get past this guard") 85 | elif choice == "2": 86 | print("The guard is more interesting, let's go that way!") 87 | elif action.lower() in ["guard", "right"]: 88 | print("The guard is more interesting, let's go that way!") 89 | else: 90 | print("Well, not sure what you picked there, let's poke the guard cos it's fun!") 91 | 92 | 93 | def red_door_room(): 94 | ''' 95 | The red door rooom contains a red dragon. 96 | 97 | If a player types "flee" as an answer, player returns to the room with two doors, 98 | otherwise the player dies. 99 | ''' 100 | print("There you see a great red dragon.") 101 | print("It stares at you through one narrowed eye.") 102 | print("Do you flee for your life or stay?") 103 | 104 | next_move = input("> ") 105 | 106 | # Flee to return to the start of the game, in the room with the blue and red door or die! 107 | if "flee" in next_move: 108 | start_adventure() 109 | else: 110 | # You call the function you_died and pass the reason why you died as 111 | # a string as an argument. 112 | you_died("It eats you. Well, that was tasty!") 113 | ### END ROOMS ### 114 | 115 | def start_adventure(): 116 | ''' 117 | This function starts the adventure by allowing two options for 118 | players to choose from: red or blue door 119 | 120 | Chosen option will print out the door chosen. 121 | ''' 122 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 123 | door_picked = input("Do you pick the red door or blue door? > ") 124 | 125 | # Pick a door and we go to a room and something else happens 126 | if door_picked == "red": 127 | red_door_room() 128 | elif door_picked == "blue": 129 | blue_door_room() 130 | else: 131 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 132 | 133 | def main(): 134 | ''' 135 | Gets the players name, print it out and starts the adventure. 136 | ''' 137 | player_name = input("What's your name? >") 138 | print(f"Your name is {player_name.upper()}") 139 | 140 | start_adventure() 141 | 142 | if __name__ == '__main__': 143 | main() -------------------------------------------------------------------------------- /game_08_comments.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # We are still in the blue room. What do we do with the treasure chest? 3 | # New code starts at line 52 4 | # 5 | # Length of a list, excaping characters and more string manipulations 6 | # 7 | # Run this code a few times and see what happens with different choices. 8 | # It's good to test all options and see if that's what you expected. 9 | 10 | ##### ACTIONS ##### 11 | def you_died(why): 12 | ''' 13 | In: Passing in the string showing player how they dies 14 | 15 | Result: 16 | Prints reason why they player died. 17 | Programme exits without error. 18 | ''' 19 | print(f"{why}. Good job!") 20 | 21 | # This exits the program entirely. 22 | exit(0) 23 | 24 | ### END ACTIONS ### 25 | 26 | ##### ROOMS ##### 27 | def blue_door_room(): 28 | ''' 29 | The player finds a treasure chest, options to investigate the treasure chest or guard. 30 | 31 | If player chooses 32 | - Treasure chest: show its contents; option to take treasure or ignore it (proceeds to guard) 33 | - Guard: nothing for now 34 | ''' 35 | # The variable treasure_chest is an object type called a list 36 | # A list maybe empty as well. 37 | # So our treasure_chest list contains 4 items. 38 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 39 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 40 | 41 | # Ask player what to do. 42 | action = input("What do you do? > ") 43 | 44 | # This is a way to see if the text typed by player is in the list 45 | if action.lower() in ["treasure", "chest", "left"]: 46 | print("Oooh, treasure!") 47 | 48 | print("Open it? Press '1'") 49 | print("Leave it alone. Press '2'") 50 | choice = input("> ") 51 | 52 | # Try just leaving 1 and 2 as a number 53 | # Change to string and see what happens 54 | if choice == "1": 55 | print("Let's see what's in here... /grins") 56 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 57 | print("You find some") 58 | 59 | # for each treasure (variable created on the fly in the for loop) 60 | # in the treasure_chest list, print the treasure. 61 | for treasure in treasure_chest: 62 | print(treasure) 63 | 64 | # So much treasure, what to do? Take it or leave it. 65 | print("What do you want to do?") 66 | 67 | # INTRODUCING len() 68 | # Go to the Python interpreter. 69 | # >>> treasure_chest = ["diamonds", "gold", "silver", "sword"] 70 | # >>> len(treasure_chest) 71 | # This should give you how many items is in a list. 72 | # 73 | # >>> len("diamonds") 74 | # This should give you how long the string is. 75 | 76 | # Get number of items in treasure chest with len)) 77 | num_items_in_chest = len(treasure_chest) 78 | 79 | print(f"Take all {num_items_in_chest} treasure, press '1'") 80 | print("Leave it, press '2'") 81 | 82 | treasure_choice = input("> ") 83 | 84 | if treasure_choice == "1": 85 | # ESCAPE CHARACTERS 86 | # We encountered this when escaping those single or double quotes in the beginning. 87 | # Go to the Python interpreter. 88 | # >>> print("hello") 89 | # >>> print("\thello") 90 | # >>> print("\nhello") 91 | # >>> print("I\nam here,\n\tbut why!\n\nEscaping charaters.") 92 | # 93 | # See https://docs.python.org/2.7/reference/lexical_analysis.html#string-literals 94 | 95 | # Removing an item from the list 96 | treasure_chest.remove("sword") 97 | print("\tYou take the shinier sword from the treasure chest. It does looks exceedingly shiney.") 98 | print("\tWoohoo! Bounty and a shiney new sword. /drops your crappy sword in the empty treasure chest.") 99 | 100 | # STRING MANIPULATION 101 | # Here's a handy way to join items in a list. 102 | # Go to the Python intrepeter. 103 | # >>> treasure_chest = ["diamonds", "gold", "silver", "sword"] 104 | # >>> ', '.join(treasure_chest) 105 | # What happens here is we created a string ', ' (comma with a space), and use the 106 | # string's in-built function called join() to join up your list items and 107 | # creates a comma separated string. Really handy, better than writing your own. :-) 108 | 109 | temp_treasure_list = treasure_chest[:] # Creates a new temp list - will see how it's used in line #98 110 | treasure_contents = ", ".join(treasure_chest) 111 | print(f"\tYou also receive {treasure_contents}.") 112 | 113 | # Removing all the rest of the items in the treasure chest list 114 | # To do this, take each treasure from the chest 115 | for treasure in temp_treasure_list: 116 | treasure_chest.remove(treasure) 117 | 118 | 119 | # Add the old sword in place of the new sword 120 | # To do this, you use the list append() function. 121 | treasure_chest.append("crappy sword") 122 | 123 | elif treasure_choice == "2": 124 | print("It will still be here (I hope), right after I get past this guard") 125 | 126 | elif choice == "2": 127 | print("The guard is more interesting, let's go that way!") 128 | elif action.lower() in ["guard", "right"]: 129 | print("The guard is more interesting, let's go that way!") 130 | else: 131 | print("Well, not sure what you picked there, let's poke the guard cos it's fun!") 132 | 133 | 134 | def red_door_room(): 135 | ''' 136 | The red door rooom contains a red dragon. 137 | 138 | If a player types "flee" as an answer, player returns to the room with two doors, 139 | otherwise the player dies. 140 | ''' 141 | print("There you see a great red dragon.") 142 | print("It stares at you through one narrowed eye.") 143 | print("Do you flee for your life or stay?") 144 | 145 | next_move = input("> ") 146 | 147 | # Flee to return to the start of the game, in the room with the blue and red door or die! 148 | if "flee" in next_move: 149 | start_adventure() 150 | else: 151 | # You call the function you_died and pass the reason why you died as 152 | # a string as an argument. 153 | you_died("It eats you. Well, that was tasty!") 154 | 155 | ### END ROOMS ### 156 | 157 | def start_adventure(): 158 | ''' 159 | This function starts the adventure by allowing two options for 160 | players to choose from: red or blue door 161 | 162 | Chosen option will print out the door chosen. 163 | ''' 164 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 165 | door_picked = input("Do you pick the red door or blue door? > ") 166 | 167 | # Pick a door and we go to a room and something else happens 168 | if door_picked == "red": 169 | red_door_room() 170 | elif door_picked == "blue": 171 | blue_door_room() 172 | else: 173 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 174 | 175 | def main(): 176 | ''' 177 | Gets the players name, print it out and starts the adventure. 178 | ''' 179 | player_name = input("What's your name? >") 180 | print(f"Your name is {player_name.upper()}") 181 | 182 | start_adventure() 183 | 184 | if __name__ == '__main__': 185 | main() -------------------------------------------------------------------------------- /game_09.py: -------------------------------------------------------------------------------- 1 | # Now we have a premise. We are in a room and we have two door to choose from. 2 | # We are still in the blue room. Now we have to deal with the guard 3 | # after taking or leaving the treasure chest. 4 | # 5 | # Introducing dictionaries and while loop 6 | 7 | ##### ACTIONS ##### 8 | def you_died(why): 9 | ''' 10 | In: Passing in the string showing player how they dies 11 | 12 | Result: 13 | Prints reason why they player died. 14 | Programme exits without error. 15 | ''' 16 | print(f"{why}. Good job!") 17 | # This exits the program entirely. 18 | exit(0) 19 | 20 | ### END ACTIONS ### 21 | 22 | ### CHARACTERS ### 23 | def guard(): 24 | ''' 25 | Encountering the guard, the player chooses to attack, check or sneak. 26 | - attack: player dies and it is game over 27 | - check: sees what the guard is doing, but nothing else happens, and get 3 options again 28 | - sneak: player sneaks past the guard and wins the game 29 | ''' 30 | # Actions on the guard 31 | actions_dict = {"check":"You see the guard is still sleeping, you need to get to that door on the right of him. What are you waiting for?", 32 | "sneak":"You approach the guard, he's still sleeping. Reaching for the door, you open it slowly and slip out.", 33 | "attack":"You swiftly run towards the sleeping guard and knock him out with the hilt of your shiney sword. Unfortunately it wasn't hard enough."} 34 | 35 | # While loop 36 | while True: 37 | action = input("What do you do? [attack | check | sneak] >").lower() 38 | if action in actions_dict.keys(): 39 | print(actions_dict[action]) 40 | if action == "sneak": 41 | print("You just slipped through the door before the guard realised it.") 42 | print("You are now outside, home free! Huzzah!\n") 43 | return 44 | elif action == "attack": 45 | you_died("Guard woke with a grunt, and reached for his dagger and before you know it, the world goes dark and you just died. \n") 46 | 47 | ### END CHARACTERS ### 48 | 49 | ##### ROOMS ##### 50 | def blue_door_room(): 51 | ''' 52 | The player finds a treasure chest, options to investigate the treasure chest or guard. 53 | 54 | If player chooses 55 | - Treasure chest: show its contents; option to take treasure or ignore it (proceeds to guard) 56 | - Guard: After checking treasure chest, ignoring treasure chest to check guard, it calls guard() function 57 | ''' 58 | # So our treasure_chest list contains 4 items. 59 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 60 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 61 | 62 | # Ask player what to do. 63 | action = input("What do you do? > ") 64 | 65 | # This is a way to see if the text typed by player is in the list 66 | if action.lower() in ["treasure", "chest", "left"]: 67 | print("Oooh, treasure!") 68 | 69 | print("Open it? Press '1'") 70 | print("Leave it alone. Press '2'") 71 | choice = input("> ") 72 | 73 | if choice == "1": 74 | print("Let's see what's in here... /grins") 75 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 76 | print("You find some") 77 | 78 | # for each treasure (variable created on the fly in the for loop) 79 | # in the treasure_chest list, print the treasure. 80 | for treasure in treasure_chest: 81 | print(treasure) 82 | 83 | # So much treasure, what to do? Take it or leave it. 84 | print("What do you want to do?") 85 | # Get number of items in treasure chest with len)) 86 | num_items_in_chest = len(treasure_chest) 87 | 88 | print(f"Take all {num_items_in_chest} treasure, press '1'") 89 | print("Leave it, press '2'") 90 | 91 | treasure_choice = input("> ") 92 | if treasure_choice == "1": 93 | treasure_chest.remove("sword") 94 | print("\tYou take the shinier sword from the treasure chest. It does looks exceedingly shiney.") 95 | print("\tWoohoo! Bounty and a shiney new sword. /drops your crappy sword in the empty treasure chest.") 96 | 97 | temp_treasure_list = treasure_chest[:] 98 | treasure_contents = ", ".join(treasure_chest) 99 | print(f"\tYou also receive {treasure_contents}.") 100 | 101 | # Removing all the rest of the items in the treasure chest 102 | for treasure in temp_treasure_list: 103 | # Use list remove() function to remove each item in the chest. 104 | treasure_chest.remove(treasure) 105 | 106 | # Add the old sword in place of the new sword 107 | treasure_chest.append("crappy sword") 108 | print(f"\tYou close the lid of the chest containing {treasure_chest} for the next adventurer. /grins") 109 | print("Now onward to get past this sleeping guard and the door to freedom.") 110 | elif treasure_choice == "2": 111 | print("It will still be here (I hope), right after I get past this guard") 112 | elif choice == "2": 113 | print("Who needs treasure, let's get out of here.'") 114 | elif action.lower() in ["guard", "right"]: 115 | print("Let's have fun with the guard.") 116 | else: 117 | print("Well, not sure what you picked there, let's poke the guard cos it's fun!") 118 | guard() 119 | 120 | def red_door_room(): 121 | ''' 122 | The red door rooom contains a red dragon. 123 | 124 | If a player types "flee" as an answer, player returns to the room with two doors, 125 | otherwise the player dies. 126 | ''' 127 | print("There you see a great red dragon.") 128 | print("It stares at you through one narrowed eye.") 129 | print("Do you flee for your life or stay?") 130 | 131 | next_move = input("> ") 132 | 133 | # Flee to return to the start of the game, in the room with the blue and red door or die! 134 | if "flee" in next_move: 135 | start_adventure() 136 | else: 137 | # You call the function you_died and pass the reason why you died as 138 | # a string as an argument. 139 | you_died("It eats you. Well, that was tasty!") 140 | ### END ROOMS ### 141 | 142 | def start_adventure(): 143 | ''' 144 | This function starts the adventure by allowing two options for 145 | players to choose from: red or blue door 146 | 147 | Chosen option will print out the door chosen. 148 | ''' 149 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 150 | door_picked = input("Do you pick the red door or blue door? > ") 151 | 152 | # Pick a door and we go to a room and something else happens 153 | if door_picked == "red": 154 | red_door_room() 155 | elif door_picked == "blue": 156 | blue_door_room() 157 | else: 158 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 159 | 160 | def main(): 161 | ''' 162 | Gets the players name, print it out and starts the adventure. 163 | ''' 164 | player_name = input("What's your name? >") 165 | print(f"Your name is {player_name.upper()}") 166 | 167 | start_adventure() 168 | 169 | if __name__ == '__main__': 170 | main() -------------------------------------------------------------------------------- /game_10.py: -------------------------------------------------------------------------------- 1 | ## 2 | ## Added coded in main(),and create a function to ask for your name and return it. 3 | ## 4 | 5 | ##### ACTIONS ##### 6 | def you_died(why): 7 | ''' 8 | In: Passing in the string showing player how they dies 9 | 10 | Result: 11 | Prints reason why they player died. 12 | Programme exits without error. 13 | ''' 14 | print("{}. Good job!".format(why)) 15 | 16 | # This exits the program entirely. 17 | exit(0) 18 | 19 | ### END ACTIONS ### 20 | 21 | ### CHARACTERS ### 22 | def guard(): 23 | ''' 24 | Encountering the guard, the player chooses to attack, check or sneak. 25 | - attack: player dies and it is game over 26 | - check: sees what the guard is doing, but nothing else happens, and get 3 options again 27 | - sneak: player sneaks past the guard and wins the game 28 | ''' 29 | # Actions on the guard 30 | actions_dict = {"check":"You see the guard is still sleeping, you need to get to that door on the right of him. What are you waiting for?", 31 | "sneak":"You approach the guard, he's still sleeping. Reaching for the door, you open it slowly and slip out.", 32 | "attack":"You swiftly run towards the sleeping guard and knock him out with the hilt of your shiney sword. Unfortunately it wasn't hard enough."} 33 | 34 | # While loop 35 | while True: 36 | action = input("What do you do? [attack | check | sneak] >").lower() 37 | if action in actions_dict.keys(): 38 | print(actions_dict[action]) 39 | if action == "sneak": 40 | print("You just slipped through the door before the guard realised it.") 41 | print("You are now outside, home free! Huzzah!\n") 42 | return 43 | elif action == "attack": 44 | you_died("Guard woke with a grunt, and reached for his dagger and before you know it, the world goes dark and you just died. \n") 45 | 46 | ### END CHARACTERS ### 47 | 48 | ##### ROOMS ##### 49 | def blue_door_room(): 50 | ''' 51 | The player finds a treasure chest, options to investigate the treasure chest or guard. 52 | 53 | If player chooses 54 | - Treasure chest: show its contents; option to take treasure or ignore it (proceeds to guard) 55 | - Guard: After checking treasure chest, ignoring treasure chest to check guard, it calls guard() function 56 | ''' 57 | # So our treasure_chest list contains 4 items. 58 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 59 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 60 | 61 | # Ask player what to do. 62 | action = input("What do you do? > ") 63 | 64 | # This is a way to see if the text typed by player is in the list 65 | if action.lower() in ["treasure", "chest", "left"]: 66 | print("Oooh, treasure!") 67 | 68 | print("Open it? Press '1'") 69 | print("Leave it alone. Press '2'") 70 | choice = input("> ") 71 | 72 | if choice == "1": 73 | print("Let's see what's in here... /grins") 74 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 75 | print("You find some") 76 | 77 | # for each treasure (variable created on the fly in the for loop) 78 | # in the treasure_chest list, print the treasure. 79 | for treasure in treasure_chest: 80 | print(treasure) 81 | 82 | # So much treasure, what to do? Take it or leave it. 83 | print("What do you want to do?") 84 | # Get number of items in treasure chest with len)) 85 | num_items_in_chest = len(treasure_chest) 86 | 87 | print(f"Take all {num_items_in_chest} treasure, press '1'") 88 | print("Leave it, press '2'") 89 | 90 | treasure_choice = input("> ") 91 | if treasure_choice == "1": 92 | treasure_chest.remove("sword") 93 | print("\tYou take the shinier sword from the treasure chest. It does looks exceedingly shiney.") 94 | print("\tWoohoo! Bounty and a shiney new sword. /drops your crappy sword in the empty treasure chest.") 95 | 96 | temp_treasure_list = treasure_chest[:] 97 | treasure_contents = ", ".join(treasure_chest) 98 | print(f"\tYou also receive {treasure_contents}.") 99 | 100 | # Removing all the rest of the items in the treasure chest 101 | for treasure in temp_treasure_list: 102 | # Use list remove() function to remove each item in the chest. 103 | treasure_chest.remove(treasure) 104 | 105 | # Add the old sword in place of the new sword 106 | treasure_chest.append("crappy sword") 107 | print(f"\tYou close the lid of the chest containing {treasure_chest} for the next adventurer. /grins") 108 | print("Now onward to get past this sleeping guard and the door to freedom.") 109 | elif treasure_choice == "2": 110 | print("It will still be here (I hope), right after I get past this guard") 111 | elif choice == "2": 112 | print("Who needs treasure, let's get out of here.'") 113 | elif action.lower() in ["guard", "right"]: 114 | print("Let's have fun with the guard.") 115 | else: 116 | print("Well, not sure what you picked there, let's poke the guard cos it's fun!") 117 | guard() 118 | 119 | def red_door_room(): 120 | ''' 121 | The red door rooom contains a red dragon. 122 | 123 | If a player types "flee" as an answer, player returns to the room with two doors, 124 | otherwise the player dies. 125 | ''' 126 | print("There you see a great red dragon.") 127 | print("It stares at you through one narrowed eye.") 128 | print("Do you flee for your life or stay?") 129 | 130 | next_move = input("> ") 131 | 132 | # Flee to return to the start of the game, in the room with the blue and red door or die! 133 | if "flee" in next_move: 134 | start_adventure() 135 | else: 136 | # You call the function you_died and pass the reason why you died as 137 | # a string as an argument. 138 | you_died("It eats you. Well, that was tasty!") 139 | ### END ROOMS ### 140 | 141 | def get_player_name(): 142 | ''' 143 | Gets players name, may or may not be renamed depending on player's choice. 144 | Returns: Player name back (altered or unaltered) 145 | ''' 146 | # LOCAL VARIABLES 147 | # The player enters their name and gets assigned to a variable called "name" 148 | name = input("What's your name? > ") 149 | 150 | # This is just an alternative name that the game wants to call the player 151 | alt_name = "Rainbow Unicorn" 152 | answer = input(f"Your name is {alt_name.upper()}, is that correct? [Y|N] > ") 153 | 154 | if answer.lower() in ["y", "yes"]: 155 | name = alt_name 156 | print(f"You are fun, {name.upper()}! Let's begin our adventure!") 157 | 158 | elif answer.lower() in ["n", "no"]: 159 | print(f"Ok, picky. {name.upper()} it is. Let's get started on our adventure.") 160 | else: 161 | print("Trying to be funny? Well, you will now be called {alt_name.upper()} anyway.") 162 | name = alt_name 163 | 164 | # Now notice that we are returning the variable called name. 165 | # In main(), it doesn't know what the variable "name" is, as it only exists in 166 | # get_player_name() function. 167 | # This is why indentation is important, variables declared in this block only exists in that block 168 | return name 169 | 170 | def start_adventure(): 171 | ''' 172 | This function starts the adventure by allowing two options for 173 | players to choose from: red or blue door 174 | 175 | Chosen option will print out the door chosen. 176 | ''' 177 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 178 | door_picked = input("Do you pick the red door or blue door? > ") 179 | 180 | # Pick a door and we go to a room and something else happens 181 | if door_picked == "red": 182 | red_door_room() 183 | elif door_picked == "blue": 184 | blue_door_room() 185 | else: 186 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 187 | 188 | def main(): 189 | ''' 190 | Gets the players name by calling get_player_name() before starting the adventure. 191 | ''' 192 | player_name = get_player_name() 193 | 194 | #################################################################### 195 | # ACTIVITIES 196 | # 197 | # Read some of the best practices when writing Python code 198 | # http://legacy.python.org/dev/peps/pep-0008/ 199 | # Main thing is if you are using tabs, make sure it's 4-spaces, 200 | # most editors will convert it (check preferences/settings). 201 | # 202 | # Modify the code 203 | # - add taunting the guard or talking 204 | # - sword fight with the guard, and keep track of health points (HP) 205 | # - puzzles like 1+2 during an encounter 206 | # - modifiy blue_door_room()'s if statement 207 | # so it takes into account player typing "right" or "guard" 208 | # Hint: Add another elif before the else statement 209 | # 210 | # So many if statements, this can be made simpler and easier to 211 | # maintain by using Finite State Machine (FSM) 212 | # You can find info about it, but it will mainly be touching 213 | # object-orient programming, which is another lesson for another day. 214 | # 215 | ##################################################################### 216 | 217 | start_adventure() 218 | 219 | print("\nThe end\n") 220 | print(f"Thanks for playing, {player_name.upper()}") 221 | 222 | 223 | if __name__ == '__main__': 224 | main() 225 | -------------------------------------------------------------------------------- /game_10_with_design.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Let's clean it up (or refactor), and create a function to ask for your name and return it. 3 | # This will keep main() as clean as possible. 4 | # New code starts at line 147 5 | # 6 | # Run this code a few times and see what happens with different choices. 7 | # It's good to test all options and see if that's what you expected. 8 | 9 | ##### ACTIONS ##### 10 | def you_died(why): 11 | print_game_over() 12 | # You expect a reason why the player died. It's a string. 13 | print("{}. Good job!".format(why)) 14 | 15 | # This exits the program entirely. 16 | exit(0) 17 | 18 | ### END ACTIONS ### 19 | 20 | ### CHARACTERS ### 21 | def guard(): 22 | print_guard() 23 | # The guard 24 | print("You approach the guard, he's still sleeping.") 25 | print("Suddenly you knocked a wooden cask with a mug on it... CRASSH!") 26 | print("\nOi, what you doing 'ere?") 27 | 28 | # Guard is not moving initially 29 | guard_moved = False 30 | 31 | # - When a player dies, it calls you_died() and it exits() the program. 32 | # - When a player escapes through the door, you return to the previous function which 33 | # called this function. 34 | while True: 35 | next_action = input("[run | door] > ").lower() 36 | if next_action == "run" and guard_moved: 37 | you_died("Guard was faster than he looks and your world goes dark...") 38 | elif next_action == "run" and not guard_moved: 39 | print("Guard jumps up and looks the other way, missing you entirely.") 40 | guard_moved = True 41 | elif next_action == "door" and guard_moved: 42 | print("You just slipped through the door before the guard realised it.") 43 | print("You are now outside, home free! Huzzah!") 44 | return 45 | elif next_action == "door" and not guard_moved: 46 | you_died("Guard was faster than he looks and your world goes dark...") 47 | else: 48 | print("Not sure what you meant there... try again.") 49 | # END CHARACTERS # 50 | 51 | ##### ROOMS ##### 52 | def blissful_ignorance_of_illusion_room(): 53 | print_chest() 54 | # The variable treasure_chest is an object type called a list 55 | # A list maybe empty as well. 56 | # So our treasure_chest list contains 4 items. 57 | treasure_chest = ["diamonds", "gold", "silver", "sword"] 58 | print("You see a room with a wooden treasure chest on the left, and a sleeping guard on the right in front of the door") 59 | 60 | # Ask player what to do. 61 | action = input("What do you do? > ") 62 | 63 | # This is a way to see if the text typed by player is in the list 64 | if action.lower() in ["treasure", "chest", "left"]: 65 | print("Oooh, treasure!") 66 | 67 | print("Open it? Press '1'") 68 | print("Leave it alone. Press '2'") 69 | choice = input("> ") 70 | 71 | # Try just leaving 1 and 2 as a number 72 | # Change to string and see what happens 73 | if choice == "1": 74 | print("Let's see what's in here... /grins") 75 | print("The chest creaks open, and the guard is still sleeping. That's one heavy sleeper!") 76 | print("You find some") 77 | 78 | # for each treasure (variable created on the fly in the for loop) 79 | # in the treasure_chest list, print the treasure. 80 | for treasure in treasure_chest: 81 | print(treasure) 82 | 83 | # So much treasure, what to do? Take it or leave it. 84 | print("What do you want to do?") 85 | print("Take all {} treasure, press '1'".format(len(treasure_chest))) 86 | print("Leave it, press '2'") 87 | 88 | treasure_choice = input("> ") 89 | if treasure_choice == "1": 90 | print("\tWoohoo! Bounty and a shiney new sword. /drops your crappy sword in the empty treasure chest.") 91 | print("\tYou just received [{}]".format(", ".join(treasure_chest))) 92 | elif treasure_choice == "2": 93 | print("It will still be here (I hope), right after I get past this guard") 94 | 95 | # Picked up treasure or left it, you will now encounter the guard. 96 | # Let's call the guard() function here. 97 | guard() 98 | else: 99 | # Let's call the guard() function here as well, no point writing a bunch of same code 100 | # twice (or more). It's good to be able to re-use code. 101 | print("The guard is more interesting, let's go that way!") 102 | guard() 103 | 104 | 105 | def painful_truth_of_reality_room(): 106 | print_monster() 107 | print("There you see the great evil Cthulhu.") 108 | print("He, it, whatever stares at you and you go insane.") 109 | print("Do you flee for your life or eat your head?") 110 | 111 | next_move = input("> ") 112 | 113 | # Flee to return to the start of the game, in the room with the blue and red door or die! 114 | if "flee" in next_move: 115 | start_adventure() 116 | else: 117 | # You call the function you_died and pass the reason why you died as 118 | # a string as an argument. 119 | you_died("You died. Well, that was tasty!") 120 | ### END ROOMS ### 121 | 122 | def get_player_name(): 123 | # LOCAL VARIABLES 124 | # The player enters their name and gets assigned to a variable called "name" 125 | name = input("What's your name? > ") 126 | 127 | # This is just an alternative name that the game wants to call the player 128 | alt_name = "Rainbow Unicorn" 129 | answer = input("Your name is {}, is that correct? [Y|N] > ".format(alt_name.upper())) 130 | if answer.lower() in ["y", "yes"]: 131 | name = alt_name 132 | print("You are fun, {}! Let's begin our adventure!".format(name.upper())) 133 | elif answer.lower() in ["n", "no"]: 134 | print("Ok, picky. {} it is. Let's get started on our adventure.".format(name.upper())) 135 | else: 136 | print("Trying to be funny? Well, you will now be called {} anyway.".format(alt_name.upper())) 137 | name = alt_name 138 | 139 | # Now notice that we are returning the variable called name. 140 | # In main(), it doesn't know what the variable "name" is, as it only exists in 141 | # get_player_name() function. 142 | # This is why indentation is important, variables declared in this block only exists in that block 143 | return name 144 | 145 | def start_adventure(): 146 | print_dungeon() 147 | print("You enter a room, and you see a red door to your left and a blue door to your right.") 148 | door_picked = input("Do you pick the red door or blue door? > ") 149 | 150 | # Pick a door and we go to a room and something else happens 151 | if door_picked == "red": 152 | painful_truth_of_reality_room() 153 | elif door_picked == "blue": 154 | blissful_ignorance_of_illusion_room() 155 | else: 156 | print("Sorry, it's either 'red' or 'blue' as the answer. You're the weakest link, goodbye!") 157 | 158 | def main(): 159 | os.system("clear") 160 | # Calls get_player_name and returns the player name 161 | player_name = get_player_name() 162 | 163 | #################################################################### 164 | # ACTIVITIES 165 | # 166 | # Read some of the best practices when writing Python code 167 | # http://legacy.python.org/dev/peps/pep-0008/ 168 | # Main thing is if you are using tabs, make sure it's 4-spaces, 169 | # most editors will convert it (check preferences/settings). 170 | # 171 | # Modify the code 172 | # - add taunting the guard or talking 173 | # - sword fight with the guard, and keep track of health points (HP) 174 | # - puzzles like 1+2 during an encounter 175 | # - modifiy blissful_ignorance_of_illusion_room()'s if statement 176 | # so it takes into account player typing "right" or "guard" 177 | # Hint: Add another elif before the else statement 178 | # 179 | # So many if statements, this can be made simpler and easier to 180 | # maintain by using Finite State Machine (FSM) 181 | # You can find info about it, but it will mainly be touching 182 | # object-orient programming, which is another lesson for another day. 183 | # 184 | ##################################################################### 185 | 186 | start_adventure() 187 | 188 | print("\nThe end\n") 189 | print("Thanks for playing, {}".format(player_name.upper())) 190 | 191 | 192 | 193 | def print_dungeon(): 194 | print() 195 | print(" _________________________________________________________") 196 | print(" /| -_- _- |\ ") 197 | print("/ |_-_- _ -_- _- -| \ ") 198 | print(" | _- _-- | ") 199 | print(" | , |") 200 | print(" | .-'````````'. '(` .-'```````'-. |") 201 | print(" | .` | `. `)' .` | `. | ") 202 | print(" | / | () \ U / | () \ |") 203 | print(" | | | ; | o T o | | ; | |") 204 | print(" | | | ; | . | . | | ; | |") 205 | print(" | | | ; | . | . | | ; | |") 206 | print(" | | | ; | .|. | | ; | |") 207 | print(" | | |____;_________| | | |____;_________| | ") 208 | print(" | | / __ ; - | ! | / `'() _ - | |") 209 | print(" | | / __ () -| - | / __-- - | |") 210 | print(" | | / __-- _ | _- _ - | / __--_ | |") 211 | print(" |__|/__________________|___________|/__________________|__|") 212 | print(" / _ - lc \ ") 213 | print("/ -_- _ - _- _--- -_- -_ \ ") 214 | print() 215 | 216 | def print_monster(): 217 | print() 218 | print(" | | ") 219 | print(" \ / \ / ") 220 | print(" -= .'> =- -= <'. =- ") 221 | print(" '.'. .'.' ") 222 | print(" '.'. .'.' ") 223 | print(" '.'.----^----.'.' ") 224 | print(" /'==========='\ ") 225 | print(" . / .-. .-. \ . ") 226 | print(" :'.\ '.O.') ('.O.' /.': ") 227 | print(" '. | | .' ") 228 | print(" '| / \ |' ") 229 | print(" \ (o'o) / ") 230 | print(" |\ /| ") 231 | print(" \('._________.')/ ") 232 | print(" '. \/|_|_|\/ .' ") 233 | print(" /'._______.'\ lc ") 234 | print() 235 | 236 | def print_chest(): 237 | print() 238 | print(" _.--. ") 239 | print(" _.-'_:-'|| ") 240 | print(" _.-'_.-::::'|| ") 241 | print(" _.-:'_.-::::::' || ") 242 | print(" .'`-.-:::::::' || ") 243 | print(" /.'`;|:::::::' ||_ ") 244 | print(" || ||::::::' _.;._'-._ ") 245 | print(" || ||:::::' _.-!oo @.!-._'-. ") 246 | print(" ('. ||:::::.-!()oo @!()@.-'_.| ") 247 | print(" '.'-;|:.-'.&$@.& ()$%-'o.'-U|| ") 248 | print(" `>'-.!@%()@'@_%-'_.-o _.|'|| ") 249 | print(" ||-._'-.@.-'_.-' _.-o |'|| ") 250 | print(" ||=[ '-._.-+U/.-' o |'|| ") 251 | print(" || '-.]=|| |'| o |'|| ") 252 | print(" || || |'| _| '; ") 253 | print(" || || |'| _.-'_.-' ") 254 | print(" |'-._ || |'|_.-'_.-' ") 255 | print(" '-._'-.|| |' `_.-' ") 256 | print(" '-.||_/.-' ") 257 | print() 258 | 259 | def print_guard(): 260 | print() 261 | print(" ___I___ ") 262 | print(" /= | #\ ") 263 | print(" /.__-| __ \ ") 264 | print(" |/ _\_/_ \| ") 265 | print(" (( __ \__)) ") 266 | print(" __ ((()))))()) __ ") 267 | print(" ,' |()))))(((()|# `. ") 268 | print(" / |^))()))))(^| =\ ") 269 | print(" / /^v^(())()()v^;' .\ ") 270 | print(" |__.'^v^v^))))))^v^v`.__| ") 271 | print(" /_ ' \______(()_____( | ") 272 | print(" _..-' _//_____[xxx]_____\.-| ") 273 | print(" /,_#\.=-' /v^v^v^v^v^v^v^v^| _| ") 274 | print(" \)|) v^v^v^v^v^v^v^v^v| _| ") 275 | print(" || :v^v^v^v^v^v`.-' |# \, ") 276 | print(" || v^v^v^v`_/\__,--.|\_=_/ ") 277 | print(" >< :v^v____| \_____|_ ") 278 | print(" , || v^ / \ / ") 279 | print(" //\_||_)\ `/_..-._\ )_...__\ ") 280 | print(" || \/ #| |_='_( | =_(_ ") 281 | print(" || _/\_ | / =\ / ' =\ ") 282 | print(" \\\/ \/ )/ gnv |=____#| '=....#| ") 283 | print() 284 | 285 | def print_game_over(): 286 | print() 287 | print(" _____ __ __ ______ ______ ________ _____ ") 288 | print(" / ____| /\ | \/ | ____| / __ \ \ / / ____| __ \ ") 289 | print(" | | __ / \ | \ / | |__ | | | \ \ / /| |__ | |__) |") 290 | print(" | | |_ | / /\ \ | |\/| | __| | | | |\ \/ / | __| | _ / ") 291 | print(" | |__| |/ ____ \| | | | |____ | |__| | \ / | |____| | \ \ ") 292 | print(" \_____/_/ \_\_| |_|______| \____/ \/ |______|_| \_\\") 293 | print() 294 | 295 | if __name__ == '__main__': 296 | main() -------------------------------------------------------------------------------- /mentor-notes.md: -------------------------------------------------------------------------------- 1 | # MENTOR NOTES 2 | 3 | * Install Python. 4 | * Install Sublime Text Editor 5 | * (Windows) Set PATH in environmental variables so python can run easily in cmd. 6 | * See if "python" runs by typing `python` in the terminal/cmd 7 | * Ask people to type `print("hello")` 8 | * Ran their first piece of Python code. 9 | * Type `exit()` to exit Python interpretor. 10 | * Switch to Sublime Text Editor. 11 | * File > New File 12 | * File > Save As... 13 | * myfirstscript.py 14 | * Type on first line `print("hello, this is my first script")` 15 | * File > Save 16 | * Switch to CMD/Terminal. 17 | * Type `python myfirstscript.py` 18 | * You just ran your first Python script via command line. 19 | 20 | Now you can begin with game_01.py. :-) 21 | 22 | 23 | 24 | --------------------------------------------------------------------------------