Exemplo n.º 1
0
    def __init__(self):
        ''' Initialize starting state'''
        self.graphics = Graphics()
        self.log = logging.getLogger("Game");
        logging.basicConfig(filename='main.log', level=DEBUG_LEVEL)
        self.log_handler = CursesHandler(self.graphics.windows["log"])
        self.log_handler.setLevel(DEBUG_LEVEL)
        self.log.addHandler(self.log_handler)
        self.commands = Commands(self)
        self.cursor_pos = [0,0]
        self.game_mode = GameModes.normal
        self.world = World(log_handler=self.log_handler)

        try:
            # Try to load a level from a text file and add it to the world
            # self.world.add_level(self.world.read_level('../data/testworld'))
            # self.world.get_current_level().add_entity(entity.creatures.SpaceOrk(5,5))
            self.world.add_level(
                    self.world.generate_level(
                        self.graphics.max_x, self.graphics.world_height))

            #Place player at spawn
            self.player = entity.creatures.Player(1,1)
            for ent in self.world.get_current_level().entities:
                if ent.__class__ == entity.world.PlayerSpawn:
                    self.player = entity.creatures.Player(ent.position[0],ent.position[1])

            self.world.get_current_level().add_entity(self.player)
        except Exception:
            # use better exception
            self.exit(1)

        self.log.debug("Game started")
Exemplo n.º 2
0
class Game(object):
    ''' Handles a game instance '''

    def __init__(self):
        ''' Initialize starting state'''
        self.graphics = Graphics()
        self.log = logging.getLogger("Game");
        logging.basicConfig(filename='main.log', level=DEBUG_LEVEL)
        self.log_handler = CursesHandler(self.graphics.windows["log"])
        self.log_handler.setLevel(DEBUG_LEVEL)
        self.log.addHandler(self.log_handler)
        self.commands = Commands(self)
        self.cursor_pos = [0,0]
        self.game_mode = GameModes.normal
        self.world = World(log_handler=self.log_handler)

        try:
            # Try to load a level from a text file and add it to the world
            # self.world.add_level(self.world.read_level('../data/testworld'))
            # self.world.get_current_level().add_entity(entity.creatures.SpaceOrk(5,5))
            self.world.add_level(
                    self.world.generate_level(
                        self.graphics.max_x, self.graphics.world_height))

            #Place player at spawn
            self.player = entity.creatures.Player(1,1)
            for ent in self.world.get_current_level().entities:
                if ent.__class__ == entity.world.PlayerSpawn:
                    self.player = entity.creatures.Player(ent.position[0],ent.position[1])

            self.world.get_current_level().add_entity(self.player)
        except Exception:
            # use better exception
            self.exit(1)

        self.log.debug("Game started")

    def run(self):
        ''' Draw the world and get input '''
        try:
            self.graphics.draw_world(self.world)
            self.graphics.show_stats(self.player)
            self.graphics.update()
            while True:
                self.parse_command(self.graphics.get_input())
                if self.game_mode == GameModes.cursor or self.game_mode == GameModes.look:
                    self.graphics.clear()
                    self.graphics.draw_world(self.world)
                    self.graphics.draw_cursor(self.cursor_pos)
                if self.game_mode == GameModes.look:
                    for ent in self.world.get_current_tile(self.cursor_pos):
                        self.log.info(ent.name)
                self.graphics.show_stats(self.player)
                self.graphics.update()
        except (Exception, KeyboardInterrupt):
            self.exit(1)

    def turn(self):
        ''' Take a turn; redraw world, run ai... '''
        try:
            for ent in self.world.get_current_level().entities:
                if hasattr(ent, "die"):
                    if ent.die():
                        continue
                if issubclass(ent.__class__, entity.base.Creature):
                    ent.turn(self.player)
            self.graphics.clear()
            self.graphics.draw_world(self.world)
        except (Exception, KeyboardInterrupt):
            self.exit(1)



    def exit(self, status):
        ''' Shutdown curses, close files, and print errors if any'''
        self.log.debug("exiting")
        self.graphics.exit()
        if sys.exc_info()[2] is not None:
            print(traceback.format_exc())
        sys.exit(status)


    def parse_command(self, command):
        ''' Executes the function associated with a command string '''
        self.log.debug(command)
        if hasattr(self.commands, command):
            try:
                self.commands.__class__.__dict__[command](self.commands)
            except (Exception, KeyboardInterrupt):
                self.exit(1)
        else:
            self.log.error("Command not found")