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


Python Snake.getBody方法代码示例

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


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

示例1: Game

# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import getBody [as 别名]
class Game():

    FOODN = 4
    SNAKEN = 8
    BOARDSIZE = [16, 16]
    STARTINGSIZE = 10
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def __init__(self):
        self.arrayPos = self.__initiatePos()
        self.snake = Snake(self.SNAKEN, self.STARTINGSIZE , [0,11], 'r')
        self.dir = 'r'
        self.food = [1,10]

    def __checkEat(self, headPos):
        """
        Checks for eats.
        Returns True if it ate.
        """
        return bool(headPos.tolist() == self.food)

    def __updateFood(self, board):
        """
        Gives random new coordinates to food from
        the available free spaces in board
        """
        interm = numpy.where(board!=self.SNAKEN, self.arrayPos, board)
        availablePos = numpy.extract(interm!=self.SNAKEN, interm)
        self.food = random.choice(availablePos)
        print("Food now at: " + str(self.food))
        print("Eaten")

    def __prepareBoard(self, eaten):
        """
        Returns a board ready to print
        """
        toprint = numpy.zeros(self.BOARDSIZE, dtype=int)
        bodyPositions = self.snake.getBody()

        #Place snake
        for pos in bodyPositions:
            toprint[pos[0],pos[1]] = self.SNAKEN
        #Replace food
        if eaten:
            self.__updateFood(toprint)
        #Place food
        toprint[self.food[0], self.food[1]] = self.FOODN

        return toprint

    def __initiatePos(self):
        """
        Returns a gameboard-sized array where each
        item contains its position in a python list.
        [[0,0],[0,1]...]
        """
        interm = numpy.empty(self.BOARDSIZE, dtype=object)
        for m in range(interm.shape[0]):
            for n in range(interm.shape[1]):
                interm[m][n] = [m, n]
        
        return interm
        

    def __printBoard(self, board):
        """
        Smoothly prints the game board
        """
        print('')
        
        for line in board:
            lineOut = ''
            for num in line:
                if not num:
                    lineOut += str(num)
                if num==self.FOODN:
                    lineOut += self.OKGREEN + str(num) + self.ENDC
                elif num==self.SNAKEN:
                    lineOut += self.OKBLUE + str(num) + self.ENDC
                lineOut += ' '
            print(lineOut)

        print('')

    def move(self):
        """
        Performs a whole move
        """
        #Update moving direction
        self.snake.changeDir(self.dir)
        #Check for eats in the upcoming move
        futHeadPos = self.snake.getBody()[0] + self.snake.getDir()
        eaten = self.__checkEat(futHeadPos)
        #Move the snake
        self.snake.move(eaten)
#.........这里部分代码省略.........
开发者ID:javigallostra,项目名称:snake,代码行数:103,代码来源:game.py


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