예제 #1
0
파일: app.py 프로젝트: sharat87/lights-poof
    def __init__(self, dev=False):
        self.dev = dev

        pygame.init()

        self.display = pygame.display.set_mode((320, 420))
        pygame.display.set_caption('Lights poof!')

        self.init_bg_surface()

        self.solver = SolverState(self.display)
        self.solver.listen('done-solving', self.on_solver_done)

        self.game_over_state = GameOverState(self.display)
        self.game_over_state.listen('resume-click', self.on_resume_click)
        self.game_over_state.listen('new-click', self.on_new_click)
        self.game_over_state.listen('restart-click', self.on_restart_click)
        self.game_over_state.listen('solve-click', self.on_solve_click)

        self.init_new_game()

        self.menu = MenuState(self.display)
        self.menu.listen('resume-click', self.on_resume_click)
        self.menu.listen('new-click', self.on_new_click)
        self.menu.listen('restart-click', self.on_restart_click)
        self.menu.listen('solve-click', self.on_solve_click)

        self.current_state = None
        self.set_state(self.game)
예제 #2
0
파일: app.py 프로젝트: sharat87/lights-poof
class App(object):

    def __init__(self, dev=False):
        self.dev = dev

        pygame.init()

        self.display = pygame.display.set_mode((320, 420))
        pygame.display.set_caption('Lights poof!')

        self.init_bg_surface()

        self.solver = SolverState(self.display)
        self.solver.listen('done-solving', self.on_solver_done)

        self.game_over_state = GameOverState(self.display)
        self.game_over_state.listen('resume-click', self.on_resume_click)
        self.game_over_state.listen('new-click', self.on_new_click)
        self.game_over_state.listen('restart-click', self.on_restart_click)
        self.game_over_state.listen('solve-click', self.on_solve_click)

        self.init_new_game()

        self.menu = MenuState(self.display)
        self.menu.listen('resume-click', self.on_resume_click)
        self.menu.listen('new-click', self.on_new_click)
        self.menu.listen('restart-click', self.on_restart_click)
        self.menu.listen('solve-click', self.on_solve_click)

        self.current_state = None
        self.set_state(self.game)

    def main_loop(self):

        while True:

            for event in pygame.event.get():
                self.handle(event)

            self.draw()
            time.sleep(.01)

    def draw(self):
        # Draw background.
        self.display.blit(self.bg_surface, (0, 0))

        # Draw the state.
        self.current_state.draw()

        # Update the display.
        pygame.display.update()

    def handle(self, event):

        if event.type == QUIT or (event.type == KEYUP and event.key == 27):
            pygame.quit()
            sys.exit()

        else:
            self.current_state.handle(event)

    def init_bg_surface(self):
        bg_image = pygame.image.load('img/bg.png')
        self.bg_surface = pygame.Surface(self.display.get_size())

        x = y = 0
        image_width, image_height = bg_image.get_size()
        bg_width, bg_height = self.display.get_size()

        while x < bg_width:
            y = 0

            while y < bg_height:
                self.bg_surface.blit(bg_image, (x, y))
                y += image_height

            x += image_width

    def init_new_game(self):
        self.game = GameState(self.display,
                level=([(2, 2)] if self.dev else None))
        self.game.listen('menu-click', self.on_menu_click)
        self.game.listen('game-over', self.on_game_over)

        self.solver.game = self.game

        self.game_over_state.game = self.game

    def set_state(self, state):
        old_state = self.current_state
        self.current_state = state

        if hasattr(old_state, 'deactivated'):
            old_state.deactivated()

        if hasattr(self.current_state, 'activated'):
            self.current_state.activated()

    def on_menu_click(self, event):
        self.set_state(self.menu)

    def on_game_over(self, event):
        self.set_state(self.game_over_state)

    def on_resume_click(self, event):
        self.set_state(self.game)

    def on_new_click(self, event):
        self.init_new_game()
        self.set_state(self.game)

    def on_restart_click(self, event):
        self.game.restart()
        self.set_state(self.game)

    def on_solve_click(self, event):
        self.set_state(self.solver)

    def on_solver_done(self, event):
        self.set_state(self.game)