Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on Oct 20, 2017
- Simple State Machine
- @author: Begining Game Development with Python using Pygame Book
- '''
- class State(object):
- def __init__(self, name):
- self.name = name
- def update(self, dt_s):
- pass
- def check_conditions(self):
- pass
- def entry_actions(self):
- pass
- def exit_actions(self):
- pass
- class State_Machine(object):
- def __init__(self):
- self.states = {}
- self.active_state = None
- def add_state(self, state):
- self.states[state.name] = state
- def update(self, dt):
- if self.active_state is None:
- return
- self.active_state.update(dt)
- new_state_name = self.active_state.check_conditions()
- if new_state_name is not None:
- self.set_state(new_state_name)
- def set_state(self, new_state_name):
- if self.active_state is not None:
- self.active_state.exit_actions()
- self.active_state = self.states[new_state_name]
- self.active_state.entry_actions()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement