本文整理匯總了Python中snake.Snake.checkCollision方法的典型用法代碼示例。如果您正苦於以下問題:Python Snake.checkCollision方法的具體用法?Python Snake.checkCollision怎麽用?Python Snake.checkCollision使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類snake.Snake
的用法示例。
在下文中一共展示了Snake.checkCollision方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Game
# 需要導入模塊: from snake import Snake [as 別名]
# 或者: from snake.Snake import checkCollision [as 別名]
#.........這裏部分代碼省略.........
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)
#Check for deads
if self.snake.checkCollision(self.BOARDSIZE):
print("PROBLEM")
return False
#Or print
else:
board = self.__prepareBoard(eaten)
self.__printBoard(board)
return True
def moveTestNTimes(self, times):
for i in range(times):
a= str(input())
if a == 'e':
break
self.dir = a
if not self.move():
break
print("\n\n\n TEST ENDED")