Exemplo n.º 1
0
    def __init__(self, screen, level="biglevel", players_=(None, None, None,
        None)):
        """
        Initialize a game with a list of player and a level,
        level is the basename of the level in levels/
        """

        self.size = (
            CONFIG.general.WIDTH,
            CONFIG.general.HEIGHT)

        self.first_frame = True
        self.notif = []
        self.type = 'local'
        self.screen = screen

        self.items = []
        self.events = EventManager()
        self.gametime = 0

        # we load the bool for smooth scrolling here, for a better performance
        self.smooth_scrolling = CONFIG.general.SMOOTH_SCROLLING

        self.level = Level(level)
        if screen is not None:
            self.zoom = 1
            # loading level
            self.level_place = [0, 0]
            self.icon_space = self.size[0] / len(players_)

            # loading players

        self.load_players(players_)

        # the optional progress bar for the players' lives
        self.progress_bar_size = (
                82.5 * CONFIG.general.WIDTH / 800,
                12.5 * CONFIG.general.WIDTH / 800)

        self.progress_bar_x = (
                CONFIG.general.HEIGHT - 25 * CONFIG.general.WIDTH / 800)

        # a countdown to the game end
        self.ending = 5.0
Exemplo n.º 2
0
class Game(object):
    """
    The game base object, initiate and update everything when in game (not in
    menu).
    """
    def __init__(self,
                 screen,
                 level="biglevel",
                 players_=(None, None, None, None)):
        """
        Initialize a game with a list of player and a level,
        level is the basename of the level in levels/
        """

        self.size = (CONFIG.general.WIDTH, CONFIG.general.HEIGHT)

        self.first_frame = True
        self.notif = []
        self.type = 'local'
        self.screen = screen

        self.items = []
        self.events = EventManager()
        self.gametime = 0

        #we load the bool for smooth scrolling here, for a better performance
        self.smooth_scrolling = CONFIG.general.SMOOTH_SCROLLING

        self.level = Level(level)
        if screen is not None:
            self.zoom = 1
            # loading level
            self.level_place = [0, 0]
            self.icon_space = self.size[0] / len(players_)

            # loading players

        self.load_players(players_)

        #the optional progress bar for the players lives
        self.progress_bar_size = (82.5 * CONFIG.general.WIDTH / 800,
                                  12.5 * CONFIG.general.WIDTH / 800)

        self.progress_bar_x = (CONFIG.general.HEIGHT -
                               25 * CONFIG.general.WIDTH / 800)

        # a countdown to the game end
        self.ending = 5.0

    def add_world_event(self):
        '''
        Insert level events into the game, those are configured in level.xml of
        each levels, level-specific events can be defined in level_events.py in
        the level directory.
        '''

        for e in self.level.get_events():
            e[2].update({'world': self})
            self.events.add_event(e[0], e[1], e[2])

    def load_player(self, i, player):
        logging.debug('player ' + str(i) + ' loaded')
        #logging.debug(player)
        if player.split(os.sep)[1][:2] == 'AI':
            try:
                ai = int(player[player.index('AI') + 2])
                player = player.replace('AI' + str(ai), '')
            except ValueError:
                # support old syntax with no number
                ai = 5
                player = player.replace('AI', '')

        else:
            ai = False

        self.players.append(
            Entity(num=i + 1,
                   game=self,
                   entity_skinname=player,
                   place=((i + 1) * self.size[0] / 5, 100)))

        if ai:
            self.players[len(self.players) - 1].ai = ai

    def load_players(self, players_):
        """
        this function is responsible of adding the requested players to the
        game.
        """

        logging.debug('loading players')
        self.players = []
        for i, player in enumerate(players_):
            self.load_player(i, player)

    def add_item(self,
                 item='heal',
                 place=(550, 50),
                 reverse=False,
                 upgraded=False,
                 vector=(0, 0),
                 bullet=False,
                 physics=True,
                 animation='static'):
        """
        Insert an item into game.

        """
        try:
            os.listdir(os.path.join(CONFIG.system_path, 'items', item))
            e = Entity(num=None,
                       game=self,
                       entity_skinname=os.path.join('items', item),
                       place=place,
                       vector=vector,
                       reverse=reverse,
                       visible=True,
                       present=True,
                       upgraded=upgraded,
                       gravity=not bullet,
                       physic=not bullet,
                       animation=animation,
                       physics=physics)

            e.entity_skin.change_animation(animation, self, {'entity': e})

            self.items.append(e)

        except OSError, e:
            if e.errno is 22:
                logging.debug(item + ' is not a valid item.')
            else:
                raise
        except IOError, e:
            if e.errno is 2:
                logging.debug(item + ' is not a valid item directory.')
                raise
Exemplo n.º 3
0
class Game(object):
    """
    The game base object, initiate and update everything when in game (not in
    menu).
    """

    def __init__(self, screen, level="biglevel", players_=(None, None, None,
        None)):
        """
        Initialize a game with a list of player and a level,
        level is the basename of the level in levels/
        """

        self.size = (
            CONFIG.general.WIDTH,
            CONFIG.general.HEIGHT)

        self.first_frame = True
        self.notif = []
        self.type = 'local'
        self.screen = screen

        self.items = []
        self.events = EventManager()
        self.gametime = 0

        # we load the bool for smooth scrolling here, for a better performance
        self.smooth_scrolling = CONFIG.general.SMOOTH_SCROLLING

        self.level = Level(level)
        if screen is not None:
            self.zoom = 1
            # loading level
            self.level_place = [0, 0]
            self.icon_space = self.size[0] / len(players_)

            # loading players

        self.load_players(players_)

        # the optional progress bar for the players' lives
        self.progress_bar_size = (
                82.5 * CONFIG.general.WIDTH / 800,
                12.5 * CONFIG.general.WIDTH / 800)

        self.progress_bar_x = (
                CONFIG.general.HEIGHT - 25 * CONFIG.general.WIDTH / 800)

        # a countdown to the game end
        self.ending = 5.0

    def add_world_event(self):
        '''
        Insert level events into the game, those are configured in level.xml of
        each levels, level-specific events can be defined in level_events.py in
        the level directory.
        '''

        for e in self.level.get_events():
            e[2].update({'world': self})
            self.events.add_event(e[0], e[1], e[2])

    def load_player(self, i, player):
        logging.debug('player ' + str(i) + ' loaded')
            #logging.debug(player)
        if player.split(os.sep)[1][:2] == 'AI':
            try:
                ai = int(player[player.index('AI') + 2])
                player = player.replace('AI' + str(ai), '')
            except ValueError:
                # support old syntax with no number
                ai = 5
                player = player.replace('AI', '')

        else:
            ai = False

        self.players.append(
                Entity(
                    num=i + 1,
                    game=self,
                    entity_skinname=player,
                    place=((i + 1) * self.size[0] / 5, 100)))

        if ai:
            self.players[len(self.players) - 1].ai = ai

    def load_players(self, players_):
        """
        this function is responsible for adding the requested players to the
        game.
        """

        logging.debug('loading players')
        self.players = []
        for i, player in enumerate(players_):
            self.load_player(i, player)

    def add_item(
        self,
        item='heal',
        place=(550, 50),
        reverse=False,
        upgraded=False,
        vector=(0, 0),
        bullet=False,
        physics=True,
        animation='static'):
        """
        Insert an item into game.

        """
        try:
            os.listdir(os.path.join(CONFIG.system_path, 'items', item))
            e = Entity(
                        num=None,
                        game=self,
                        entity_skinname=os.path.join('items', item),
                        place=place,
                        vector=vector,
                        reverse=reverse,
                        visible=True,
                        present=True,
                        upgraded=upgraded,
                        gravity=not bullet,
                        physic=not bullet,
                        animation=animation,
                        physics=physics)

            e.entity_skin.change_animation(animation, self, {'entity': e})

            self.items.append(e)

        except OSError, e:
            if e.errno is 22:
                logging.debug(item + ' is not a valid item.')
            else:
                raise
        except IOError, e:
            if e.errno is 2:
                logging.debug(item + ' is not a valid item directory.')
                raise