Ejemplo n.º 1
0
    def __init__(self, level=None):
        # Set the games objects
        self.clock = pygame.time.Clock()
        self.main_character = Slash(20.,20.,position=Vector3(0.,0.,-1.), orientation=0.)

        self.enemies = []
        self.characters = [self.main_character]
        self.projectiles = []  # projectiles pool
        self.sound_wave = None # sound wave 'pool'
        self.stage = Stage(STAGE_SIZE)
        # Control
        self.print_debug = False
        self.last_enemy_addition = 0
Ejemplo n.º 2
0
class Game:
    def __init__(self, level=None):
        # Set the games objects
        self.clock = pygame.time.Clock()
        self.main_character = Slash(20.,20.,position=Vector3(0.,0.,-1.), orientation=0.)

        self.enemies = []
        self.characters = [self.main_character]
        self.projectiles = []  # projectiles pool
        self.sound_wave = None # sound wave 'pool'
        self.stage = Stage(STAGE_SIZE)
        # Control
        self.print_debug = False
        self.last_enemy_addition = 0

    def set_level(self, level):
        """
        Set the game's level, following this operations:
        1. Get level structure
        2. Set level's obstacles
        3. Load the level's graph
        4. Set level's characters (enemies)
        """
        # 1. Little bit of wired code =p
        from utils.levels import LEVEL
        global LEVEL
        self.level = LEVEL
        # 2.
        self.stage.set_level(LEVEL['obstacles'])
        # 3.
        self.graph = Graph(LEVEL['number_of_nodes'], LEVEL['nodes'], LEVEL['neighbors'])
        self.a_star = AStar(self.graph)
        self.graph.load(self.a_star)
        LEVEL['graph'] = self.graph
        # 4.
        self.random_enemies(LEVEL['enemies'])

    def draw_axes(self):
        # Space axes
        # Axis X
        glBegin(GL_LINES)
        glColor3f(1.0,0.0,0.0)
        glVertex3f(-20000.0,0.0,0.0)
        glVertex3f(20000.0,0.0,0.0)
        glEnd()
        # Axis Y
        glBegin(GL_LINES)
        glColor3f(0.,1.,0.)
        glVertex3f(0.0,-200.0,0.0)
        glVertex3f(0.0,200.0,0.0)
        glEnd()
        # Axis Z
        glBegin(GL_LINES)
        glColor3f(0.,0.,1.)
        glVertex3f(0.0,0.0,-200.0)
        glVertex3f(0.0,0.0,200.0)
        glEnd()

    def behave(self):
        """
        For the main character, catch keyboards interruptions and execute the
        appropiate behavior according to the key stroke catched.
        For AI characters (enemies), gets the current behavior they are doing
        and updates its kinematic and steering data.
        """
        # Slash behavior
        keymap_handler(self) # Maybe i can just pass self.main_character
        self.main_character.behave(game=self)

        # AI characters behavior
        for enemy in self.enemies:
            enemy.behave()
            if not hasattr(enemy, 'state'):
                setattr(enemy, 'state', StateMachine(enemy))
            enemy.state.update(self).execute(self)

    def render(self):
        # Renders sectors and nodes
        if self.print_debug:
            self.render_debug()

        # Renders all the eye candy:
        life_bar(self.main_character, -10.)
        rock_bar(self.main_character, -9.)
        enemy_z_pos = 10.
        for enemy in self.enemies:
            life_bar(enemy, enemy_z_pos)
            enemy_z_pos -= 1
            

        # Renders all game's objects
        self.stage.render()

        try:
            self.main_character.update(self).render()
        except AttributeError, e:
            self.game_over(False)

        if not self.enemies:
            self.game_over(True)
        for enemy in self.enemies:
            try:
                enemy.update(self).render()
            except AttributeError, e:
                pass