本文整理匯總了Python中snake.Snake.get_head方法的典型用法代碼示例。如果您正苦於以下問題:Python Snake.get_head方法的具體用法?Python Snake.get_head怎麽用?Python Snake.get_head使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類snake.Snake
的用法示例。
在下文中一共展示了Snake.get_head方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Engine
# 需要導入模塊: from snake import Snake [as 別名]
# 或者: from snake.Snake import get_head [as 別名]
class Engine(object):
def __init__(self, world_size=WORLD_SIZE):
self.world_center = Point((world_size // 2, world_size // 2))
self.world_size = world_size
self.snake = Snake(start=self.world_center, start_length=SNAKE_START_LENGTH, growth_pending = GROWTH_PENDING)
self.level = Level(size=self.world_size, snake=self.snake)
self.score = 0
self.controller = Controller(self.level.level_render)
def reset(self):
"""Start a new game."""
self.playing = True
self.score = 0
self.snake = Snake(start=self.world_center,
start_length=SNAKE_START_LENGTH)
self.level = Level(size=self.world_size, snake=self.snake)
self.play()
def update(self, dt):
"""Update the game by dt seconds."""
self.check_input()
# time.sleep(dt)
if self.snake.update():
self.level.update_level()
self.level.level_render.draw_text(
Point((0, 0)), 'Score {}'.format(self.score))
self.level.show_level()
head = self.snake.get_head()
# If snake hits a food block, then consume the food, add new
# food and grow the snake.
if head in self.level.food:
self.eat(head)
if self.snake.self_intersecting():
raise GameOver('snake intersecting')
if head in self.level.blocks:
raise GameOver('snake try eat block')
time.sleep(dt)
def eat(self, head=None):
print('mmm, tasty')
self.level.food.remove(head)
self.snake.grow()
self.score += len(self.snake) * SEGMENT_SCORE
def play(self):
"""Play game until the QUIT event is received."""
while True:
try:
self.update(TIME_DELTA)
except GameOver, err:
print(str(err))
print('You score {}'.format(self.score))
time.sleep(3)
self.reset()