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


Python UI.get_input方法代码示例

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


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

示例1: ask

# 需要导入模块: from ui import UI [as 别名]
# 或者: from ui.UI import get_input [as 别名]
    def ask(self):
        self.prompt_user()
        UI.display('Loading ..')
        self.add_more_questions()

        ques = None
        score = 0

        while self.question_count > 0:
            ques = self.manager.get_question(self.current_question)
            self.current_question += 1

            UI.display(ques.question)
            answer = UI.get_input('Answer: ')

            try:
                if ques.answer == int(answer):
                    UI.display('Correct!\n')
                    score += 2
                else:
                    UI.display('Wrong!\n')
                    break

            except ValueError:
                UI.display('Please enter a valid answer.')
                self.current_question -= 1
                continue

            if self.question_count == 1:
                self.add_more_questions()

            self.question_count -= 1

        self.manager.set_score(self.user, score)
开发者ID:betrakiss,项目名称:HackBulgaria,代码行数:36,代码来源:game.py

示例2: start

# 需要导入模块: from ui import UI [as 别名]
# 或者: from ui.UI import get_input [as 别名]
 def start(self):
     while True:
         command = UI.get_input("> ")
         if command == 'start':
             self.ask()
         elif command == 'highscores':
             UI.display(self.show_highscores())
         elif command == 'help':
             UI.display(self.__HELP_MESSAGE)
         elif command == 'exit':
             return
开发者ID:betrakiss,项目名称:HackBulgaria,代码行数:13,代码来源:game.py

示例3: prompt_user

# 需要导入模块: from ui import UI [as 别名]
# 或者: from ui.UI import get_input [as 别名]
 def prompt_user(self):
     self.user = UI.get_input('Enter Username: ')
     self.manager.add_user(self.user)
开发者ID:betrakiss,项目名称:HackBulgaria,代码行数:5,代码来源:game.py

示例4: __init__

# 需要导入模块: from ui import UI [as 别名]
# 或者: from ui.UI import get_input [as 别名]
class Game:
    welcome_message = '\n{} {} {}\nWrite start to play or exit to quit!'.format(
                      '-' * 10, 'Tic Tac Toe!', '-' * 10)

    winner_message = 'Game Over! Winner is {}'

    winning_routes = [[(0, 0), (0, 1), (0, 2)],
                      [(0, 0), (1, 0), (2, 0)],
                      [(0, 0), (1, 1), (2, 2)],
                      [(1, 0), (1, 1), (1, 2)],
                      [(0, 1), (1, 1), (2, 1)],
                      [(0, 2), (1, 2), (2, 2)],
                      [(2, 0), (2, 1), (2, 2)],
                      [(2, 0), (1, 1), (0, 2)]]

    def __init__(self):
        self.UI = UI()

        self.AI = AI(self.UI.BOARD, self.winning_routes)
        self.player = None
        self.game_over = False

    def input_choice(self):
        x = int(self.UI.get_input('x: '))
        y = int(self.UI.get_input('y: '))

        if self.are_x_y_valid(x, y) or self.UI.BOARD[x][y] != ' ':
            raise ValueError

        return (x, y)

    def are_x_y_valid(self, x, y):
        return x >= self.UI.SIZE or y >= self.UI.SIZE or x < 0 or y < 0

    def start(self):
        while self.game_over is False:
            self.UI.print_map()

            self.tick()

    def tick(self):
        try:
            choice = self.input_choice()

        except ValueError:
            self.UI.display('\nInvalid input! Please reenter.\n')
            return

        self.UI.BOARD[choice[0]][choice[1]] = self.UI.X

        if self.is_board_full():
            self.trigger_game_over(None)
            return

        self.AI.attack()

        is_there_a_winner = self.check_for_winner()

        if is_there_a_winner is not False:
            self.trigger_game_over(is_there_a_winner)
            return

    def check_for_winner(self):
        for route in self.winning_routes:
            if all(self.UI.BOARD[coords[0]][coords[1]] == self.UI.X for coords in route):
                return self.UI.X

            if all(self.UI.BOARD[coords[0]][coords[1]] == self.UI.O for coords in route):
                return self.UI.O

        return False

    def is_board_full(self):
        return all(all(block != ' ' for block in line) for line in self.UI.BOARD)

    def trigger_game_over(self, winner):
        self.game_over = True
        self.UI.print_map()
        self.UI.display(self.winner_message.format('Player' if winner == self.UI.X else
                                                   ('Enemy' if winner == self.UI.O else winner)))

    def go(self):
        self.UI.display(self.welcome_message)

        if self.UI.get_input('> ') == 'start':
            self.start()
开发者ID:betrakiss,项目名称:HackBulgaria,代码行数:88,代码来源:game.py


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