RPG Systems
Simple Validate User Input System 1
Brief:
_______________________________________________________________
2202/02/13 02:55 24hr PST
!! Post Updated !! Tutorial / How To video added below.
This was actually part of my "Simple Turn System" tutorial. But I realized it
was substantial enough to be it's own post. The system is meant to granulate
the process of taking in user input, and checking if that input is valid. This
is broken into three different parts.
- The prompt with a list of options
-
The player needs some idea of what they can do in the game at any given
moment. So we need a way to convey that information.
- The validation check
-
Whether it is a "yes/no" question, a list of menu actions, or items in an
inventory. We need a way to check if what the player has entered in
response to the prompt was in fact an option displayed to the player.
- The validation loop
-
Generally we want to ask the player what they want to do until we get an
response that is allowed at the time.
Functionality:
_______________________________________________________________
I need to test the code in a working project to see if it needs any
adjustments. But for the text project everything works as intended.
- Improvements:
-
N/A
- Issues:
-
N/A
Demo:
_______________________________________________________________
How To:
Code Snippet:
_______________________________________________________________
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 41 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 |
import time import os __IMPORTS__ = dir() class Game_App(): """Game application doc_string""" # =================================== # CLASS OBJECT CONSTRUCTOR def __init__(self, *args, **kwargs): self.delay_clear_console() self.game_loop() # CLASS OBJECT CONSTRUCTOR # =================================== # =================================== # PLAYER INPUT HANDLING & Display # Fluff so we can see code output @staticmethod def delay_clear_console(delay=0): try: time.sleep(int(delay)) except: pass os.system('cls' if os.name == 'nt' else 'clear') def action_options(self, actions_dict, div_space=" - "): for action_index, action_text in enumerate(actions_dict): print(f"{action_index+1}{div_space}{action_text}") def validate_choice(self, choice_str, actions_dict): if choice_str == '': return None validated_choice = None if choice_str.isdigit(): option_num = int(choice_str) if option_num < len(actions_dict)+1: validated_choice = (choice_str, list(actions_dict)[option_num-1]) else: action_selected = [ (action_index, action) for action_index, action in enumerate(actions_dict) if choice_str in action ] if [] != action_selected: validated_choice = (action_selected[0][0], action_selected[0][1]) if tuple != type(validated_choice) or () == validated_choice: validated_choice = None return validated_choice def standard_input(self, prompt:str, optionText_actionFunc_dict:{'key':'func'}) -> (('index', 'key'), {'key':'func'}): action_display_order = optionText_actionFunc_dict action_key_value = None while None == action_key_value: print(f"Player x's turn:") print("--------------------") self.action_options(action_display_order, " - ") print("--------------------") print(f"{prompt}:") option_selected = input(">>").lower() action_key_value = self.validate_choice(option_selected, action_display_order) if None == action_key_value: print() print(f"<{option_selected}> is not valid, enter option number or name from list.") self.delay_clear_console(2) self.delay_clear_console() return action_key_value, action_display_order # PLAYER INPUT HANDLING # =================================== def game_loop(self): running = True while running: self.delay_clear_console() # Clear previous 'frame' selected_option, options = self.standard_input( "selection option by name or number", {'exit':"self.exit_app", 'leave game':"self.exit_game", 'end turn':"self.end_turn", 'menu':"self.open_menu", 'wait':"self.order_unit_wait", 'status':"self.status", 'move':"self.order_unit_move", 'act':"self.order_unit_action" } ) # Visual Debug of output print(selected_option) # . . . Do things with the response Here . . . # End of loop self.delay_clear_console(2) # Loop termination condition if 'exit' == selected_option[1]: running = False # End of application print("Don't go the drones need you. They look up to you.") self.delay_clear_console(3) if __name__ == "__main__": game_catch = Game_App() |
Credits & Resources:
- Code formatted via: http://hilite.me/ : Styling: Python, monokai