class TargetPractice:
    """Overrall class to manage assets and behavior"""
    def __init__(self):
        """intialize the game, and create resources"""
        pygame.init()
        self.settings = Settings()

        #Set the window size and caption
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Target Practice")

        #create an instance to store game statistics
        self.stats = GameStats(self)

        #import the rocket ship and make an instance of it
        self.rocketShip = RocketShip(self)

        #import the orb
        self.orbs = pygame.sprite.Group()

        #import the target
        self.target = Target(self)

        #Create a play button
        self.play_button = Button(self, "Play")

        #misses
        self.miss = 0

    def run_game(self):
        while True:
            #watch for keyboard and mouse events
            self._check_events()
            if self.stats.game_active:
                self.rocketShip.update()
                self._update_target()
                self._update_orbs()
            self._update_screen()

    def _update_screen(self):
        """Update images on the screen and flip to a new screen"""
        #Redraw the screen during each pass through the loop
        self.screen.fill(self.settings.bg_color)

        #draw the rocket ship
        self.rocketShip.blitme()

        #draw the target
        self.target.blitme()

        for orb in self.orbs.sprites():
            orb.draw_orb()

        #draw the play button
        if not self.stats.game_active:
            self.play_button.draw_button()

        #Make the most recently drawn screen visible
        pygame.display.flip()

    def _check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_keydown_events(self, event):
        """Respond to keypress events"""
        if event.key == pygame.K_w:
            #move the rocket up
            self.rocketShip.moving_up = True
        elif event.key == pygame.K_s:
            #move the rocket down
            self.rocketShip.moving_down = True
        elif event.key == pygame.K_SPACE:
            self._fire_orb()

    def _check_keyup_events(self, event):
        if event.key == pygame.K_w:
            self.rocketShip.moving_up = False
        if event.key == pygame.K_s:
            self.rocketShip.moving_down = False

    def _update_target(self):
        """move the target up and down"""
        self.target.update()

    def _fire_orb(self):
        """update the position of the orb"""
        new_orb = Orb(self)
        self.orbs.add(new_orb)

    def _update_orbs(self):
        """update the position of the orbs and get rid of old orbs"""
        #update the orb positions
        self.orbs.update()

        #Get rid of old orbs that have disappeared
        for orb in self.orbs.copy():
            if orb.rect.left >= self.rocketShip.screen_rect.right:
                self.orbs.remove(orb)
                self.miss += 1

        #look for orb-target collision
        if pygame.sprite.spritecollideany(self.target, self.orbs):
            self._target_hit()

        if self.miss >= 3:
            self.stats.game_active = False

    def _target_hit(self):
        #render GOAL! on screen
        self.target.shorten_target()
        self.miss = 0

        #self.stats.game_active = False

    def _check_play_button(self, mouse_pos):
        """Start a new game when the player clicks play"""
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            self._start_game()

    def _start_game(self):
        """start the game"""
        #reset the game statistics
        self.stats.reset_stats()
        self.miss = 0
        self.stats.game_active = True

        #Get rid of any remaining shots
        self.orbs.empty()

        #Center the target and ship
        self.target.center_target()
        self.rocketShip.center_rocketShip
class HitTheTarget:
    """Overall class to manage assets and behavior."""

    def __init__(self):
        """Initialize the game and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Hit the Target")
        # self.screen_rect = self.screen.get_rect()

        # Create an instance to store game statistics.
        self.stats = GameStats(self)

        self.target = Target(self)
        self.gun = Gun(self)
        self.bullets = pygame.sprite.Group()

        # Make the Play button.
        self.play_button = Button(self, "Play")

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()

            if self.stats.game_active:
                self._target_direction()
                self.gun.update()
                self.target.update()
                self._update_bullets()

            self._screen_update()

    def _check_events(self):
        """Check for key presses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_play_button(self, mouse_pos):
        """Start a new game when the player clicks Play."""
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            self._start_game()

    def _check_keydown_events(self, event):
        """Respond for keypresses."""
        if event.key == pygame.K_q:
            exit()
        elif event.key == pygame.K_UP:
            self.gun.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.gun.moving_down = True
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()
        elif event.key == pygame.K_p:
            self._start_game()

    def _check_keyup_events(self, event):
        """Respond for key releases."""
        if event.key == pygame.K_UP:
            self.gun.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.gun.moving_down = False

    def _start_game(self):
        """Start a new game."""
        # Reset the game statistics.
        self.stats.reset_stats()
        self.stats.game_active = True

        # Get rid of any remaining aliens and bullets.
        self.target.start_position()
        self.bullets.empty()

        self.gun.start_position()

        # Hide the mouse cursor.
        pygame.mouse.set_visible(False)

    def _target_direction(self):
        """Change target direction if hit the edges."""
        if self.target.rect.bottom >= self.settings.screen_height \
                or self.target.rect.top <= 0:
            self.settings.target_direction *= -1

    def _fire_bullet(self):
        """Create a new bullet and add it to the bullet group."""
        if self.stats.bullets_left > 0:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)
            self.stats.bullets_left -= 1

    def _screen_update(self):
        self.screen.fill(self.settings.bg_color)
        self.target.blitme()
        self.gun.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

        # Make the play button if the game is inactive.
        if not self.stats.game_active:
            self.play_button.draw_button()

        pygame.display.flip()

    def _update_bullets(self):
        """Update position of bullets and get rid of old bullets."""
        # Update bullets position
        self.bullets.update()

        # Get rid of bullets that have disappeared.
        for bullet in self.bullets.copy():
            if bullet.rect.left > self.settings.screen_width:
                self.bullets.remove(bullet)
        self._check_bullet_target_collisions()

    def _check_bullet_target_collisions(self):
        """Respond to bullet-target collisions."""
        # Remove any bullets and aliens that have collided.
        if pygame.sprite.spritecollideany(self.target, self.bullets):
            self.bullets.empty()
            self.stats.bullets_left = 3
            self.target.start_position()
            self.settings.increase_target_speed()

        if not self.bullets and self.stats.bullets_left == 0:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)
            self.settings.initialize_dynamic_settings()
class TargetPractice:
    """Overall class to manage game assets and behavior."""
    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Target Practice")

        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.target = Target(self)
        self.hitcount = 0

        self.play_button = Button(self, "Play")

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()
            if self.settings.game_active:
                self.ship.update()
                self._update_bullets()
                self._update_target()
            self._update_screen()

    def _check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self, event):
        """Respond to keypresses."""
        if event.key == pygame.K_UP:
            self.ship.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        elif event.key == pygame.K_SPACE and self.settings.game_active:
            self._fire_bullet()
        elif event.key == pygame.K_p and not self.settings.game_active:
            self._start_game()
        elif event.key == pygame.K_q:
            sys.exit()

    def _check_keyup_events(self, event):
        """Respond to to key Releases."""
        if event.key == pygame.K_UP:
            self.ship.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = False

    def _check_play_button(self, mouse_pos):
        """Start a new game when the player clicks Play."""
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.settings.game_active:
            self._start_game()

    def _start_game(self):
        # Hide the mouse cursor.
        pygame.mouse.set_visible(False)
        # Reset the game statistics/
        self.settings.game_active = True

        # Get rid of any remaining bullets & reset hitcount.
        self.bullets.empty()
        # Create a new fleet and center the ship.
        self.target.center_target()
        self.ship.center_ship()
        self.settings.initialize_dynamic_settings()

    def _fire_bullet(self):
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)

    def _update_bullets(self):
        """Position of bullets and get rid of old bullets."""
        # Update bullet positions.
        self.bullets.update()

        # Get rid of bullets that have dissapeared.
        for bullet in self.bullets.copy():
            if bullet.rect.right >= self.screen.get_rect().width:
                self.bullets.remove(bullet)
                self.settings.misses += 1
                if self.settings.misses >= 3:
                    self.settings.game_active = False
                    pygame.mouse.set_visible(True)
                    self.hitcount = 0
                    self.settings.misses = 0
                    break
            bullet.draw_bullet()

        self._check_bullet_target_collisions()

    def _check_bullet_target_collisions(self):
        """Respond to bullet-target collisions."""
        # Check for any bullets that have hit target.
        #    If so, get rid of the bullet keep the target.
        for bullet in self.bullets.copy():
            if bullet.is_collided_with(self.target):
                bullet.kill()
                self.hitcount += 1
                print("Good Hit #" + str(self.hitcount))
                if self.hitcount % 3 == 0:
                    self.settings.increase_speed()

    def _update_target(self):
        """
		Check if the fleet is at an edge,
		 then Update the positions of all aliens in the fleet."""
        self._check_target_edges()
        self.target.update()

    def _check_target_edges(self):
        """Respond if target hits edge"""
        if self.target.check_edges():
            self._change_target_direction()

    def _change_target_direction(self):
        self.settings.target_direction *= -1

    def _update_screen(self):
        """Update images on the screen, and flip to new screen."""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.target.blitme()

        # Draw the play button if the game is inactive.
        if not self.settings.game_active:
            self.play_button.draw_button()

        pygame.display.flip()