Python Forum
[PyGame] I've been programming in a vacuum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] I've been programming in a vacuum
#6
(Jan-16-2018, 05:00 AM)Rabbyte Wrote: Should all the static data be in one file or split into categorical separate files?
Also do I just import these files or is there a more pythonic way to get these values back into the main file?
I would start with 1 file for simplicity. I would say the main goal for now should be breaking up the main file. You can do that later depending on how complex that part is after.
(Jan-16-2018, 05:00 AM)Rabbyte Wrote: When I changed the code it threw an error saying "TypeError: list indices must be integers or slices, not Sound"
for i in sound_typing_multiple:
    sound_typing_multiple[i].set_volume(volume_typing)
it would be roughly this
for sound in sound_typing_multiple:
    sound.set_volume(volume_typing)
(Jan-16-2018, 05:00 AM)Rabbyte Wrote: I'm also not sure how to change my huge if/elif tree into a dictionary. it is working as a lookup table, but it runs the code block that is looked up...I'm not sure how to implement this with a dictionary. My only thought would be to assign the key to the command, and return a command ID that then gets put into an interpreter function in a new file that is again a series of if/elif that instead take the ID as the condition. I know by just reading that, this is clearly not the right step to take, though putting the command handler in a separate file would clean up the main file
I think its harder to rearrange a program than it is to do it from scratch. So its going to be a lot of work to rearrange the program a different way.

Note: A dictionary key can also be a function, callable at any time (minus the parenthesis that call it within the dictionary value). This function would contain whatever is inside the main if/elif blocks. If it is another long if/elif inside that, then it would be a nested key/value pair for that if/elif structure. And so on and so on. So at the very worst you could spit the main if/elif into a dictionary, and everything nested in a function. Then mesh those functions in the dictionary after slowly.

Your dictionary then would become the "lookup table" as you put it.

Im not sure what else to say. That tutorial's example puts it very well on how you would execute from the dictionary. However the more nested you have the more nested dictionary you are going to have.
Quote:
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            # bad movement
            print("You can't go that way.")

The alternative is what you have.
    if command.lower() in ('n', 'north'):
        if current_room == 'empty':
            current_room = 'temple'
        elif current_room == 'bedroom':
            current_room = 'torture'
        else:
            print("You can't go that way.")
    elif command.lower() in ('s', 'south'):
        if current_room == 'temple':
            current_room = 'empty'
        elif current_room == 'torture':
            current_room = 'bedroom'
        else:
            print("You can't go that way.")
    elif command.lower() in ('e', 'east'):
        if current_room == 'empty':
            current_room = 'bedroom'
        elif current_room == 'temple':
            current_room = 'torture'
        else:
            print("You can't go that way.")
    elif command.lower() in ('w', 'west'):
        if current_room == 'bedroom':
            current_room = 'empty'
        elif current_room == 'torture':
            current_room = 'temple'
        else:
            print("You can't go that way.")
    # quit game
    elif command.lower() in ('q', 'quit'):
        break
    # bad command
    else:
        print("I don't understand that command.")
This part is so repetitive that you could just instead use a function to handle all this and plug in the differences.
Quote:
def event_handler():
    global terminateDisplay, user_input, terminal_input
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            terminateDisplay = True

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_0 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += ")"

                else:
                    user_input += "0"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_1 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "!"

                else:
                    user_input += "1"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_2 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "@"

                else:
                    user_input += "2"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_3 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "#"

                else:
                    user_input += "3"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_4 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "$"

                else:
                    user_input += "4"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_5 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "%"

                else:
                    user_input += "5"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_6 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "^"

                else:
                    user_input += "6"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_7 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "&"

                else:
                    user_input += "7"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_8 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "*"

                else:
                    user_input += "8"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_9 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "("

                else:
                    user_input += "9"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_a and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "A"

                else:
                    user_input += "a"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_b and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "B"

                else:
                    user_input += "b"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_c and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "C"

                else:
                    user_input += "c"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_d and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "D"

                else:
                    user_input += "d"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_e and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "E"

                else:
                    user_input += "e"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_f and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "F"

                else:
                    user_input += "f"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_g and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "G"

                else:
                    user_input += "g"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_h and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "H"

                else:
                    user_input += "h"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_i and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "I"

                else:
                    user_input += "i"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_j and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "J"

                else:
                    user_input += "j"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_k and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "K"

                else:
                    user_input += "k"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_l and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "L"

                else:
                    user_input += "l"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_m and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "M"

                else:
                    user_input += "m"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_n and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "N"

                else:
                    user_input += "n"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_o and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "O"

                else:
                    user_input += "o"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_p and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "P"

                else:
                    user_input += "p"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_q and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "Q"

                else:
                    user_input += "q"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_r and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "R"

                else:
                    user_input += "r"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_s and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "S"

                else:
                    user_input += "s"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_t and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "T"

                else:
                    user_input += "t"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_u and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "U"

                else:
                    user_input += "u"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_v and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "V"

                else:
                    user_input += "v"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_w and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "W"

                else:
                    user_input += "w"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_x and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "X"

                else:
                    user_input += "x"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_y and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "Y"

                else:
                    user_input += "y"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_z and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "Z"

                else:
                    user_input += "z"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_SLASH and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "?"

                else:
                    user_input += "/"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_BACKSLASH and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "|"

                else:
                    user_input += "\\"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_COMMA and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "<"

                else:
                    user_input += ","

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_PERIOD and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += ">"

                else:
                    user_input += "."

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_SEMICOLON and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += ":"

                else:
                    user_input += ";"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_QUOTE and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += '"'

                else:
                    user_input += "'"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_BACKQUOTE and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "~"

                else:
                    user_input += "`"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_LEFTBRACKET and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "{"

                else:
                    user_input += "["

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_RIGHTBRACKET and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "}"

                else:
                    user_input += "]"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_EQUALS and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "+"

                else:
                    user_input += "="

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_MINUS and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "_"

                else:
                    user_input += "-"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_BACKSPACE and len(user_input) > 0:

                user_input = user_input[:-1]
                sound_enter[random.randint(0, 2)].play()

            elif event.key == pygame.K_SPACE and len(user_input) < 1045:

                user_input += " "
                sound_enter[random.randint(0, 2)].play()

            elif event.key == pygame.K_RETURN and len(user_input) > 0:

Also while looking at your code more i found another repetition that should be removed. You have what i call "sentdex syndrome". Which is basically numerous event loops / updates instead of having one single event loop and update to handle states. It gets repetitive and confusing and makes it spaghetti code. Here is an example of what i mean.
https://python-forum.io/Thread-PyGame-wa...-tutorials

Also you should never ever have a time sleep in a GUI program. Even if it is a replication of a terminal. Time sleep pauses everything. You should use a GUI timer instead.
Recommended Tutorials:
Reply


Messages In This Thread
I've been programming in a vacuum - by Rabbyte - Jan-16-2018, 02:46 AM
RE: I've been programming in a vacuum - by Rabbyte - Jan-16-2018, 04:00 AM
RE: I've been programming in a vacuum - by Rabbyte - Jan-16-2018, 05:00 AM
RE: I've been programming in a vacuum - by metulburr - Jan-16-2018, 02:45 PM
RE: I've been programming in a vacuum - by Windspar - Jan-16-2018, 03:59 PM
RE: I've been programming in a vacuum - by Rabbyte - Jan-16-2018, 05:25 PM
RE: I've been programming in a vacuum - by Windspar - Jan-16-2018, 05:43 PM
RE: I've been programming in a vacuum - by Rabbyte - Jan-16-2018, 11:07 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020