From 03a7fcb5b3d3c6a680743805cf2befac34d8ab1e Mon Sep 17 00:00:00 2001 From: chicrobt Date: Wed, 26 Aug 2026 01:41:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=98=9F=E7=B3=BB=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entity/galaxy.py | 80 +++++++++++++++++++++++++++++++++++++++ src/scenes/space_scene.py | 35 +++++++++++++++-- 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/entity/galaxy.py diff --git a/src/entity/galaxy.py b/src/entity/galaxy.py new file mode 100644 index 0000000..2ef584e --- /dev/null +++ b/src/entity/galaxy.py @@ -0,0 +1,80 @@ +# --------------------------------------------------------------------------- +# 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) diff --git a/src/scenes/space_scene.py b/src/scenes/space_scene.py index a1b25e2..c22b3aa 100644 --- a/src/scenes/space_scene.py +++ b/src/scenes/space_scene.py @@ -1,9 +1,11 @@ +import math import os import pygame from src.config.color import Color from src.config.game_config import data_path, GameConfig +from src.entity.galaxy import Galaxy from src.entity.player import Player from src.scenes.base_scene import BaseScene @@ -17,6 +19,10 @@ class SpaceScene(BaseScene): # 实例化玩家 self.player = Player(screen) + # 初始化星系行星 + sun = Galaxy("sun", 7) + self.plants = sun.plants + # 设置字体 font_file = os.path.join(data_path(), "Dymon-ShouXieTi.otf") self.font = pygame.font.Font(font_file, 32) @@ -29,7 +35,11 @@ class SpaceScene(BaseScene): # 玩家更新,接收键盘输入与每帧间隔 self.player.update(pygame.key.get_pressed(), dt) - self.__show("加速:"+str(self.player.is_running)) + # 行星位置更新 + self._plants_update() + + self.__show("加速:" + str(self.player.is_running)) + self.__show("加速:" + str(self.player.is_running)) def handle_event(self, event): """ @@ -39,7 +49,6 @@ class SpaceScene(BaseScene): if event.key == pygame.K_ESCAPE: self.switch_to_scene("MainMenuScene") - def __show(self, info, dest=(0, 0)): """ 显示调试文本 @@ -47,4 +56,24 @@ class SpaceScene(BaseScene): if not isinstance(info, str): info = str(info) info_text = self.font.render(info, True, Color.white) - self.screen.blit(info_text, dest) \ No newline at end of file + self.screen.blit(info_text, dest) + + def _plants_update(self): + for plant in self.plants: + dx = plant.x - GameConfig.window_x/2 + dy = plant.y - GameConfig.window_y/2 + r = math.sqrt(dx**2 + dy**2) + info = f"({round(plant.x,2)}, {round(plant.y,2)}), {round(r,2)}" + + font_file = os.path.join(data_path(), "Dymon-ShouXieTi.otf") + pos_font = pygame.font.Font(font_file, 16) + text = pos_font.render(info, True, Color.white) + self.screen.blit(text, (plant.x, plant.y)) + self._show_plant(plant) + self._show_track(plant) + + def _show_plant(self, plant): + pygame.draw.circle(self.screen, Color.white, (plant.x, plant.y), plant.size) + + def _show_track(self, plant): + pygame.draw.circle(self.screen, Color.white, (plant.center_x, plant.center_y), plant.r, 1)