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


Python AI.set_prev_was_hit方法代码示例

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


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

示例1: __init__

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import set_prev_was_hit [as 别名]

#.........这里部分代码省略.........
        # if enhancements are enabled initialize and setup variables for the GUI display
        if enhancements:
            pygame.init()
            self._screen1 = pygame.display.set_mode(((size * 22) + 1,(size * 11) + 50))
            self._screen1.fill((0,0,0))
            self._font = pygame.font.Font(None, 30)
            self._ship_font = pygame.font.Font(None, 20)
        
    def start(self):
        # continue playing the game as long as someone has not lost
        turn_count = 0
        draw_ships = False
        while(not self._player1.is_defeated() and not self._player2.is_defeated()):
            # if enhancements are enabled generate the images to display for the GUI and display them
            if enhancements:
                for event in pygame.event.get(pygame.QUIT):
                    pygame.quit()
                    sys.exit()
                self._screen1.fill((0,0,0))
                image, imagerect = self._player1.get_board().draw_board(draw_ships)
                imagerect.top += 50
                image1, imagerect1 = self._player2.get_board().draw_board(draw_ships)
                imagerect1.top += 50
                imagerect1.left = imagerect.width + 1
                self._screen1.blit(image, imagerect)
                self._screen1.blit(image1, imagerect1)
                # create the 'scoreboard' for the GUI with the player name highlighted for
                # whoever's turn it currently is
                if turn_count % 2 == 0:
                    self._font.set_bold(True)
                else:
                    self._font.set_bold(False)
                font_image = self._font.render("Player 1", False, (255,255,255))
                self._screen1.blit(font_image, pygame.Rect(0,0,self._font.size("Player 1")[0], self._font.size("Player 1")[1]))
                ship_image = self._ship_font.render("ships left = " + str(self._player1.get_ships_left()), False, (255,255,255))
                self._screen1.blit(ship_image, pygame.Rect(0,30,self._ship_font.size("ships left = " + str(self._player1.get_ships_left()))[0], self._ship_font.size("ships left = " + str(self._player1.get_ships_left()))[1]))
                if turn_count % 2 != 0:
                    self._font.set_bold(True)
                else:
                    self._font.set_bold(False)
                font_image = self._font.render("Player 2", False, (255,255,255))
                self._screen1.blit(font_image, pygame.Rect(self._screen1.get_width() - self._font.size("Player 2")[0],0,self._font.size("Player 2")[0], self._font.size("Player 2")[1]))
                ship_image = self._ship_font.render("ships left = " + str(self._player2.get_ships_left()), False, (255,255,255))
                self._screen1.blit(ship_image, pygame.Rect(self._screen1.get_width() - self._ship_font.size("ships left = " + str(self._player2.get_ships_left()))[0],30,self._ship_font.size("ships left = " + str(self._player2.get_ships_left()))[0], self._ship_font.size("ships left = " + str(self._player2.get_ships_left()))[1]))
                pygame.display.update()
            # get player 1 or player 2 move and check for hit and update board
            if turn_count % 2 == 0:
                if not enhancements:
                    print self._player1.get_board()
                value = self._player2.is_hit(Point(0, self._player1.get_move()))
                if value:
                    self._player1.set_prev_was_hit(True)
                elif self._player1.get_prev_was_hit() and not value:
                    self._player1.next_direction()
            else:
                if not enhancements:
                    print self._player2.get_board()
                value = self._player1.is_hit(Point(0, self._player2.get_move()))
                if value:
                    self._player2.set_prev_was_hit(True)
                elif self._player2.get_prev_was_hit() and not value:
                    self._player2.next_direction()
            # change players turn
            turn_count += 1
        # if enhancements are enabled display the final display after a player has won
        if enhancements:
            self._screen1.fill((0,0,0))
            self._font.set_bold(False)
            font_image = self._font.render("Player 1", False, (255,255,255))
            self._screen1.blit(font_image, pygame.Rect(0,0,self._font.size("Player 1")[0], self._font.size("Player 1")[1]))
            font_image = self._font.render("Player 2", False, (255,255,255))
            self._screen1.blit(font_image, pygame.Rect(self._screen1.get_width() - self._font.size("Player 2")[0],0,self._font.size("Player 2")[0], self._font.size("Player 2")[1]))
        # display who was the winner of the game
        if self._player1.is_defeated():
            if not enhancements:
                print "Player 2 Wins."
                print self._player1.get_board()
            else:
                end_image = self._ship_font.render("Player 2 Wins.", False, (255,255,255))
                self._screen1.blit(end_image, pygame.Rect(0,30, self._ship_font.size("Player 2 Wins.")[0], self._ship_font.size("Player 2 Wins.")[1]))
        elif self._player2.is_defeated():
            if not enhancements:
                print "Player 1 Wins."
                print self._player2.get_board()
            else:
                end_image = self._ship_font.render("Player 1 Wins.", False, (255,255,255))
                self._screen1.blit(end_image, pygame.Rect(0,30, self._ship_font.size("Player 1 Wins.")[0], self._ship_font.size("Player 1 Wins.")[1]))
        # if enhancements are enabled display the actual boards of the players after one has won
        if enhancements:
            image, imagerect = self._player1.get_board().draw_board(draw_ships)
            imagerect.top += 50
            image1, imagerect1 = self._player2.get_board().draw_board(draw_ships)
            imagerect1.top += 50
            imagerect1.left = imagerect.width + 1
            self._screen1.blit(image, imagerect)
            self._screen1.blit(image1, imagerect1)        
            pygame.display.update()
            # wait for a mouse click to allow the users to see the outcome of the game
            while len(pygame.event.get(pygame.MOUSEBUTTONUP)) == 0:
                continue
开发者ID:cprimera,项目名称:projects,代码行数:104,代码来源:game.py


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