This commit is contained in:
2025-03-10 18:31:39 +08:00
parent afd1a37c0c
commit 5519a8595b
10 changed files with 486 additions and 102 deletions

1
testfield/vgl/README.md Normal file
View File

@@ -0,0 +1 @@
# VGL - 矢量图形渲染库

113
testfield/vgl/aux.py Normal file
View File

@@ -0,0 +1,113 @@
def generate_shape_dict(shape, **kwargs):
if shape == 'rect':
return {
'method': 'rect',
'pos': kwargs.get('pos', (0, 0)),
'size': kwargs.get('size', (1, 1)),
'color': kwargs.get('color', (255, 255, 255)),
'width': kwargs.get('width', 0) # 添加边框宽度
}
elif shape == 'line':
return {
'method': 'line',
'start_pos': kwargs.get('start_pos', (0, 0)),
'end_pos': kwargs.get('end_pos', (1, 1)),
'color': kwargs.get('color', (255, 255, 255))
}
elif shape == 'circle':
return {
'method': 'circle',
'center': kwargs.get('center', (0, 0)),
'radius': kwargs.get('radius', 1),
'color': kwargs.get('color', (255, 255, 255))
}
elif shape == 'ellipse':
return {
'method': 'ellipse',
'pos': kwargs.get('pos', (0, 0)),
'size': kwargs.get('size', (1, 1)),
'color': kwargs.get('color', (255, 255, 255))
}
elif shape == 'polygon':
return {
'method': 'polygon',
'pointlist': kwargs.get('pointlist', []),
'color': kwargs.get('color', (255, 255, 255))
}
elif shape == 'arc':
return {
'method': 'arc',
'pos': kwargs.get('pos', (0, 0)),
'size': kwargs.get('size', (1, 1)),
'color': kwargs.get('color', (255, 255, 255)),
'start_angle': kwargs.get('start_angle', 0),
'stop_angle': kwargs.get('stop_angle', 3.14)
}
elif shape == 'point':
return {
'method': 'point',
'pos': kwargs.get('pos', (0, 0)),
'color': kwargs.get('color', (255, 255, 255))
}
elif shape == 'lines':
return {
'method': 'lines',
'pointlist': kwargs.get('pointlist', []),
'color': kwargs.get('color', (255, 255, 255))
}
else:
raise ValueError("Unsupported shape type")
def main():
print("欢迎使用图形参数生成器!")
shape = input("请输入图形类型 (rect, line, circle, ellipse, polygon, arc, point, lines): ").strip().lower()
params = {}
if shape == 'rect':
params['pos'] = tuple(map(int, input("请输入矩形位置 (x y): ").split()))
params['size'] = tuple(map(int, input("请输入矩形大小 (width height): ").split()))
params['color'] = tuple(map(int, input("请输入矩形颜色 (R G B): ").split()))
params['width'] = int(input("请输入矩形边框宽度: ")) # 添加边框宽度输入
elif shape == 'line':
params['start_pos'] = tuple(map(int, input("请输入起始位置 (x y): ").split()))
params['end_pos'] = tuple(map(int, input("请输入结束位置 (x y): ").split()))
params['color'] = tuple(map(int, input("请输入线条颜色 (R G B): ").split()))
elif shape == 'circle':
params['center'] = tuple(map(int, input("请输入圆心位置 (x y): ").split()))
params['radius'] = int(input("请输入圆的半径: "))
params['color'] = tuple(map(int, input("请输入圆的颜色 (R G B): ").split()))
elif shape == 'ellipse':
params['pos'] = tuple(map(int, input("请输入椭圆位置 (x y): ").split()))
params['size'] = tuple(map(int, input("请输入椭圆大小 (width height): ").split()))
params['color'] = tuple(map(int, input("请输入椭圆颜色 (R G B): ").split()))
elif shape == 'polygon':
points = input("请输入多边形的点 (x1 y1 x2 y2 ...): ").split()
params['pointlist'] = [(int(points[i]), int(points[i + 1])) for i in range(0, len(points), 2)]
params['color'] = tuple(map(int, input("请输入多边形颜色 (R G B): ").split()))
elif shape == 'arc':
params['pos'] = tuple(map(int, input("请输入弧的位置 (x y): ").split()))
params['size'] = tuple(map(int, input("请输入弧的大小 (width height): ").split()))
params['color'] = tuple(map(int, input("请输入弧的颜色 (R G B): ").split()))
params['start_angle'] = float(input("请输入弧的起始角度 (弧度制): "))
params['stop_angle'] = float(input("请输入弧的结束角度 (弧度制): "))
elif shape == 'point':
params['pos'] = tuple(map(int, input("请输入点的位置 (x y): ").split()))
params['color'] = tuple(map(int, input("请输入点的颜色 (R G B): ").split()))
elif shape == 'lines':
points = input("请输入线段的点 (x1 y1 x2 y2 ...): ").split()
params['pointlist'] = [(int(points[i]), int(points[i + 1])) for i in range(0, len(points), 2)]
params['color'] = tuple(map(int, input("请输入线段颜色 (R G B): ").split()))
else:
print("不支持的图形类型")
return
# 生成图形字典
shape_dict = generate_shape_dict(shape, **params)
# 输出结果
print("生成的图形参数字典:")
print(shape_dict)
if __name__ == "__main__":
main()

26
testfield/vgl/test.py Normal file
View File

@@ -0,0 +1,26 @@
import pygame
import sys
from testfield.vgl.vgllib import *
pygame.init()
window_size = (800, 600)
window = pygame.display.set_mode(window_size)
frame = Frame("Main Frame", window_size)
# 注册一个矩形组件
frame.register("rectangle", {"method": "rect", "pos": (100, 100), "size": (200, 150), "color": (255, 0, 0)})
# 注册一个圆形组件
frame.register("circle", {"method": "circle", "center": (400, 300), "radius": 50, "color": (0, 255, 0)})
# 主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
window.fill((0, 0, 0)) # 清空窗口
frame.drawall(frame.components) # 绘制所有组件
frame.show(window, (0, 0)) # 显示框架
pygame.display.flip() # 更新显示

114
testfield/vgl/vgllib.py Normal file
View File

@@ -0,0 +1,114 @@
# Vector Graphics Library
# vgllib.py
import pygame
import uuid
class Graph:
@staticmethod
def rect(frame, pos: tuple = (0, 0), size: tuple = (1, 1), color: tuple = (255, 255, 255), width: int = 0):
pygame.draw.rect(frame.surface, color=color, rect=(pos[0], pos[1], size[0], size[1]), width=width)
@staticmethod
def line(frame, start_pos: tuple, end_pos: tuple, color: tuple = (255, 255, 255)):
pygame.draw.aaline(frame.surface, color, start_pos, end_pos)
@staticmethod
def circle(frame, center: tuple, radius: int, color: tuple = (255, 255, 255), width: int = 0):
pygame.draw.circle(frame.surface, color, center, radius, width)
@staticmethod
def ellipse(frame, pos: tuple = (0, 0), size: tuple = (1, 1), color: tuple = (255, 255, 255), width: int = 0):
rect=(pos[0], pos[1], size[0], size[1])
pygame.draw.ellipse(frame.surface, color, rect, width)
@staticmethod
def polygon(frame, pointlist: list, color: tuple = (255, 255, 255), width: int = 0):
pygame.draw.polygon(frame.surface, color, pointlist, width)
@staticmethod
def arc(frame, pos: tuple = (0, 0), size: tuple = (1, 1), color: tuple = (255, 255, 255), start_angle: float = 0, stop_angle: float = 3.14, width: int = 1):
rect=(pos[0], pos[1], size[0], size[1])
pygame.draw.arc(frame.surface, color, rect, start_angle, stop_angle, width)
@staticmethod
def point(frame, pos: tuple, color: tuple = (255, 255, 255)):
pygame.draw.point(frame.surface, color, pos)
@staticmethod
def lines(frame, pointlist: list, color: tuple = (255, 255, 255)):
pygame.draw.aalines(frame.surface, color, closed=False, points=pointlist)
@staticmethod
def call(frame, method, **kwargs):
if hasattr(Graph, method):
getattr(Graph, method)(frame, **kwargs)
else:
print(f"方法 {method} 不存在")
class Frame(object):
components = dict()
components_stat = dict()
def __init__(self, name: str, size: tuple):
self.name = name
self.size = size
self.surface = pygame.Surface(size, flags=pygame.HWSURFACE)
self.is_hide = False
print("初始化子模块")
def show(self, window, position: tuple):
if not self.is_hide:
window.blit(self.surface, position)
def set_visible(self, newstat=True):
self.is_hide = newstat
def set_position(self, newposition):
self.position = newposition
def register(self, subname="", attr=None):
if subname == "":
subname = uuid.uuid4()
self.components[subname] = attr
self.components_stat[subname] = 1 # by default, not hiding
def draw(self, attr):
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
Graph.call(self, **attr)
def set_component_visible(self, subname, newstat):
self.components_stat[subname] = newstat
def drawall(self, attr):
for i in self.components.keys():
if self.components_stat[i]:
self.draw(self.components[i])
def clear(self, color=(0,0,0)):
self.surface.fill(color)
def loads(self, ):
# 示例
if __name__ == "__main__":
pygame.init()
window = pygame.display.set_mode((800, 600))
frame = Frame("Test", (800, 600))
frame.draw({'method':'rect', 'pos':(50, 50), 'size':(200, 100), 'color':(255, 0, 0)}) # 绘制红色矩形
# 主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
window.fill((0, 0, 0)) # 清空窗口
frame.show(window, (0, 0)) # 显示帧
pygame.display.flip() # 更新显示
pygame.quit()