コード例 #1
0
ファイル: game.py プロジェクト: JohanLaine/ScaRab
 def _start_at_new_level(self, level_size):
     self.player.inventory.pop('key', '')
     self.player_pos = (0, 0)
     self.player_dir = Direction((1,0))
     self.level = LevelCreator().create_level(level_size)
コード例 #2
0
ファイル: game.py プロジェクト: JohanLaine/ScaRab
class Game(object):
    def __init__(
        self,
        player_name='Player',
        starting_level_size=(2,2),
        renderer=None,
    ):
        pygame.init()
        self.player = Player(player_name=player_name)
        self._start_at_new_level(starting_level_size)
        if renderer is not None:
            self.renderer = renderer

    def _start_at_new_level(self, level_size):
        self.player.inventory.pop('key', '')
        self.player_pos = (0, 0)
        self.player_dir = Direction((1,0))
        self.level = LevelCreator().create_level(level_size)

    def _move_player(self, vector):
        dx, dy = vector
        x, y = self.player_pos
        self.player_pos = (x + dx, y+dy)

    def display_time_left(self):
        time_now = pygame.time.get_ticks()
        time_left = (self.level.end_time - time_now) * 1000
        level_allowed_time = (self.level.end_time - self.level.start_time) * 1000
        print('Time left: {}/{} = {}%'.format(
            time_left, level_allowed_time, round(100*time_left/level_allowed_time)
        ))

    def change_renderer(self, kind):
        print('changing renderer')
        if kind == 'playerviewrenderer':
            self.renderer = PlayerViewRenderer()
        elif kind == 'maprenderer':
            self.renderer = MapRenderer()
        elif kind == 'johanmaprenderer':
            self.renderer = JohanMapRenderer()
        else:
            raise Exception('wtf what is the renderer')

    def try_moving_player_forward(self):
        if not isinstance(self.renderer, PlayerViewRenderer):
            print('Must use player renderer! Press p')
            return
        if self.player_dir.to_tuple() in self.level.nodes[self.player_pos].openings:
            print('FORWARD MY STEED')
            self.player_pos = tuple(
                np.array(self.player_pos) + np.array(self.player_dir.to_tuple())
            )
        elif self.level.nodes[self.player_pos].walls.get(
            self.player_dir.to_tuple(),
            ''
        ) == 'Exit':
            if 'key' in self.player.inventory:
                print('YOU HAVE COMPLETED THE LEVEL, CONGRATULATIONS!')
                print('NEW LEVEL SIZE: {size}x{size}'.format(size=self.level.size[0]+1))
                self._start_at_new_level(
                    level_size=tuple(
                        np.array(self.level.size)+1
                    )
                )
            else:
                print('YOU ARE NOT YET WORTHY. FIND THE KEY')
        else:
            print("That's a wall yo")

    def _update_creatures(self):
        map(
            self._update_one_creature,
            self.level.creatures
        )
        
    def _update_one_creature(self, creature):
        if creature.wants_to_move:
            creature.move()

    def main_loop(self):
        done = False
        clock = pygame.time.Clock()

        while not done:
            if pygame.time.get_ticks() + 3 > self.level.end_time:
                print('GAME OVER')
                print('GAME OVER')
                print('GAME OVER')
                done = True
            self._update_creatures()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                if event.type == pygame.MOUSEBUTTONUP:
                    print('hej')
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_m:
                        self.change_renderer('maprenderer')
                    if event.key == pygame.K_j:
                        self.change_renderer('johanmaprenderer')
                    if event.key == pygame.K_p:
                        self.change_renderer('playerviewrenderer')
                    if event.key == pygame.K_t:
                        self.display_time_left()
                    if event.key == pygame.K_LEFT:
                        if not isinstance(self.renderer, PlayerViewRenderer):
                            print('Must use player renderer! Press p')
                        else:
                            self.player_dir = self.player_dir.rotate_left()
                    if event.key == pygame.K_RIGHT:
                        if not isinstance(self.renderer, PlayerViewRenderer):
                            print('Must use player renderer! Press p')
                        else:
                            self.player_dir = self.player_dir.rotate_right()
                    if event.key == pygame.K_UP:
                        self.try_moving_player_forward()
                    if event.key == pygame.K_d:  # DEBUG
                        print('level nodes:', self.level.nodes)
                        print('player_pos:', self.player_pos) 
                        print('player_dir:', self.player_dir) 
                    if event.key == pygame.K_q:
                        import ipdb; ipdb.set_trace()
                    if event.key == pygame.K_i:
                        print('INVENTORY:', self.player.inventory.keys())
                    if event.key == pygame.K_RETURN:
                        if 'lamp' in self.player.inventory:
                            print('ACTIVATING LAMP')
                            self.player.inventory.pop('lamp')
                            self.level._set_start_and_end_time()
                        else:
                            print('NO TENGO LAMPO')
                    if event.key == pygame.K_SPACE:
                        current_node = self.level.nodes[self.player_pos]
                        if current_node.contains:
                            print('Picked up stuff!')
                            print(current_node.contains.keys())
                        else:
                            print('Did not pick up stuff')
                        self.player.inventory.update(current_node.contains)
                        current_node.contains = {}  # FIXME OMG SO BAD
            if hasattr(self, 'renderer'):
                self.renderer.render(
                    self.level,
                    player_pos=self.player_pos,
                    player_dir=self.player_dir
                )
            clock.tick(60)
        
        pygame.quit()