當前位置: 首頁>>代碼示例>>Python>>正文


Python Snake.changeDir方法代碼示例

本文整理匯總了Python中snake.Snake.changeDir方法的典型用法代碼示例。如果您正苦於以下問題:Python Snake.changeDir方法的具體用法?Python Snake.changeDir怎麽用?Python Snake.changeDir使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在snake.Snake的用法示例。


在下文中一共展示了Snake.changeDir方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Game

# 需要導入模塊: from snake import Snake [as 別名]
# 或者: from snake.Snake import changeDir [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.changeDir方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。