添加星系显示
This commit is contained in:
1 parent
88104f6703
commit
03a7fcb5b3
2 files changed
+110
-1
No files matched your search
@@ -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)
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
|
import math
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from src.config.color import Color
|
from src.config.color import Color
|
||||||
from src.config.game_config import data_path, GameConfig
|
from src.config.game_config import data_path, GameConfig
|
||||||
|
from src.entity.galaxy import Galaxy
|
||||||
from src.entity.player import Player
|
from src.entity.player import Player
|
||||||
from src.scenes.base_scene import BaseScene
|
from src.scenes.base_scene import BaseScene
|
||||||
|
|
||||||
@@ -17,6 +19,10 @@ class SpaceScene(BaseScene):
|
|||||||
# 实例化玩家
|
# 实例化玩家
|
||||||
self.player = Player(screen)
|
self.player = Player(screen)
|
||||||
|
|
||||||
|
# 初始化星系行星
|
||||||
|
sun = Galaxy("sun", 7)
|
||||||
|
self.plants = sun.plants
|
||||||
|
|
||||||
# 设置字体
|
# 设置字体
|
||||||
font_file = os.path.join(data_path(), "Dymon-ShouXieTi.otf")
|
font_file = os.path.join(data_path(), "Dymon-ShouXieTi.otf")
|
||||||
self.font = pygame.font.Font(font_file, 32)
|
self.font = pygame.font.Font(font_file, 32)
|
||||||
@@ -29,6 +35,10 @@ class SpaceScene(BaseScene):
|
|||||||
# 玩家更新,接收键盘输入与每帧间隔
|
# 玩家更新,接收键盘输入与每帧间隔
|
||||||
self.player.update(pygame.key.get_pressed(), dt)
|
self.player.update(pygame.key.get_pressed(), dt)
|
||||||
|
|
||||||
|
# 行星位置更新
|
||||||
|
self._plants_update()
|
||||||
|
|
||||||
|
self.__show("加速:" + str(self.player.is_running))
|
||||||
self.__show("加速:" + str(self.player.is_running))
|
self.__show("加速:" + str(self.player.is_running))
|
||||||
|
|
||||||
def handle_event(self, event):
|
def handle_event(self, event):
|
||||||
@@ -39,7 +49,6 @@ class SpaceScene(BaseScene):
|
|||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
self.switch_to_scene("MainMenuScene")
|
self.switch_to_scene("MainMenuScene")
|
||||||
|
|
||||||
|
|
||||||
def __show(self, info, dest=(0, 0)):
|
def __show(self, info, dest=(0, 0)):
|
||||||
"""
|
"""
|
||||||
显示调试文本
|
显示调试文本
|
||||||
@@ -48,3 +57,23 @@ class SpaceScene(BaseScene):
|
|||||||
info = str(info)
|
info = str(info)
|
||||||
info_text = self.font.render(info, True, Color.white)
|
info_text = self.font.render(info, True, Color.white)
|
||||||
self.screen.blit(info_text, dest)
|
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)
|
||||||
Reference in new issue
Block a user