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


Python Gui.paint方法代码示例

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


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

示例1: __init__

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

#.........这里部分代码省略.........
        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)

            if self.won is None and not self.players_turn:
                hit, ship = self.ai.shoot(self.enemy_board)
                self.log_shot(ship)
                if ship is not None:
                    self.check_game_end()
                    continue
                self.switch_turns()

            for event in pygame.event.get():
                if self.gui.is_active() or self.gui.is_gui_click(event):
                    # pass it on to gui
                    self.gui.handle(event)
                elif event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == KEYDOWN:
                    # TODO: keyboard shortcuts & navigation for menu
                    if event.key == K_ESCAPE:
                        sys.exit()
                    elif event.key in [K_UP, K_DOWN, K_RIGHT, K_LEFT]:
                        self.crosshair.move(event.key)
                    elif event.key == K_RETURN and self.won is None:
                        hit, ship = self.my_board.shoot(self.crosshair.position)
                        if hit is None:
                            break
                        self.log_shot(ship)
                        self.check_game_end()
                        self.switch_turns(hit)
                    elif event.key == K_SPACE:
                        self.my_board.uncover_all()
                        self.check_game_end()
                elif event.type == MOUSEBUTTONDOWN and event.button == 1 and self.won is None:  # left click
                    hit, ship = self.my_board.uncoverPixels(event.pos)
                    if hit is None:
                        break
                    self.log_shot(ship)
                    self.check_game_end()
                    self.switch_turns(hit)
                elif event.type == MOUSEMOTION:
                    self.crosshair.moveTo(event.pos)

                coords = self.font.render(self.crosshair.coords(), 1, TEXT_COLOR)

            # Redraw the background
            self.screen.fill(BG_COLOR)
            self.my_board.display()
            self.enemy_board.display()
            self.crosshair.display()
            self.show_remaining_ships()
            self.show_coords(coords)

            self.gui.paint()
            pygame.display.flip()
开发者ID:janwilamowski,项目名称:battleship,代码行数:104,代码来源:battleship.py


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