zork.py

Created by cent20

Created on February 13, 2023

6.69 KB


# Introduction narration of game
# https://github.com/iamjawa/zork-py/blob/master/zork.py
# Do What The F*ck You Want To Public License

from time import sleep

def center_texte(texte, sep=" "):
    '''Cette fonction affiche un texte dans la console et le centre'''
    f = max(0, (30-len(texte))//2-2) 
    print(sep*f + " " + texte + " "+ sep*f)

def split_text(text):
    '''Cette fonction coupe un texte en phrase de longueur <30'''
    words = text.split()
    split_text = []
    current_string = ""
    for word in words:
        if len(current_string + " " + word) <= 30:
            current_string += " " + word
            if word[-1] in ("!", ":", ",", "?", ".") and len(current_string) > 15:
                #print("******")
                split_text.append(current_string.strip())
                current_string = ""
        else:
            split_text.append(current_string.strip())
            current_string = word
    split_text.append(current_string.strip())
    return split_text

def scroll_text(text):
    '''Cette fonction affiche des lignes de texte avec des pauses'''
    for ligne in split_text(text):
        if ligne!= "":
            print(ligne)
            #sleep(1.42)

def help():
    print("*"*28)
    center_texte(" help", "*")
    print("*"*28)
    scroll_text("To play, you must describe the player's actions. An action is generally composed of a verb followed by a direction, a place, an object.")


# Ne pas lire le code sous cette ligne sinon le jeu n'aura plus aucun intérêt.
# todo : Chiffrer le texte. Ecrire une fontion de chiffrage / déchiffrage.

def game():
    
    loop = 4
    print("*"*28)
    center_texte(" Welcome to Zork I", "*")
    center_texte(" nsi.xyz/zork ", "*")
    center_texte("text-based adventure game", "*")    
    
    while True:
      # First Input Loop
      
      while loop == 4:
        print("-"*28)
        if loop == 4:      
          scroll_text("You are standing in an open field west of a white house, with a boarded front door.")
          scroll_text("(A secret path leads southwest into the forest.)")
          scroll_text("There is a Small Mailbox.")
          inp = input("What do you do?\n> ").lower()
        if inp == ("take mailbox"):
          scroll_text("It is securely anchored.")
        elif inp == ("open mailbox"):
          scroll_text("Opening the small mailbox reveals a leaflet.")
        elif inp == ("go east"):
          scroll_text("The door is boarded and you cannot remove the boards.")
        elif inp == ("open door"):
          scroll_text("The door cannot be opened.")
        elif inp == ("take boards"):
          scroll_text("The boards are securely fastened.")
        elif inp == ("look at house"):
          scroll_text("The house is a beautiful colonial house which is painted white. It is clear that the owners must have been extremely wealthy.")
        elif inp == ("go southwest"):
          loop = 8
        elif inp == ("read leaflet"):
          scroll_text("Welcome to the Unofficial Python Version of Zork. Your mission is to find a Jade Statue.")
        elif inp == ("help"):
            help()
        else:
          scroll_text("# invalid action")
     

      # Southwest Loop
      while loop == 8:
        print("-"*28)
        if loop == 8:
          scroll_text("This is a forest, with trees in all directions. To the east, there appears to be sunlight.")
          forest_inp = input("What do you do?\n> ")

        if forest_inp == ("go west"):
          scroll_text("You would need a machete to go further west.")
        elif forest_inp == ("go north"):
          scroll_text("The forest becomes impenetrable to the North.")
        elif forest_inp == ("go south"):
          scroll_text("Storm-tossed trees block your way.")
        elif forest_inp == ("go east"):
          loop = 9
        elif inp == ("help"):
            help()
        elif inp == ("go back"):
            loop = 4
        else:
          scroll_text("# invalid action")
      

      # East Loop and Grating Input
      while loop == 9:
        print("-"*28)
        if loop == 9:
          scroll_text("You are in a clearing, with a forest surrounding you on all sides. A path leads south.")
          scroll_text("There is an open grating, descending into darkness.")
          grating_inp = input("What do you do?\n> ")

        if grating_inp == ("go south"):
          scroll_text("You see a large ogre and turn around.")
        elif grating_inp == ("descend grating"):
          loop = 10
        elif inp == ("help"):
            help()
        elif inp == ("go back"):
            loop = 8
        else:
          scroll_text("# invalid action") 


      # Grating Loop and Cave Input
      while loop == 10:
        print("-"*28)
        if loop == 10:
          scroll_text("You are in a tiny cave with a dark, forbidding staircase leading down.")
          scroll_text("There is a skeleton of a human male in one corner.")
          cave_inp = input("What do you do?\n> ")

        if cave_inp == ("descend staircase"):
          loop = 11
        elif cave_inp == ("take skeleton"):
          scroll_text("Why would you do that? Are you some sort of sicko?")
        elif cave_inp == ("smash skeleton"):
          scroll_text("Sick person. Have some respect mate.")
        elif cave_inp == ("light up room"):
          scroll_text("You would need a torch or lamp to do that.")
        elif cave_inp == ("break skeleton"):
          scroll_text("I have two questions: Why and With What?")
        elif cave_inp == ("go down staircase"):
          loop = 11
        elif cave_inp == ("scale staircase"):
          loop = 11
        elif cave_inp == ("suicide"):
          scroll_text("You throw yourself down the staircase as an attempt at suicide. You die.")
          print("-"*28)
          suicide_inp = input("Do you want to continue? Y/N\n> ")
          if suicide_inp == ("n"):
            exit()
          if suicide_inp == ("y"):
            loop = 4
        elif cave_inp == ("scale staircase"):
          loop = 11
        elif inp == ("help"):
            help()
        elif inp == ("go back"):
            loop = 9
        else:
          scroll_text("# invalid action")


      # End of game
      while loop == 11:
        print("-"*28)
        if loop == 11:
          scroll_text("You have entered a mud-floored room.")
          scroll_text("Lying half buried in the mud is an old trunk, bulging with jewels.")
          last_inp = input("What do you do?\n> ")

        if last_inp == ("open trunk"):
          scroll_text("You have found the Jade Statue and have completed your quest!")
          exit()
        elif inp == ("help"):
            help()
        elif inp == ("go back"):
            loop = 10
        else:
          scroll_text("# invalid action")

          
game()