You've already forked AiraPulsar
							
							
		
			
				
	
	
		
			115 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # 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()
 |