本文整理汇总了Python中snake.Snake.length方法的典型用法代码示例。如果您正苦于以下问题:Python Snake.length方法的具体用法?Python Snake.length怎么用?Python Snake.length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类snake.Snake
的用法示例。
在下文中一共展示了Snake.length方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import length [as 别名]
class Game(object):
def __init__(self, screen_height, screen_width):
pygame.init()
self.screen_height = screen_height
self.screen_width = screen_width
self.level_maps = ("./data/level1.map", "./data/level2.map", "./data/level3.map")
self.current_level_map_index = 0
self.pause = False
self.won_grow_rate = 10
height_width = (self.screen_height, self.screen_width)
self.screen = pygame.display.set_mode(height_width)
pygame.display.set_caption("PySnake")
self.score = Score((0, 500))
self.reload_game = False
self.speed = 5
def init_resources(self):
self.level = Level(self.level_maps[self.current_level_map_index])
self.snake = Snake((64, 64))
self.move_direction = Direction.RIGHT
self.food = None
def start(self):
clock = pygame.time.Clock()
game_over = False
won = True
first_load = True
while not game_over:
clock.tick(self.speed)
for event in pygame.event.get():
self.event_handler(event)
if self.reload_game:
break
if not self.pause:
background = self.level.render()
self.snake.render(background)
if self.food == None:
position = self.generate_free_position()
self.food = Food(position, 1)
background.blit(self.food.image, self.food.rect)
self.screen.fill((0, 0, 0))
self.screen.blit(background, (0, 0))
self.screen.blit(self.score.image, self.score.rect)
pygame.display.flip()
if first_load:
first_load = False
self.pause = True
continue
not_collide = self.snake.update(self.move_direction)
if not not_collide or self.level.collide(self.snake.position):
game_over = True
won = False
if self.food.rect.colliderect(self.snake.head.rect):
self.snake.grow(self.food.grow_rate)
self.score.add(100)
self.food = None
if self.snake.length() == self.won_grow_rate:
game_over = True
if self.reload_game:
self.load_game()
elif won and game_over:
self.load_next_level()
elif game_over:
print("Game over")
def event_handler(self, event):
if event.type == QUIT:
sys.exit(0)
elif event.type == KEYDOWN:
key = pygame.key.get_pressed()
new_direction = self.move_direction
if key[pygame.K_LEFT]:
new_direction = Direction.LEFT
elif key[pygame.K_RIGHT]:
new_direction = Direction.RIGHT
elif key[pygame.K_UP]:
new_direction = Direction.UP
elif key[pygame.K_DOWN]:
new_direction = Direction.DOWN
elif key[pygame.K_p]:
self.pause = not self.pause
elif key[pygame.K_s]:
self.pause = True
level = self.current_level_map_index
snake = self.snake.get_snake_coords()
food = self.food.get_food_position()
direction = self.move_direction
SaveGame.save(level, snake, food, direction)
elif key[pygame.K_l]:
self.reload_game = True
#.........这里部分代码省略.........