def test_up(): mapping = {"right": "up", "left": "up", "up": "up", "down": "down"} for key in DIRECTIONS: snake = Snake() snake.direction = key snake.up() assert snake.direction == mapping[key]
def play(self): global rows snake = Snake(config) apple = Apple(config) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # Detect collision with apple if snake.x == apple.x and snake.y == apple.y: snake.eat() apple = Apple(config) # collision with body for part in snake.body: if snake.x == part[0] and snake.y == part[1]: snake = Snake(config) direction = snake.vision(apple) snake.pre_direction = snake.direction snake.decision(direction) # collision with wall if snake.collision_with_wall(snake.direction): direction = (snake.direction + 1) % 4 if snake.collision_with_wall(direction): direction = (snake.direction - 1) % 4 if snake.collision_with_wall(direction): snake = Snake(config) snake.direction = direction snake.move() self.display.fill(self.color) pygame.draw.rect(self.display, Color.black, ((0, 0), (config.game_w, config.game_h)), config.wall_offset) apple.draw(self.display) snake.draw(self.display) score = self.font.render(f'Score: {snake.score}', True, Color.black) score_rect = score.get_rect(center=(config.game_w / 2, config.game_h - 10)) self.display.blit(score, score_rect) pygame.display.update() clock.tick(config.fps)
def play(self): global snake, apple snake = Snake(config) apple = Apple(config) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # Detect collision with apple if snake.x == apple.x and snake.y == apple.y: snake.eat() apple = Apple(config) snake.x_change_old, snake.y_change_old = snake.x, snake.y with torch.no_grad(): data = get_data() data = data.reshape(1, 20) data = torch.tensor(data) result = model(data) snake.direction = np.argmax(result) snake.move() self.display.fill(self.color) pygame.draw.rect(self.display, Color.black, ((0, 0), (config.game_w, config.game_h)), 10) apple.draw(self.display) snake.draw(self.display) if snake.x < 0 or snake.y < 0 or snake.x > config.game_w or snake.y > config.game_h: self.play() score = self.font.render(f'Score: {snake.score}', True, Color.black) score_rect = score.get_rect(center=(config.game_w / 2, config.game_h - 10)) self.display.blit(score, score_rect) pygame.display.update() clock.tick(30) # fps
# Detect collision with apple if snake.x == apple.x and snake.y == apple.y: snake.eat() apple = Apple(config) # collision with body for part in snake.body: if snake.x == part[0] and snake.y == part[1]: snake = Snake(config) direction = snake.vision(apple) snake.pre_direction = snake.direction snake.decision(direction) if snake.collision_with_wall(snake.direction): direction = (snake.direction + 1) % 4 if snake.collision_with_wall(direction): direction = (snake.direction - 1) % 4 if snake.collision_with_wall(direction): snake = Snake(config) snake.direction = direction if add_data(): rows += 1 pbar.update((1 / args.count) * 100) snake.move() f.close()