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


Python Gui.clear_log方法代码示例

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


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

示例1: __init__

# 需要导入模块: from gui import Gui [as 别名]
# 或者: from gui.Gui import clear_log [as 别名]
class Game:

    def __init__(self):
        pygame.init()
        logo = pygame.image.load(os.path.join(BASE_DIR, 'logo.png'))
        pygame.display.set_icon(logo)
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
        pygame.display.set_caption("BATTLESHIPS!")
        self.clock = pygame.time.Clock()
        self.ai = AI()
        menu_font = pygame.font.Font('DejaVuSans.ttf', 16) # default font can't display check mark
        self.gui = Gui(self.init, self.set_ai, menu_font)
        self.gui.update_ai_menu(self.ai.strength)
        self.font = pygame.font.Font(None, 36)

        menu_offset = (0, self.gui.menus.rect.h)
        self.my_board = Board(BOARD_WIDTH, BOARD_HEIGHT, self.screen, menu_offset)
        self.crosshair = Crosshair(self.my_board, (0, 0), menu_offset)
        self.enemy_board = Board(BOARD_WIDTH, BOARD_HEIGHT, self.screen, (550, 25))

        self.textpos = pygame.Rect(10, 545, 50, 30)
        self.ship_images = {}
        for size in range(1, 6):
            image = load_image('ship{size}.bmp'.format(size=size), 'gfx')
            self.ship_images[size] = image

    def init(self, log):
        shipCountBySize = {5: 1, 4: 1, 3: 2, 2: 2, 1: 3}
        self.won = None
        self.players_turn = True
        self.my_board.reset_fields()
        self.enemy_board.reset_fields()
        self.my_ships = place_ships(self.enemy_board, shipCountBySize)
        self.enemy_ships = place_ships(self.my_board, shipCountBySize, False)
        self.remaining_ships = shipCountBySize
        if log:
            self.gui.clear_log()
            self.gui.log("Welcome to Battleships!")

    def set_ai(self, level):
        self.ai.strength = level
        self.gui.log('AI level set to {l}'.format(l=level.name))
        self.gui.update_ai_menu(level)

    def switch_turns(self, value=None):
        if value is None:
            self.players_turn = not self.players_turn
        else:
            self.players_turn = value

    def log_shot(self, ship):
        who = 'You have' if self.players_turn else 'Your opponent has'
        color = MY_COLOR if self.players_turn else ENEMY_COLOR
        if ship is None:
            self.gui.log('{who} missed'.format(who=who), color)
            return

        action = 'sunk' if ship.discovered else 'hit'
        whose = 'your' if ship.is_mine else 'the enemy'
        self.gui.log('{who} {action} {whose} ship!'.format(who=who, action=action, whose=whose), color)

        if ship.discovered and not ship.is_mine:
            self.remaining_ships[ship.size] -= 1

    def check_game_end(self):
        if self.won is not None: return

        if all(ship.discovered for ship in self.my_ships):
            self.won = False
            self.gui.log('YOU LOST!', ENEMY_COLOR)
        elif all(ship.discovered for ship in self.enemy_ships):
            self.won = True
            self.gui.log('YOU WON!', MY_COLOR)
        # TODO: popup message?

    def show_remaining_ships(self):
        offset = 60
        for size, count in self.remaining_ships.items():
            text_pos = pygame.Rect(offset, 545, 50, 30)
            amount = self.font.render('{c}x'.format(c=count), 1, TEXT_COLOR)
            self.screen.blit(amount, text_pos)
            offset += 30

            ship_size = size * FIELD_SIZE
            ship_pos = pygame.Rect(offset, 535, ship_size, FIELD_SIZE)
            image = self.ship_images[size]
            self.screen.blit(image, ship_pos)
            offset += ship_size + 20

    def show_coords(self, coords):
        self.screen.blit(coords, self.textpos)

    def run(self):
        self.init(False)

        # The main game loop
        while True:
            # Limit frame speed to 50 FPS
            time_passed = self.clock.tick(50)

#.........这里部分代码省略.........
开发者ID:janwilamowski,项目名称:battleship,代码行数:103,代码来源:battleship.py


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