def __init__(self, name, entities_data): self.name = name self.entities = [] self.started_entities = [] self.event_input = None self.cont_input = None for entity_data in entities_data: position = (entity_data["X"], entity_data["Y"]) entity = Entity(entity_data["Name"], position) for component_data in entity_data["Components"]: try: component_constructor = getattr(controller.component, component_data["Type"]) component = component_constructor() component.scene = self data = component_data["ComponentData"] if not len(data) == 0: component.load_data(data) entity.add_component(component) except AttributeError: debug.log(component_data["Type"] + " not recognised :/") self.entities.append(entity)
def run(self): if self.scene is None: debug.log('scene not set!') return self.scene.start() self.tick_method = self.tick super().run()
def invoke(self): try: getattr(sys.modules[__name__], self.action)() except AttributeError as error: debug.log('Something went wrong :(') debug.log(error.args) raise error
def target_reached(self): own_pixel_pos = self.entity.x, self.entity.y target_pixel_pos = helpers.multiply_vector(self.target_pos, configuration.tile_size) result = helpers.vector_equality(own_pixel_pos, target_pixel_pos) debug.log('[{0}]-[{1}]: {2}'.format(own_pixel_pos, target_pixel_pos, result)) return result
def run(self): if self.tick_method is None: debug.log('no assigned tick method') return self.setup() self.total_frames = 0 debug.log('starting main loop execution') while True: self.tick_method() self.clock.tick(configuration.clock_tick) pygame.display.flip() self.total_frames += 1
def set_target(self, direction): """ sets target to neighbouring tile specified by direction :param direction: target direction :return: """ debug.log(direction) self.entity.direction = direction direction_vector = helpers.direction_to_direction_vector(direction) next_tile_pos = helpers.add_vectors(self.position, direction_vector) if not self.tile_map_component.in_bounds(next_tile_pos): debug.log('can\'t go that way!') return next_tile_type = self.tile_map_component.get_tile_type(next_tile_pos) if next_tile_type == 'collision': return if not helpers.vector_equality(next_tile_pos, self.position): self.target_pos = next_tile_pos
def tick(self): pygame.event.pump() self.gui.draw() self.overlay.draw() mouse_input = input.tick('mouse') if mouse_input[0] != self.last_tick_mouse: mouse_changed = mouse_input[0] != 'none' self.overlay.draw_mouse = mouse_changed if mouse_changed: debug.log('mouse button down') debug.log(mouse_input[0] + ' ' + str(mouse_input[1])) else: debug.log('mouse button up') self.last_tick_mouse = mouse_input[0]
def start(self): self.event_input = 'none' self.cont_input = 'none' while not self.ready_to_start(): debug.log('preparing to start entities...') entities_to_start = [] for ent in self.entities: if not ent.is_started(): entities_to_start.append(ent) debug.log(str(len(entities_to_start)) + ' entities ready to start.') debug.log('starting...') for entity in entities_to_start: try: entity.start() except Exception as e: debug.log('could not start entity. Logging error:') debug.log(e) debug.log('started {0} entities :)'.format(len(self.entities)))
def start(self): self.event_input = 'none' self.cont_input = 'none' while not self.ready_to_start(): debug.log('preparing to start entities...') entities_to_start = [] for ent in self.entities: if not ent.is_started(): entities_to_start.append(ent) debug.log( str(len(entities_to_start)) + ' entities ready to start.') debug.log('starting...') for entity in entities_to_start: try: entity.start() except Exception as e: debug.log('could not start entity. Logging error:') debug.log(e) debug.log('started {0} entities :)'.format(len(self.entities)))
import argparse from core import menu from util import debug parser = argparse.ArgumentParser(description='Pythonmon game engine') parser.add_argument('-l', action='store_true', help='enable logging') args = parser.parse_args() # set logging state debug.enabled = args.l debug.log('Program started') start_menu = menu.make_console_menu('start-menu') start_menu.run()