class AnimationRenderComponent(RenderComponent): resource_manager = ResourceManager.get_instance() def __init__(self, entity, animation_sheet, width, height): self.entity = entity self.frame = 0 self.elapsed_time = 0 self.animation_sheet = animation_sheet self.width, self.height = width, height self.set_animation('idle') def set_animation(self, animation): self.animation_frames, self.seconds_per_frame = self.resource_manager.get_animation( self.animation_sheet, animation, self.width, self.height ) self.frame = 0 self.elapsed_time = 0 def update(self, delta): self.elapsed_time += delta iterations = int(self.elapsed_time // self.seconds_per_frame) self.frame += iterations self.frame %= len(self.animation_frames) self.elapsed_time %= self.seconds_per_frame def draw(self, screen): screen.blit(self.animation_frames[self.frame], (int(self.entity.position[0]), int(self.entity.position[1])))
class SpriteRenderComponent(RenderComponent): resource_manager = ResourceManager.get_instance() def __init__(self, entity, sprite_file, width, height): self.entity = entity self.width, self.height = width, height self.sprite = SpriteRenderComponent.resource_manager.get_sprite(sprite_file) def draw(self, screen): screen.blit(self.sprite, self.entity.position)
def run(): resource_manager = ResourceManager.get_instance() system_manager = set_up_systems() from lib.ecs.component.physics import PhysicsComponent # PinnedBlock(100, 230, 100, 25) # PinnedBlock(400, 230, 100, 25) PinnedBlock(150, 300, 500, 25) PlayerBlock(200, 200, 64, 64) # Spirit(500, 0, 64, 64) # PinnedBlock(503, 230, 100, 25) # PlayerBlock(503, 0, 200, 200) # p2 = PlayerBlock(301, 31, 200, 200) # p2.set_components([ImmovableComponent()]) # WackBlock(100, 100, [20 * Vec2d(0.5, -0.5), 20 * Vec2d(-0.5, 0.5), 20 * Vec2d(0.5, 1.5), 20 * Vec2d(1.5, 0.5)]) # WackBlock(100, 100, [20 * Vec2d(1, 0), 20 * Vec2d(0, 1), 20 * Vec2d(1, 2), 20 * Vec2d(2, 1)]) if settings.ENABLE_PROFILING: pr = cProfile.Profile() pr.enable() max_frame_time = 0.25 fixed_update_dt = 0.01 accumulated_time = 0 current_time = time.time() last_render_time = 0 game_over = False while not game_over: new_time = time.time() frame_time = new_time - current_time current_time = new_time if frame_time >= max_frame_time: frame_time = max_frame_time accumulated_time += frame_time while accumulated_time >= fixed_update_dt: accumulated_time -= fixed_update_dt if system_manager.update(fixed_update_dt) is False: game_over = True if settings.ENABLE_PROFILING: pr.disable() s = StringIO.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print s.getvalue()
class AudioManager(object): instance = None resource_manager = ResourceManager.get_instance() @staticmethod def get_instance(): if AudioManager.instance is None: AudioManager.instance = AudioManager() return AudioManager.instance def play(self, name): audio = self.resource_manager.get_audio(name) if audio: audio.play()
def run(): resource_manager = ResourceManager.get_instance() resource_manager.setup(settings.SPRITES_FOLDER) system_manager = set_up_systems() obstacle1 = Obstacle(Vec2d(250, 50), 100, 100, 10) obstacle2 = Obstacle(Vec2d(250, 250), 100, 100, 10) obstacle3 = Obstacle(Vec2d(250, 450), 100, 100, 10) # obstacle3 = Obstacle(Vec2d(300, 50), 100, 100, 10) # obstacle3 = Obstacle(Vec2d(300, 250), 100, 100, 10) # obstacle3 = Obstacle(Vec2d(300, 450), 100, 100, 10) obstacle3 = Obstacle(Vec2d(400, 50), 100, 100, 10) obstacle3 = Obstacle(Vec2d(400, 250), 100, 100, 10) obstacle3 = Obstacle(Vec2d(400, 450), 100, 100, 10) system_manager.send_message( {'message_type': MESSAGE_TYPE.INIT_MOVEMENT_PLANNER}) queen = Queen() clock = pygame.time.Clock() quit = False while True: system_manager.update(1 / float(30)) for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() system_manager.send_message({ 'message_type': MESSAGE_TYPE.MOVE_ENTITY, 'entity': queen, 'goal': mouse_pos, }) keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: quit = True if quit: sys.exit() clock.tick(30)
def run(): resource_manager = ResourceManager.get_instance() resource_manager.setup(settings.SPRITES_FOLDER) system_manager = set_up_systems() obstacle1 = Obstacle(Vec2d(250, 50), 100, 100, 10) obstacle2 = Obstacle(Vec2d(250, 250), 100, 100, 10) obstacle3 = Obstacle(Vec2d(250, 450), 100, 100, 10) # obstacle3 = Obstacle(Vec2d(300, 50), 100, 100, 10) # obstacle3 = Obstacle(Vec2d(300, 250), 100, 100, 10) # obstacle3 = Obstacle(Vec2d(300, 450), 100, 100, 10) obstacle3 = Obstacle(Vec2d(400, 50), 100, 100, 10) obstacle3 = Obstacle(Vec2d(400, 250), 100, 100, 10) obstacle3 = Obstacle(Vec2d(400, 450), 100, 100, 10) system_manager.send_message({'message_type': MESSAGE_TYPE.INIT_MOVEMENT_PLANNER}) queen = Queen() clock = pygame.time.Clock() quit = False while True: system_manager.update(1 / float(30)) for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() system_manager.send_message({ 'message_type': MESSAGE_TYPE.MOVE_ENTITY, 'entity': queen, 'goal': mouse_pos, }) keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: quit = True if quit: sys.exit() clock.tick(30)