Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define a base State class with methods that all states will share.
- class State:
- def enter(self):
- pass # Called when entering the state; override in subclasses.
- def update(self):
- pass # Called every update/frame; override in subclasses.
- def exit(self):
- pass # Called when leaving the state; override in subclasses.
- # Define the IdleState, inheriting from State.
- class IdleState(State):
- def enter(self):
- print("Entering Idle State") # Print a message when entering Idle state.
- def update(self):
- # Check for transitions: if the 'W' key is pressed, transition to Walking state.
- if self.fsm.input_manager.is_key_pressed('W'):
- self.fsm.transition_to('Walking')
- def exit(self):
- print("Exiting Idle State") # Print a message when exiting Idle state.
- # Define the WalkingState, inheriting from State.
- class WalkingState(State):
- def enter(self):
- print("Entering Walking State") # Print a message when entering Walking state.
- def update(self):
- # In a real game you would handle movement here.
- # Check for transitions: if the 'W' key is released, transition back to Idle.
- if self.fsm.input_manager.is_key_released('W'):
- self.fsm.transition_to('Idle')
- def exit(self):
- print("Exiting Walking State") # Print a message when exiting Walking state.
- # Define a simple InputManager to handle keyboard inputs.
- class InputManager:
- def __init__(self):
- # Dictionary to store the pressed state of keys.
- self.key_pressed = {}
- # Dictionary to store the released state of keys.
- self.key_released = {}
- def process_inputs(self):
- """
- Process input from the user.
- This simple version waits for the user to type a key.
- If the user types a key, we record it as 'pressed'.
- If no key is typed (just pressing Enter), we simulate releasing key 'W' if it was pressed.
- """
- # Ask the user for input (in a real game, input would be non-blocking).
- key = input("Press a key (W to walk, or Enter to simulate release): ").strip()
- if key:
- # When a key is pressed, mark it as pressed (converted to uppercase).
- self.key_pressed[key.upper()] = True
- # Also, mark that key as not released.
- self.key_released[key.upper()] = False
- else:
- # If no key is pressed, simulate that 'W' has been released if it was pressed before.
- if self.key_pressed.get('W', False):
- self.key_pressed['W'] = False
- self.key_released['W'] = True
- else:
- # Reset the released state if nothing is happening.
- self.key_released = {}
- def is_key_pressed(self, key):
- """
- Return True if the specified key is currently marked as pressed.
- """
- return self.key_pressed.get(key.upper(), False)
- def is_key_released(self, key):
- """
- Return True if the specified key is currently marked as released.
- """
- return self.key_released.get(key.upper(), False)
- # Define the Finite State Machine (FSM) class that manages states.
- class FSM:
- def __init__(self):
- # Dictionary to hold the states by name.
- self.states = {}
- # Variable to track the current state.
- self.current_state = None
- # Create an instance of the InputManager to handle inputs.
- self.input_manager = InputManager()
- def add_state(self, name, state):
- """
- Add a new state to the FSM.
- 'name' is a string key, and 'state' is an instance of a State.
- """
- self.states[name] = state
- # Give the state a reference back to the FSM so it can trigger transitions.
- state.fsm = self
- def transition_to(self, state_name):
- """
- Transition from the current state to a new state.
- """
- # If there is a current state, call its exit method.
- if self.current_state is not None:
- self.current_state.exit()
- # Set the new state as the current state.
- self.current_state = self.states[state_name]
- # Call the enter method of the new state.
- self.current_state.enter()
- def update(self):
- """
- Update the current state by calling its update method.
- """
- if self.current_state:
- self.current_state.update()
- # Main execution starts here.
- # Create an instance of the FSM.
- fsm = FSM()
- # Add the Idle and Walking states to the FSM.
- fsm.add_state('Idle', IdleState())
- fsm.add_state('Walking', WalkingState())
- # Set the initial state of the FSM to Idle.
- fsm.transition_to('Idle')
- # Start the game loop.
- # In this example, the loop will continue indefinitely.
- # In a real game, you would include a proper exit condition.
- while True:
- # Process inputs using the InputManager.
- fsm.input_manager.process_inputs()
- # Update the FSM which in turn updates the current state.
- fsm.update()
- # Print the current state (class name)
- print("Current State:", fsm.current_state.__class__.__name__)
- # Print a separator for clarity.
- print("-" * 40)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement