本文整理汇总了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")