The primary objective of this study is to investigate maritime collision avoidance and trajectory planning in the presence of dynamic and static obstacles during navigation. Adhering to safety regulations is crucial when executing ship collision avoidance tasks. To address this issue, we propose an optimized APF-ACO algorithm for collision avoidance and path planning. First, a ship collision avoidance constraint model is constructed based on COLREGs to enhance the safety and applicability of the algorithm. Then, by introducing factors such as velocity, position, and shape parameters, the traditional APF method is optimized, creating a dynamic APF gradient for collision avoidance decision making in the face of dynamic obstacles. Furthermore, the optimized APF method is integrated with the ant colony optimization algorithm, the latter modified to overcome the inherent local optimality issues in the APF method. Ultimately, validations are conducted in three areas: static avoidance and planning in restricted sea areas, avoidance under conditions of mixed static and dynamic obstacles, and avoidance in situations of multiple ship encounters. These serve to illustrate the feasibility and efficacy of the proposed algorithm in achieving dynamic ship collision avoidance while simultaneously completing path-planning tasks.