示例#1
0
 def init_level(self, map_name='level0.txt'):
     'Initialize map level'
     self.current_map = Map(map_name)
     self.current_map.main_menu_visible = True
     self.current_map.load()
     self.players = self.current_map.players
     self.monsters = self.current_map.monsters
示例#2
0
class ProtoNinja:
    'Main game'

    def __init__(self):
        'Init'
        self.screen = pygame.display.set_mode(RESOLUTION, pygame.RESIZABLE)
        self.clock = pygame.time.Clock()
        self.moving = set()
        self.current_map = None
        self.players = []

        # game_status can be 'menu', 'playing' or 'game over'
        self.game_status = 'menu'
        self.main_menu = Menu()

    def init_level(self, map_name='level0.txt'):
        'Initialize map level'
        self.current_map = Map(map_name)
        self.current_map.main_menu_visible = True
        self.current_map.load()
        self.players = self.current_map.players
        self.monsters = self.current_map.monsters

    def run_level(self):
        'One pass through the main loop for current level'
        self.current_map.update()
        rect = self.screen.blit(self.current_map.surface, (0, 0))
        pygame.display.update(rect)

    def restart_level(self, map_name=None):
        'Re-initialize this level'
        self.moving = set()
        if not map_name:
            map_name = self.current_map.map_name
        self.current_map = None
        self.players = []
        self.init_level(map_name)
        self.run_level()

    def check_player(self, player):
        'Check for collissions and apply damage on player'
        for monster in self.monsters:
            if player.touched_by(monster):
                player.apply_damage(damage=1)
                if player.state == DEAD:
                    self.game_status = 'game over'

    def edit_level(self):
        'Main loop for map editor'
        pass

    def start_editor(self):
        pass

    def parse_events(self, player):
        'Handle user events'
        for event in pygame.event.get():

            #TODO: use pygame.ACTIVEEVENT
            #TODO: It is received when gaining or loosing focus

            if event.type == pygame.VIDEORESIZE:
                pygame.display.set_mode(event.size, pygame.RESIZABLE)
            elif event.type == pygame.QUIT:
                return True

            if self.game_status == 'game over':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        self.game_status = 'menu'
                    elif event.key == pygame.K_ESCAPE:
                        self.game_status = 'menu'
                    return False
            elif self.game_status == 'menu':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        return True
                    elif event.key == pygame.K_RETURN:  # Resume game
                        self.game_status = (
                            'game over' if
                            player.state == DEAD else 'playing')
                    elif event.key == pygame.K_n:  # New game
                        self.restart_level()
                        self.game_status = 'playing'
                    elif event.key == pygame.K_e:  # Edit current level
                        self.start_editor()
                        self.game_status = 'editor'
                    return False
            elif self.game_status == 'playing':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.game_status = 'menu'
                    if event.key == pygame.K_DOWN:
                        player.moving |= {SOUTH}
                        player.moving -= {NORTH}
                    elif event.key == pygame.K_UP:
                        player.moving |= {NORTH}
                        player.moving -= {SOUTH}
                    elif event.key == pygame.K_LEFT:
                        player.moving |= {WEST}
                        player.moving -= {EAST}
                    elif event.key == pygame.K_RIGHT:
                        player.moving |= {EAST}
                        player.moving -= {WEST}
                    elif event.key == pygame.K_a:
                        self.players[0].alert(not self.players[0]._alert)
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_DOWN:
                        player.moving -= {SOUTH}
                    elif event.key == pygame.K_UP:
                        player.moving -= {NORTH}
                    elif event.key == pygame.K_LEFT:
                        player.moving -= {WEST}
                    elif event.key == pygame.K_RIGHT:
                        player.moving -= {EAST}
            elif self.game_status == 'editor':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.game_status = 'menu'

                #if click select object at coord
                #if none propose object to put

                #allow to place same image on next click
                #idem for selecting area w/mouse

        return False

    def main(self):
        'Main loop'
        _quit = False

        pygame.init()
        pygame.display.init()
        pygame.display.set_caption('Proto-Ninja')

        self.init_level()
        playing_states = ['playing', 'game over']
        while not _quit:
            if self.game_status in playing_states:
                self.run_level()
            elif self.game_status in ['editor']:
                self.edit_level()
            elif self.game_status == 'menu':
                self.main_menu.update()
                blit_centered(self.current_map.surface, self.main_menu.surface)
                rect = self.screen.blit(self.current_map.surface, (0, 0))
                pygame.display.update(rect)

            _quit = self.parse_events(self.players[0])

            if self.game_status in playing_states:
                self.check_player(self.players[0])
            self.clock.tick(SPEED)
        pygame.quit()