mardi 25 février 2020

Text based RPG game: Improving code readability and maintenance in python 2.7.10

I've been learning how to code for a month now using the "Learning Python The Hard Way" tutorial.

So far it's been a lot of fun.

I challenged myself in creating a text based RPG game.

The structure of my game so far is using functions within functions to move the character around.

Since it's an input based game I've been using "while" loops with if, elif, else conditions and boolean values within them.

IMO, there is no need for class items as the player has no HP or any data that needs to be stored.

Any ideas on how I could improve the code for better readability and maintenance?

This part is at the very beginning of the script:

#GAME: "SEARCHING FOR APRIL."

from sys import exit
import time
import random

global name
name = None


command_list = "\nCommands: \"DODGE\", \"ATTACK\", \"GO\", \"OPEN\", \"SEARCH\" and \"LOOK 
AROUND\".\nThese are the only commands that you need to play the game.\nNOTE: The OPEN and 
SEARCH commands need an item after it to specifiy what you are opening or searching. Be 
specific.\nThe GO command is used to move from one //// AREA \\\\\\\\ to another adjacent 
//// AREA \\\\\\\\, NOT to move your character WITHIN the area.\nType \"LIST\" while playing 
to have the list of commands."

invalid = "\nInvalid command. Try something else.\n"
nothing = "\nYou find nothing interesting."

curr_prompt = "\n\nWhat do you do?\n"


#Boolean values for Items Found or Triggered Events.

entrance_key = False      # Main entrance key described as antique key.
prison_key = False        # Prison key described as silver key.

Here is the function I'd like to improve:

#CELLAR: Items -  Entrance Key.

def cellar():
    print "\n..."
    #time.sleep(3)
    print "\n//// CELLAR (FLOOR -1) \\\\\\\\"
    print "\nThe room is a moist and dimly lit stone walled cellar."
    print "\nThe few rays of moonlight coming from the window"
    print "cast light on what seem to be old wine kegs."
    print "\nA smell of rancid vinegar reaches your nostrils."
    print "\nYou can barely see anything."
    print "\nThere's a bucket next to the wooden door."
    print "\nYou distinguish a heavy steel door at the opposite end"
    print "of the room."
    print curr_prompt
    global entrance_key
    choice = raw_input("> ").lower()
    if entrance_key == False:
        while "search" not in choice or "keg" not in choice:
            if ("go" in choice and ("gravel" in choice or "path" in choice)) or ("open" in choice and "wooden door" in choice):
                print "\nYou push open and walk through the wooden door."
                gravel_path()
            elif "go" in choice and "cellar" in choice:
                print "\nYou're already in the cellar."
            elif "look around" in choice:
                cellar()
            elif "list" in choice:
                print command_list
            elif "drink" in choice and ("wine" in choice or "keg" in choice):
                print "\nYou alcoholic, now is not the time!"
            elif "search" == choice:
                print invalid
            elif "search" in choice and "bucket" in choice:
                print "\nInside you find... Skulls from rodents of some kind.", nothing
            elif "search" in choice and ("steel door" in choice or "heavy door" in choice or "metal door" in choice):
                print "\nThe steel door has a small barred opening at head's height. The inside of the room is pitch black."
            elif "search" in choice and "door" in choice and "wooden" not in choice:
                print "\nWhich one?"
            elif "search" in choice:
                print nothing
            elif ("open" in choice and ("steel door" in choice or "heavy door" in choice or "metal door" in choice or "prison" in choice)) or ("go" in choice and "prison" in choice):
                print "\nThe door is locked."
            elif "open" in choice and "door" in choice:
                print "\nWhich one?"
            else:
                print invalid
            print curr_prompt
            choice = raw_input("> ").lower()
        entrance_key = True
        print "\nYou find an antique key under one of the wine kegs. It might come in handy so you take it with you."
        print curr_prompt
        choice = raw_input("> ").lower()
    while "go" not in choice or ("gravel" not in choice and "path" not in choice):
        if "list" in choice:
            print command_list
        elif "look around" in choice:
            cellar()
        elif "go" in choice and "cellar" in choice:
            print "\nYou're already in the cellar."
        elif "open" in choice and "wooden" in choice:
            gravel_path()
        elif "search" == choice:
            print invalid
        elif "search" in choice and "bucket" in choice:
                print "\nInside you find... Skulls from rodents of some kind. Nothing interesting."
        elif "search" in choice and ("steel door" in choice or "heavy door" in choice or "metal door" in choice):
            print "\nThe steel door has a small barred opening at head's height. The inside of the room is pitch black."
        elif "search" in choice and "door" in choice and "wooden" not in choice:
            print "\nWhich one?"
        elif "search" in choice:
            print nothing
        elif (("open" in choice or "unlock" in choice) and ("steel door" in choice or "heavy door" in choice or "metal door" in choice or "prison" in choice)) or ("go" in choice and "prison" in choice):
            print "\nThis isn't the apropriate key... The door is locked."
        elif (("open" in choice or "unlock" in choice) and ("steel door" in choice or "heavy door" in choice or "metal door" in choice)) or ("go" in choice and "prison" in choice) and prison_key:
            print "\nThe lock clicks and you push the door open."
            prison()
        elif "open" in choice and "door" in choice:
                print "\nWhich one?"
        elif "drink" in choice and ("wine" in choice or "keg" in choice):
            print "\nYou alcoholic, now is not the time!"
        else:
            print invalid
        print curr_prompt
        choice = raw_input("> ").lower()
    print "\nYou push and walk through he wooden door."
    gravel_path()

The code works exactly as I want it to now, but I'm sure it can be written better. It's a mess, I know :)

I've been thinking of doing nested loops, for example: (This is a simplified, on the top of my head code and probably not working or the most logical version obviously but I hope you get the idea.)

while "list" not in choice:
    while "open" in choice:
        if "door" in choice:
            print "You open the door."
            new_room()
        print curr_prompt
        choice = raw_input("> ").lower()
    while "search" in choice:
        if "kegs" in choice:
            entrance_key = True
            print "You find an antique key"
        elif "steel door" in choice:
            print "\nThe steel door has a small barred opening at head's height. The inside of the room is pitch black."
        elif "bucket" in choice:
            print "\nInside you find... Skulls from rodents of some kind.", nothing
        else:
            print nothing
        print curr_prompt
        choice = raw_input("> ").lower()
    while "go" in choice:
        if "gravel" in choice or "path" in choice:
            gravel_path()
        elif "cellar" in choice:
            print "You're already in the cellar."
        else:
            print invalid
        print curr_prompt
        choice = raw_input("> ").lower()
    print curr_prompt
    choice = raw_input("> ").lower()
print command_list

Etc..

Sorry for newbie post format. I will improve :)

Thanks to anybody reading this and taking the time to help, you rock !

Aucun commentaire:

Enregistrer un commentaire