```"""

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

"""

import math
import os

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BULLET_SPEED = 4

""" 主要应用程序类 """

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

# 设置工作目录 for python -m而设
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)

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()  # 玩家列表

# 实例化玩家操作的飞船
self.player = arcade.Sprite("images/playerShip1_orange.png", 0.5)
self.player_list.append(self.player)

# 实例化左上角敌人
enemy = arcade.Sprite("images/playerShip1_green.png", 0.5)
enemy.center_x = 120
enemy.center_y = SCREEN_HEIGHT - enemy.height
enemy.angle = 180
self.enemy_list.append(enemy)

# 实例化右上角敌人
enemy = arcade.Sprite("images/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):
"""渲染屏幕. """

self.enemy_list.draw()  # 重画所有敌人
self.bullet_list.draw() # 重画所有子弹
self.player_list.draw() # 重画所有玩家飞船

def update(self, delta_time):
"""移动所有角色与游戏逻辑代码. """

self.frame_count += 1  # 帧计数

# 遍历每个敌人
for enemy in self.enemy_list:

# 首先，计算到玩家的角度. 我们可以当发射时才计算，但是这里要让敌人
# 实时地面向玩家飞船，所以每帧都计算。

# 起点就是敌人的坐标
start_x = enemy.center_x
start_y = enemy.center_y

# 终点就是飞船的坐标
dest_x = self.player.center_x
dest_y = self.player.center_y

# 用反正切函数计算朝向角度.
x_diff = dest_x - start_x
y_diff = dest_y - start_y
angle = math.atan2(y_diff, x_diff)

# 设置角度
enemy.angle = math.degrees(angle)-90

# 每60帧发射一次，约1秒
if self.frame_count % 60 == 0:
bullet.center_x = start_x
bullet.center_y = start_y

# 子弹射击角度转换为度数表示
bullet.angle = math.degrees(angle)

# 设置速度向量
bullet.change_x = math.cos(angle) * BULLET_SPEED
bullet.change_y = math.sin(angle) * BULLET_SPEED

self.bullet_list.append(bullet)

# 子弹的顶y坐标小于0就杀死它
for bullet in self.bullet_list:
if bullet.top < 0:
bullet.kill()

self.bullet_list.update()

def on_mouse_motion(self, x, y, delta_x, delta_y):
"""鼠标指针控制玩家. """
self.player.center_x = x
self.player.center_y = y

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