This is the source code for my cumulative project. In here, it shows all the ‘techy stuff’ behind the game that makes it work so that it can be played. While I was programming this, I had quite a bit of stress and frustrations, so I decided to include somewhat funny comments that are still useful and serve their purpose. I hope they aren’t too distracting, but it was actually pretty helpful to ease a little bit of my stress and frustration, which is the only reason I never went back to change it.
To play my game, there are some things you’ll need to know and/or download in order to play my game, if you so desire.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
""" Program: Final Project Programmer: Eliese Schwinck Created: 18 November 2019 Updated: 18 November 2019 Trouble in Terrorist Town (look at that alliteration) """ from time import sleep import random def main(): print_introduction() terrorist, citizens = start_game() terroristGuess = 0 whichStory = 1 # # Start a loop to make sure that they can actually play the full game. # while terroristGuess != terrorist: victim, story, whichStory = night(terrorist, whichStory, citizens) morning(victim, story) terroristGuess = retrieve_guess(terrorist) terroristGuess = trial(terroristGuess, victim) print_suspects_defence(terroristGuess, terrorist, whichStory) vote = vote_of_guilt() # # Run an execution if voted guilty. # if vote == "guilty": execution(terroristGuess, victim) # # Let the user know if they killed an innocent being. # Hopefully not, but you can never trust a user. # if terroristGuess != terrorist: slow_print("This person was innocent...") congratulations() |
This is the main. This is the part of the code that is calling all the functions so that they will run in the correct order. There is also a while loop (while terroristGuess != terrorist:) which is what is telling the program to continue to run until the user guesses the terrorist correctly, where the program will stop the loop and move into the congratulations, where the program will finish running.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
def start_game(): """Set the logistics to get the game running here.""" # 1=Rose, 2=Joe, 3=Mary, 4=Lucy, 5=Ray, 6=Scottie, 7= Junior, 8=Jen # 9=Kara, 10=Alyssa, 11=Nigel, 12=Rich, 13=Karma, 14=Sean, 15=Brian citizens = {'Rose':1, 'Joe':2, 'Mary':3, 'Lucy':4, 'Ray':5, 'Scottie':6, 'Junior':7, 'Jen':8, 'Kara':9, 'Alyssa':10, 'Nigel':11, 'Rich':12, 'Karma':13, 'Sean':14, 'Brian':15} # # Set a random citizen as the terrorist. # terrorist = random.choice(list(citizens.keys())) return terrorist, citizens def night(terrorist, whichStory, citizens): """Do technical things that run the story here.""" # # Pause to give the user time to read. Just in case. # slow_print("Press enter/return to continue. ",newLine=False) input() # # Start the night. # slow_print("Night falls upon the town...") # # Figure out who the victim of the night is. # victim = death(terrorist, citizens) victim = name_of_number(victim, citizens) # # Figure out how the person died. # story, whichStory = story_of_death(whichStory, victim) print() return victim, story, whichStory def morning(victim, story): """Tell the story of the victim's death.""" slow_print("The sun rises...") # # Tell the user the victim of the night. # slow_print(f"And the body of {victim} is found...") print() # # Tell the user the story of the victim's death. # slow_print(f'{story}') def death(terrorist, citizen): """Randomly figure who dies.""" victim = random.choice(list(citizens.keys())) # # Make sure the killer doesn't...you know...kill themself. # while victim == terrorist: victim = random.choice(list(citizens.keys())) return victim def retrieve_guess(terrorist): """Get the user's guess for the murderer.""" slow_print(" Judge: Sheriff, who do you think the murderer is? ", newLine=False) terroristGuess = input() # Set the name to an int to compare to the terrorist (in int form). terroristGuess = number_of_name(terroristGuess) return terroristGuess def trial(terroristGuess, victim): """Run the trial here.""" # # The repetetive text of the trials goes here. # terroristGuess = name_of_number(terroristGuess, citizens) slow_print(f""" Judge: Today, we are gathered here to discuss the death of {victim} last night. The Sheriff would like to bring up {terroristGuess} for questioning. {terroristGuess} makes their way to the front, standing, ready to give their alibi. You: {terroristGuess}, last night {victim} was found dead. Where were you? """) return terroristGuess def print_suspects_defence(terroristGuess, terrorist, whichStory): """Have the terroristGuess defend themself. Or, you know, have the terrorist give themself away.""" # # Defense of an innocent person. # if terroristGuess != terrorist: defense = print_defense(terroristGuess, whichStory) print(defense) # # Obvious defense fail of the terrorist so it's not, ya'know, impossible. # elif terroristGuess == terrorist: slow_print(f"""{terroristGuess}: I didn't go out last night. In fact, I didn't even do anything last night. Why would you think I did anything?""") def vote_of_guilt(): """Ask the user if they think this citizen is guilty or not.""" slow_print("Do you think this person is innocent or guilty? ", newLine=False) vote = input() vote = vote.lower() # # Make sure the user gives an acceptable vote. Because you can never trust # your user. # while (vote != "guilty") and (vote != "innocent"): slow_print("That is not an acceptable answer.") vote_of_guilt() return vote def execution(terroristGuess, victim): """Act on the decision of the user.""" print() print() # # User wants to kill off the terroristGuess? Alright. Done and done. # slow_print(f"{terroristGuess} was voted to be guilty by the citizens of this town.") slow_print(f"""By orders of the Sheriff and the will of the public, {terroristGuess} is sentenced to hang until death for the murder of {victim}.""") print() def congratulations(): """Congratulate the user on guessing the terrorist.""" sleep(1.7) print() print() # # Congrats, user, on making it through the repetitiveness of this game! # slow_print("Congratulations, Sheriff! You solved the mystery!") |
This part of the program is one of the sections of the game. Here, the start_game() function called in main runs, which sets the dictionary and terrorist. It moves into the night, where the victim is selected and the ‘story’ of that victim’s death is found. In the morning() funciton, it tells the user the victim and their death story. So goes with the rest of the functions. In the print when the curely brackets, print is using it’s function of format, where it will place what the variable equals rather than the variable (ex. it will place Rose instead of {terroristGuess} and Kara instead of {victim}.) The rest of the functions called in main are listed here.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
def print_defense(terroristGuess, whichStory): """Figure out which defense the terroristGuess will use based on the story.""" if whichStory == 1: defense = (f"""I was in the hospital last night with stomach pains.""") if whichStory == 2: defense = (f"""I was taking a walk in the park and was nowhere near the barber shop.""") if whichStory == 3: defense = (f"""I was at home taking a nap.""") if whichStory == 4: defense = (f"""I was at the barber's shop getting a hair cut.""") if whichStory == 5: defense = (f"""I was in my backyard with my brother.""") if whichStory == 6: defense = (f"""I was at the movie theatre last night.""") if whichStory == 7: defense = (f"""I was at the pool.""") return defense def story_of_death(whichStory, victim): """Figure out the story of the victim's death.""" if whichStory == 1: story = (f"""Last night, {victim} was exiting the barber's shop after receiving a brand new haircut. {victim} unsuspectingly stepped out the door, only to slip on oil that had been place on the group, falling down and hitting their head. {victim} bled to death outside the door of the barber shop...""") elif whichStory == 2: story = (f"""{victim} was at the saloon last night, getting their usual Friday night break from a week of work. Taking a satifying sip from their drink, black filled their vision as they fell, closing their eyes and breathing their last. {victim}'s drink was poisoned. {victim} died of asphyxiation on the saloon's floor...""") elif whichStory == 3: story = (f"""{victim} was enjoying a nice night in the pool to release tension from their frustrating day. They leaned their head back slightly, releasing a sigh, frustrations leaving with the air, as their head was forced under the water, a hand over their face as the air ran out from their lungs. {victim} was drowned...""") elif whichStory == 4: story = (f"""During their meeting in the three story building, {victim} stepped outside onto the balcony for a moment to take a breath of fresh air. As they were about to turn around to re-enter the building, two hands were placed on their upper back, and their body was pushed over the railing. {victim} died of falling...""") elif whichStory == 5: story = (f"""In the cool, crisp air, {victim} had gone a little out of the town to hunt for some food. They shot something in the trees and went out to grab the prey. They knelt down before feeling a pain in their chest. {victim} was shot in the forest...""") elif whichStory == 6: story = (f"""Asleep in their bed, {victim} thought they were safe. But off in dreamland, they were vulnerable. Someone snuck into their room during the night and covered {victim}'s mouth. There was no struggle. {victim} died of smothering...""") elif whichStory == 7: story = (f"""{victim} took a walk last night. They closed their eyes to enjoy the fresh night air that filled their lungs. They walked aimlessly, no clear destination in mind. They felt a sting near their collarbone and collapsed on the road. {victim} died of an injection...""") whichStory = 0 # # Make sure next time has a new story. # whichStory += 1 return story, whichStory def name_of_number(person): """Change the citizen from an int to a str.""" if person == 1: person = "Rose" elif person == 2: person = "Joe" elif person == 3: person = "Mary" elif person == 4: person = "Lucy" elif person == 5: person = "Ray" elif person == 6: person = "Scottie" elif person == 7: person = "Junior" elif person == 8: person = "Jen" elif person == 9: person = "Kara" elif person == 10: person = "Alyssa" elif person == 11: person = "Nigel" elif person == 12: person = "Rich" elif person == 13: person = "Karma" elif person == 14: person = "Sean" elif person == 15: person = "Brian" # # Make sure the user doesn't find a way to somehow magically break this. # else: slow_print("Congratulations, you are magical and I don't even know what to do anymore.") return person def number_of_name(terroristGuess, citizens): """Change the citizen from a str to an int.""" # # Lower case the names to make it easier for me (the programmer). # terroristGuess = terroristGuess.lower() if terroristGuess == "rose": terroristGuess = 1 elif terroristGuess == "joe": terroristGuess = 2 elif terroristGuess == "mary": terroristGuess = 3 elif terroristGuess == "lucy": terroristGuess = 4 elif terroristGuess == "ray": terroristGuess = 5 elif terroristGuess == "scottie": terroristGuess = 6 elif terroristGuess == "junior": terroristGuess = 7 elif terroristGuess == "jen": terroristGuess = 8 elif terroristGuess == "kara": terroristGuess = 9 elif terroristGuess == "alyssa": terroristGuess = 10 elif terroristGuess == "nigel": terroristGuess = 11 elif terroristGuess == "rich": terroristGuess = 12 elif terroristGuess == "karma": terroristGuess = 13 elif terroristGuess == "sean": terroristGuess = 14 elif terroristGuess == "brian": terroristGuess = 15 # # Make sure the user gives a citizen's name. # And no Abby, PAM IS NOT AN OPTION. # while terroristGuess not in citizens: slow_print("That is not a proper answer. Please give a citizen's name. ", newLine=False) terroristGuess = number_of_name(input()) return terroristGuess def slow_print(string,pauseTime=0.02,newLine=True): """print characters one at a time.""" for character in string: print(character,end="") if character == ',': sleep(0.5) elif character in '.?!': sleep(0.75) else: sleep(pauseTime-0.015) if newLine == True: print() def print_introduction(): """Print the instructions so the user knows what he/she/they are doing.""" print(" ???: ",end="") slow_print("Hey! You there! Yeah, you.") print(" ???: ",end="") slow_print("Hey, I haven't seen you before. Are you new in town?") print(" ???: ",end="") # # A little character POV introduction to make it interesting. # slow_print("""Well, of course you are! My name is Rose. Rose: Are you the new Sheriff? You: Yes. Rose: That’s great! Hey, if you ever need anything, don’t be afraid to let me know, alright? Well, I’ve gotta go finish up some work. I’ll see you around!""") print() sleep(2.5) print() # # A proper introduction telling the user how to play. # print("""----------------------------------------------------------------------------- Trouble in Terrorist Town -----------------------------------------------------------------------------""") sleep(1) slow_print("""This is a game called Trouble in Terrorist Town. Have you heard of it? It's a murder mystery game. And we're going to play it today. I hope you brought your detective cap, because if you can't find the mafia, not only you will die, but so will the entire town you are trying to save! This is really a simple game. You are new in town. You were hired to be the new Sheriff, and your task is to save all the innocent characters who live in the town. There will come nighttime, where the murderer will kill somebody who is innocent. In the morning, there will be a trial, where you will vote if you think the person is innocent after hearing their defense. If they are voted guilty, there will be an execution. If voted innocent, there will be no execution and you will move on to the next night. Can you save the town before the terrorist murders everyone? *The possible murderers are Rose, Joe, Mary, Lucy, Ray, Scottie, Junior, Jen, Kara, Alyssa, Nigel, Rich, Karma, Sean, or Brian.* """) main() |
This is all the long wordy stuff. Everything that has long functions are put at the end, with the exception of the slow_print() function. At the very end, there is main(). This is telling the computer to run main, and then the functions listed in main. The reason main() is listed at the end is so that it will be the computer sees when it is scanning the python file and it knows that it should run all the information above that it read. This not only includes the long functions, but also all the other functions as well that are in the other sections above.
When it’s all complete, it’s prints out in the chell for the user to see easily. Everywhere that there is an input() function used, the computer will wait for the needed input from the user, and will hold in its place until the user gives that needed input.