"""
Show how to have enemies shoot bullets aimed at the player.

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.sprite_bullets_enemy_aims
"""

import arcade
import math
import os

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Sprites and Bullets Enemy Aims Example"
BULLET_SPEED = 4


class MyGame(arcade.Window):
    """ Main application class """

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        arcade.set_background_color(arcade.color.BLACK)

        self.frame_count = 0

        self.enemy_list = None
        self.bullet_list = None
        self.player_list = None
        self.player = None

    def setup(self):
        self.enemy_list = arcade.SpriteList()
        self.bullet_list = arcade.SpriteList()
        self.player_list = arcade.SpriteList()

        # Add player ship
        self.player = arcade.Sprite(":resources:images/space_shooter/playerShip1_orange.png", 0.5)
        self.player_list.append(self.player)

        # Add top-left enemy ship
        enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", 0.5)
        enemy.center_x = 120
        enemy.center_y = SCREEN_HEIGHT - enemy.height
        enemy.angle = 180
        self.enemy_list.append(enemy)

        # Add top-right enemy ship
        enemy = arcade.Sprite(":resources:images/space_shooter/playerShip1_green.png", 0.5)
        enemy.center_x = SCREEN_WIDTH - 120
        enemy.center_y = SCREEN_HEIGHT - enemy.height
        enemy.angle = 180
        self.enemy_list.append(enemy)

    def on_draw(self):
        """Render the screen. """

        arcade.start_render()

        self.enemy_list.draw()
        self.bullet_list.draw()
        self.player_list.draw()

    def on_update(self, delta_time):
        """All the logic to move, and the game logic goes here. """

        self.frame_count += 1

        # Loop through each enemy that we have
        for enemy in self.enemy_list:

            # First, calculate the angle to the player. We could do this
            # only when the bullet fires, but in this case we will rotate
            # the enemy to face the player each frame, so we'll do this
            # each frame.

            # Position the start at the enemy's current location
            start_x = enemy.center_x
            start_y = enemy.center_y

            # Get the destination location for the bullet
            dest_x = self.player.center_x
            dest_y = self.player.center_y

            # Do math to calculate how to get the bullet to the destination.
            # Calculation the angle in radians between the start points
            # and end points. This is the angle the bullet will travel.
            x_diff = dest_x - start_x
            y_diff = dest_y - start_y
            angle = math.atan2(y_diff, x_diff)

            # Set the enemy to face the player.
            enemy.angle = math.degrees(angle)-90

            # Shoot every 60 frames change of shooting each frame
            if self.frame_count % 60 == 0:
                bullet = arcade.Sprite(":resources:images/space_shooter/laserBlue01.png")
                bullet.center_x = start_x
                bullet.center_y = start_y

                # Angle the bullet sprite
                bullet.angle = math.degrees(angle)

                # Taking into account the angle, calculate our change_x
                # and change_y. Velocity is how fast the bullet travels.
                bullet.change_x = math.cos(angle) * BULLET_SPEED
                bullet.change_y = math.sin(angle) * BULLET_SPEED

                self.bullet_list.append(bullet)

        # Get rid of the bullet when it flies off-screen
        for bullet in self.bullet_list:
            if bullet.top < 0:
                bullet.remove_from_sprite_lists()

        self.bullet_list.update()

    def on_mouse_motion(self, x, y, delta_x, delta_y):
        """Called whenever the mouse moves. """
        self.player.center_x = x
        self.player.center_y = y


def main():
    """ Main method """
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

[((136, 4, 136, 16), 'arcade.run', 'arcade.run', ({}, {}), '()', False, 'import arcade\n'), ((29, 8, 29, 27), 'os.chdir', 'os.chdir', ({(29, 17, 29, 26): 'file_path'}, {}), '(file_path)', False, 'import os\n'), ((31, 8, 31, 55), 'arcade.set_background_color', 'arcade.set_background_color', ({(31, 36, 31, 54): 'arcade.color.BLACK'}, {}), '(arcade.color.BLACK)', False, 'import arcade\n'), ((41, 26, 41, 45), 'arcade.SpriteList', 'arcade.SpriteList', ({}, {}), '()', False, 'import arcade\n'), ((42, 27, 42, 46), 'arcade.SpriteList', 'arcade.SpriteList', ({}, {}), '()', False, 'import arcade\n'), ((43, 27, 43, 46), 'arcade.SpriteList', 'arcade.SpriteList', ({}, {}), '()', False, 'import arcade\n'), ((46, 22, 46, 98), 'arcade.Sprite', 'arcade.Sprite', ({(46, 36, 46, 92): '""":resources:images/space_shooter/playerShip1_orange.png"""', (46, 94, 46, 97): '0.5'}, {}), "(':resources:images/space_shooter/playerShip1_orange.png', 0.5)", False, 'import arcade\n'), ((50, 16, 50, 91), 'arcade.Sprite', 'arcade.Sprite', ({(50, 30, 50, 85): '""":resources:images/space_shooter/playerShip1_green.png"""', (50, 87, 50, 90): '0.5'}, {}), "(':resources:images/space_shooter/playerShip1_green.png', 0.5)", False, 'import arcade\n'), ((57, 16, 57, 91), 'arcade.Sprite', 'arcade.Sprite', ({(57, 30, 57, 85): '""":resources:images/space_shooter/playerShip1_green.png"""', (57, 87, 57, 90): '0.5'}, {}), "(':resources:images/space_shooter/playerShip1_green.png', 0.5)", False, 'import arcade\n'), ((66, 8, 66, 29), 'arcade.start_render', 'arcade.start_render', ({}, {}), '()', False, 'import arcade\n'), ((28, 36, 28, 61), 'os.path.abspath', 'os.path.abspath', ({(28, 52, 28, 60): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((98, 20, 98, 46), 'math.atan2', 'math.atan2', ({(98, 31, 98, 37): 'y_diff', (98, 39, 98, 45): 'x_diff'}, {}), '(y_diff, x_diff)', False, 'import math\n'), ((101, 26, 101, 45), 'math.degrees', 'math.degrees', ({(101, 39, 101, 44): 'angle'}, {}), '(angle)', False, 'import math\n'), ((105, 25, 105, 89), 'arcade.Sprite', 'arcade.Sprite', ({(105, 39, 105, 88): '""":resources:images/space_shooter/laserBlue01.png"""'}, {}), "(':resources:images/space_shooter/laserBlue01.png')", False, 'import arcade\n'), ((110, 31, 110, 50), 'math.degrees', 'math.degrees', ({(110, 44, 110, 49): 'angle'}, {}), '(angle)', False, 'import math\n'), ((114, 34, 114, 49), 'math.cos', 'math.cos', ({(114, 43, 114, 48): 'angle'}, {}), '(angle)', False, 'import math\n'), ((115, 34, 115, 49), 'math.sin', 'math.sin', ({(115, 43, 115, 48): 'angle'}, {}), '(angle)', False, 'import math\n')]