本文整理汇总了Python中snake.Snake.load_snake_from_coord方法的典型用法代码示例。如果您正苦于以下问题:Python Snake.load_snake_from_coord方法的具体用法?Python Snake.load_snake_from_coord怎么用?Python Snake.load_snake_from_coord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类snake.Snake
的用法示例。
在下文中一共展示了Snake.load_snake_from_coord方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import load_snake_from_coord [as 别名]
#.........这里部分代码省略.........
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
oposide_direction = self.opposite_direction(self.move_direction)
if not oposide_direction == new_direction:
self.move_direction = new_direction
def opposite_direction(self, direction):
if direction == Direction.UP:
return Direction.DOWN
elif direction == Direction.DOWN:
return Direction.UP
elif direction == Direction.LEFT:
return Direction.RIGHT
elif direction == Direction.RIGHT:
return Direction.LEFT
def generate_free_position(self):
x, y = None, None
while x is None or self.level.collide((x, y)) or self.snake.collide((x, y)):
x = random.randrange(0, self.level.width - 32, 2)
y = random.randrange(0, self.level.height - 32, 2)
return (x, y)
def load_next_level(self):
if len(self.level_maps) - 1 == self.current_level_map_index:
print("Game over, you win")
return
self.current_level_map_index += 1
self.init_resources()
self.start()
def get_current_level_map(self):
return self.level_maps[self.current_level_map_index]
def load_game(self):
loaded_dict = LoadGame.load()
self.current_level_map_index = loaded_dict["level"]
self.init_resources()
self.snake.load_snake_from_coord(loaded_dict["snake"])
self.food = Food(loaded_dict["food"], 1)
self.move_direction = loaded_dict["direction"]
self.reload_game = False
self.start()