2 Commits
Author SHA1 Message Date
chicrobot 03a7fcb5b3 添加星系显示 2026-08-26 01:41:04 +08:00
chicrobot 88104f6703 添加自动获取屏幕大小 2026-08-23 00:09:21 +08:00
4 changed files with 124 additions and 5 deletions

No files matched your search

+4 -2
View File
@@ -3,8 +3,10 @@ import os
class GameConfig: class GameConfig:
game_name = "Test Game" game_name = "Test Game"
window_x = 1920 # window_x = 1920
window_y = 1080 # window_y = 1080
window_x = 0
window_y = 0
bg_color = (0, 0, 0) bg_color = (0, 0, 0)
def data_path(): def data_path():
+80
View File
@@ -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)
+32 -3
View File
@@ -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,7 +35,11 @@ class SpaceScene(BaseScene):
# 玩家更新,接收键盘输入与每帧间隔 # 玩家更新,接收键盘输入与每帧间隔
self.player.update(pygame.key.get_pressed(), dt) 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): 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)):
""" """
显示调试文本 显示调试文本
@@ -47,4 +56,24 @@ class SpaceScene(BaseScene):
if not isinstance(info, str): if not isinstance(info, str):
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)
+8
View File
@@ -14,6 +14,13 @@ class Game:
# 显示输入法候选框 # 显示输入法候选框
os.environ["SDL_IME_SHOW_UI"] = "1" os.environ["SDL_IME_SHOW_UI"] = "1"
# 如果设置中屏幕为0,0则获取屏幕大小
display_info = pygame.display.Info()
if not GameConfig.window_x:
GameConfig.window_x = display_info.current_w
if not GameConfig.window_y:
GameConfig.window_y = display_info.current_h
# 设置窗口大小 # 设置窗口大小
size = (GameConfig.window_x, GameConfig.window_y) size = (GameConfig.window_x, GameConfig.window_y)
self.screen = pygame.display.set_mode(size, pygame.NOFRAME) self.screen = pygame.display.set_mode(size, pygame.NOFRAME)
@@ -61,5 +68,6 @@ class Game:
# 更新显示 # 更新显示
pygame.display.update() pygame.display.update()
if __name__ == "__main__": if __name__ == "__main__":
Game() Game()