Exemplo n.º 1
0
    def run(self):
        # SPRITE_MANAGER.instance.clear(SCREEN, BACKGROUND)

        # for debug purposes...
        f = open("log3", 'a')
        sys.stdout = f

        while not self.exit:
            SCREEN.fill((0, 0, 0))
            move_and_draw_stars(SCREEN, CAMERA)
            pygame.draw.rect(SCREEN, (255, 0, 0), CAMERA.inner_rect, 2)
            # pygame.display.flip()
            # SCREEN.blit(BACKGROUND, BACKGROUND_POS)

            # SPRITE_MANAGER.instance.clear(SCREEN, BACKGROUND)
            # print("Background pos")
            # print(BACKGROUND_POS)
            # pygame.draw.rect(SCREEN, (255, 0, 0), CAMERA.inner_rect, 1)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.exit = True
            self.clock.tick(50)
            self.update()
            self.draw()

            # should call this once per game loop to ensure pygame talks to sys
            pygame.event.pump()
        f.close()
        sys.stdout = sys.__stdout__
        pygame.quit()
Exemplo n.º 2
0
    def run(self):
        # SPRITE_MANAGER.instance.clear(SCREEN, BACKGROUND)

        # for debug purposes...
        f = open("log3", 'a')
        sys.stdout = f

        while not self.exit:
            SCREEN.fill((0, 0, 0))
            move_and_draw_stars(SCREEN, CAMERA)
            pygame.draw.rect(SCREEN, (255, 0, 0), CAMERA.inner_rect, 2)
            # pygame.display.flip()
            # SCREEN.blit(BACKGROUND, BACKGROUND_POS)

            # SPRITE_MANAGER.instance.clear(SCREEN, BACKGROUND)
            # print("Background pos")
            # print(BACKGROUND_POS)
            # pygame.draw.rect(SCREEN, (255, 0, 0), CAMERA.inner_rect, 1)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.exit = True
            self.clock.tick(50)
            self.update()
            self.draw()

            # should call this once per game loop to ensure pygame talks to sys
            pygame.event.pump()
        f.close()
        sys.stdout = sys.__stdout__
        pygame.quit()
Exemplo n.º 3
0
    def draw(self, **kwargs):
        SCREEN.fill(self.background)

        if 'life' in kwargs:
            if kwargs['life'] != None:
                for cell in kwargs['life']:
                    if cell.pos[0] >= 0 and cell.pos[0] <= WIDTH:
                        if cell.pos[1] >= 0 and cell.pos[1] <= HEIGHT:
                            cell.draw()

        for rect in self.rects:
            pygame.draw.rect(
                SCREEN, rect['color'],
                (rect['x'], rect['y'], rect['WIDTH'], rect['HEIGHT']),
                rect['stroke'])

        for line in self.lines:
            pygame.draw.line(SCREEN, line['color'],
                             (line['startx'], line['starty']),
                             (line['endx'], line['endy']), line['width'])

        for text in self.texts:
            SCREEN.blit(text[0], (text[1]))

        button_list = self.buttons.keys()
        for button in button_list:
            self.buttons[button].draw(SCREEN)
Exemplo n.º 4
0
    def init_game(self):
        # set game constants
        SCREEN.fill((0, 0, 0))
        init_stars(SCREEN)

        load_level(self.joysticks)

        self.exit = False
Exemplo n.º 5
0
    def init_game(self):
        # set game constants
        SCREEN.fill((0, 0, 0))
        init_stars(SCREEN)

        load_level(self.joysticks)

        self.exit = False
Exemplo n.º 6
0
def main():
    pygame.init()
    # Ressources loading
    ImageManager.load()
    FontManager.load()
    SoundManager.load()
    # loading of the maze
    tiles = []
    Model.load_from_file(tiles)
    Controller.randomize_objects(tiles)
    # States of the game
    state = "rules"
    while True:
        pygame.time.Clock().tick(30)
        # Display
        SCREEN.fill((0, 0, 0))
        View.draw_title()
        if state == "rules":
            View.draw_rules()
        else:
            View.draw_maze(tiles)
            View.draw_picked_objects(Controller.counter_object)
            if state == "win":
                View.draw_win_message()
            elif state == "lose":
                View.draw_lose_message()
        pygame.display.flip()
        # Events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.KEYDOWN:
                if state == "rules":
                    if event.key == pygame.K_q:
                        return
                    elif event.key == pygame.K_s:
                        state = "running"
                elif state == "running":
                    if event.key in BINDING_MOVE:
                        Controller.move(tiles, View.index_macgyver(tiles),
                                        BINDING_MOVE[event.key])
        # Logics
        if state == "running":
            state = Controller.check_win(tiles, View.index_macgyver(tiles))
        elif state in ("win", "lose"):
            SoundManager.get(state).play().set_volume(0.1)
            pygame.time.wait(4000)
            break
Exemplo n.º 7
0
    def draw(self, **kwargs):
        SCREEN.fill(self.background)

        if 'life' in kwargs:
            if kwargs['life'] != None:
                for cell in kwargs['life']:
                    if cell.pos[0] >= 0 and cell.pos[0] <= WIDTH:
                        if cell.pos[1] >= 0 and cell.pos[1] <= HEIGHT:
                            cell.draw()

        for rect in self.rects:
            pygame.draw.rect(SCREEN, rect['color'], (rect['x'],rect['y'],rect['WIDTH'],rect['HEIGHT']), rect['stroke'])

        for line in self.lines:
            pygame.draw.line(SCREEN, line['color'], (line['startx'],line['starty']), (line['endx'],line['endy']), line['width'])


        for text in self.texts:
            SCREEN.blit(text[0], (text[1]))

        button_list = self.buttons.keys()
        for button in button_list:
            self.buttons[button].draw(SCREEN)
Exemplo n.º 8
0
    def start_game(self):
        player_spawn_x = WIDTH / 2.13
        player_spawn_y = HEIGHT / 2.25
        player = Player(self.entities, self.moving_colliders, player_spawn_x,
                        player_spawn_y, 100, 300)

        sheet = Location('Surface map.txt',
                         self.location_cells,
                         'surface',
                         0,
                         0,
                         player.speed,
                         collider_group=None)
        sheet.location_creating()
        terrain = Location('Terrain map.txt',
                           self.location_objects,
                           'terrain',
                           0,
                           0,
                           player.speed,
                           collider_group=self.static_colliders,
                           sheet=sheet)
        terrain.location_creating()

        camera = Camera()
        previous_camera_works = False

        while True:
            self.frame_tick = self.clock.tick() / 1000
            set_objects_ticks(self.frame_tick, player, camera)
            determine_direction(player)
            SCREEN.fill((0, 0, 0))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.terminate_game()

            camera_works = camera.update(player)

            if not camera_works:
                if not previous_camera_works:
                    check_colliders(self.moving_colliders,
                                    self.static_colliders, self.frame_tick)
                else:
                    check_colliders(self.moving_colliders,
                                    self.static_colliders,
                                    self.frame_tick,
                                    clearing=True)
            else:
                if previous_camera_works:
                    check_colliders(self.static_colliders,
                                    self.moving_colliders,
                                    self.frame_tick,
                                    camera=camera,
                                    location_groups=[
                                        self.location_cells,
                                        self.location_objects
                                    ])
                else:
                    check_colliders(self.static_colliders,
                                    self.moving_colliders,
                                    self.frame_tick,
                                    clearing=True,
                                    camera=camera,
                                    location_groups=[
                                        self.location_cells,
                                        self.location_objects
                                    ])

            previous_camera_works = camera_works

            for cell in self.location_cells.get_sprites():
                camera.apply(cell)
            for obj in self.location_objects.get_sprites():
                camera.apply(obj)

            self.location_cells.draw(SCREEN)
            self.entities.update()
            self.entities.draw(SCREEN)
            self.location_objects.draw(SCREEN)

            if COLLIDERS_CONTOUR:
                draw_group_contour(self.moving_colliders, 'red', 2)
                draw_group_contour(self.static_colliders, 'red', 2)

            pygame.display.flip()