Files
Project1/src/entity/galaxy.py
T
2026-08-26 01:41:04 +08:00

81 lines
2.6 KiB
Python

# ---------------------------------------------------------------------------
# ProjectName: Project1
# FileName: galaxy.py
# Author: chic_robot
# Datetime: 2026/8/25 23:45
# Description:
# 命名规范:文件名全小写+下划线,类名大驼峰,方法和变量小写+下划线连接,
# 常量大写,变量和常量用名词,方法用动词
# ---------------------------------------------------------------------------
import math
import random
from src.config.game_config import GameConfig
class Plant:
"""
星球
"""
def __init__(self, x=None, y=None, center_x=None, center_y=None, angle=0.0, r=1, v=1, size=5):
self.x = x # 星球坐标
self.y = y
self.center_x = center_x # 星球环绕中心
self.center_y = center_y
self.angle = angle # 星球绕行所处角度
self.r = r # 星球绕行半径
self.v = v # 绕行速度
self.size = size # 星球半径
# 如果没有输入中心坐标则默认为屏幕中心,xy分别判断
if not self.center_x:
self.center_x = GameConfig.window_x/2
if not self.center_y:
self.center_y = GameConfig.window_y/2
# 如果没有传入星球坐标则根据绕行角度更新
if not self.x or not self.y:
self._update_position_by_angle()
def _update_position_by_angle(self):
"""
根据角度与绕行圆心计算坐标
"""
self.x = self.center_x + self.r * math.cos(self.angle)
self.y = self.center_y + self.r * math.sin(self.angle)
class Galaxy:
"""
星系
"""
def __init__(self, name, plants_num=0, x=None, y=None):
self.name = name # 星系名称
self.plants = [] # 星系下行星
self.x = x # 星系中心坐标
self.y = y
# 如果没有传入中心坐标则默认为屏幕中心
if not x:
self.x = GameConfig.window_x/2
if not y:
self.y = GameConfig.window_y/2
self.init_plants(plants_num)
def init_plants(self, plants_num, center_x=None, center_y=None):
"""
初始化生成行星
"""
for i in range(plants_num):
r = (i+1) * 50
angle = random.uniform(0, 2 * math.pi)
plant = Plant(center_x=center_x, center_y=center_y, r=r, angle=angle)
self.plants.append(plant)
# def init_plants(self, plants_num):
# for i in range(plants_num):
# x = random.randint(0, GameConfig.window_x)
# y = random.randint(0, GameConfig.window_y)
# plant = Plant(x, y)
# self.plants.append(plant)