当前位置: 首页>>代码示例>>Python>>正文


Python Console.render方法代码示例

本文整理汇总了Python中console.Console.render方法的典型用法代码示例。如果您正苦于以下问题:Python Console.render方法的具体用法?Python Console.render怎么用?Python Console.render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在console.Console的用法示例。


在下文中一共展示了Console.render方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import render [as 别名]
class DeityRL:
    def __init__(self):
        self._game_config = GameConfig
        self._config = Config()
        self._worldmap = None
        self._output = Console(self._game_config.display)
        self._input = self._output
        self._player = Player(self.let_player_act, self.render)

        self._message_buffer = []

        self._closed = False

    def has_saved_game(self):
        return False

    def load_game(self):
        raise Exception("no")

    def new_game(self):
        self._player.xy(int(self._game_config.world_width / 2), int(self._game_config.world_height / 2))
        maker = MapMaker()
        self._worldmap = maker.generate(self._game_config.world_width, self._game_config.world_height)
        self._worldmap.set_message(self.add_message)

        self._worldmap.insert_objects(self._player)
        self._tick = 0

    def run(self):
        while not self._closed:
            self.render()
            self.tick()

    def render(self):
        self._worldmap.restack()
        map_view = self._worldmap.get_view(
            self._player.x, self._player.y, self._output.get_view_width(), self._output.get_view_height()
        )
        display_data = AttrDict(
            {
                "view": "main",
                "tick": self._tick,
                "xy": "{}, {}".format(self._player.x, self._player.y),
                "player_health": "{}/{}".format(self._player.max_health, self._player.health),
                "player_energy": self._player._energy,
                "player_inventory": [i.name for i in self._player._inventory],
                "map_view": map_view,
                "messages": self._message_buffer[-8:],
            }
        )
        self._output.render(display_data)

    def let_player_act(self):
        key = self._input.get_key()
        self.handle_key(key)

    def shutdown(self):
        self._console.teardown()

    def tick(self):
        self._worldmap.tick()
        self._tick += 1

    def handle_key(self, key):
        if key in ("q", "Q"):
            self._closed = True
            self._player._energy = -1
            return True
        elif key in self._config.keybindings:
            return self.act_on_bound_key(key)
        else:
            self.add_message("Unbound key '{}'".format(key))
            return False

    def act_on_bound_key(self, key):
        binding = self._config.keybindings[key]
        if binding in (
            Bind.MOVE_N,
            Bind.MOVE_NE,
            Bind.MOVE_E,
            Bind.MOVE_SE,
            Bind.MOVE_S,
            Bind.MOVE_SW,
            Bind.MOVE_W,
            Bind.MOVE_NW,
        ):
            return self.object_act_direction(self._player, AttrDict({"x": binding[0], "y": binding[1]}))
        elif binding == Bind.PICKUP:
            return self._worldmap.grab_object(self._player)
        elif binding == Bind.WAIT:
            self._player._energy -= self._player._speed  # wait 1 tick

    def object_act_direction(self, obj, movement):
        if self._worldmap.can_move_object(obj, movement):
            obj._energy -= self._worldmap.move_object(obj, movement)
        else:
            obj._energy -= self._worldmap.object_act_in_direction(obj, movement)

    def add_message(self, message):
        if isinstance(message, list):
#.........这里部分代码省略.........
开发者ID:RWJMurphy,项目名称:roguelike,代码行数:103,代码来源:deityrl.py

示例2: __init__

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import render [as 别名]
class Application:
    entitylist = [[],[],[]] # 2D array - [0] er bak, [1] er player, [2] er forran

    def __init__(self):
        self.width = getWidth()
        self.height = getHeight()

        self.fps = getFPS()
        self.bubbleCount = int(getWidth() / 160)
        self.drawBubbles = True
        self.drawBG = True
        
        pygame.init()
        self.fpsClock = pygame.time.Clock()
        if fullscreen():
            self.windowSurfaceObj = pygame.display.set_mode((self.width, self.height), pygame.FULLSCREEN)
        else:
            self.windowSurfaceObj = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption('Anim')
        
        pygame.mouse.set_visible(cursor())
        
        for i in range(0, self.bubbleCount):
            self.entitylist[2].append(Bubble(True))
            self.entitylist[0].append(Bubble(False))
        
        self.player = Player()
        self.entitylist[1].append(self.player)
        self.background = Background()
        
        print('Init...')
        self.console = Console(getWidth(), getHeight())
        Console.host = self
        self.DrawPosVector = False

    def event(self):
        self.events = pygame.event.get()
        for event in self.events:
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
                
            if event.type == KEYUP:
                if event.key == K_ESCAPE:
                    print('Exiting...')
                    self.exit()
            if event.type == KEYDOWN:
                if event.key == K_F12:
                    self.console.toggle()

                elif self.console.isActive():
                    if event.key == K_RETURN:
                        self.console.execute()
                        break
                    elif event.key == K_BACKSPACE:
                        self.console.setCurrentLine(self.console.getCurrentLine()[:-1])
                        break
                    self.console.appendCurrentLine(event.unicode)
                    return
            
        self.background.dirVector.setX(-(self.player.getXSpeed() / 8))
        self.background.event(self.events)

        for lst in self.entitylist:
            for e in lst:
                if isinstance(e, Bubble):
                    e.setXSpeedFromPlayer(-self.player.getXSpeed())
                e.event(self.events)
            
    def render(self):
        #Background
        if self.drawBG: self.background.render(self.windowSurfaceObj)
        for lst in self.entitylist:
            for e in lst:
                e.render(self.windowSurfaceObj, self.DrawPosVector)
        #Konsollen
        self.console.render(self.windowSurfaceObj)
        pygame.display.update()
        self.fpsClock.tick(getFPS())
        

    def exit(self):
        pygame.event.post(pygame.event.Event(QUIT))

    def fullscreen(self, input):
        if input:
            self.windowSurfaceObj = pygame.display.set_mode((self.width, self.height), pygame.FULLSCREEN)
        else:
            self.windowSurfaceObj = pygame.display.set_mode((self.width, self.height))
开发者ID:sondrehav,项目名称:onABoat,代码行数:91,代码来源:application.py


注:本文中的console.Console.render方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。