-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_renderer.py
More file actions
55 lines (45 loc) · 2.12 KB
/
Copy pathshape_renderer.py
File metadata and controls
55 lines (45 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pygame
import math
class ShapeRenderer:
@staticmethod
def create_polygon_surface(points, color, width=0):
# Calculate the bounding box of the polygon
min_x = min([p[0] for p in points])
max_x = max([p[0] for p in points])
min_y = min([p[1] for p in points])
max_y = max([p[1] for p in points])
# Create a surface with the size of the bounding box
width_surf = max_x - min_x
height_surf = max_y - min_y
surface = pygame.Surface((width_surf, height_surf), pygame.SRCALPHA)
# Adjust points to the surface coordinate system
adjusted_points = [(p[0] - min_x, p[1] - min_y) for p in points]
# Draw the polygon
pygame.draw.polygon(surface, color, adjusted_points, width)
return surface
@staticmethod
def create_circle_surface(radius, color, width=0):
diameter = radius * 2
surface = pygame.Surface((diameter, diameter), pygame.SRCALPHA)
pygame.draw.circle(surface, color, (radius, radius), radius, width)
return surface
@staticmethod
def create_rectangle_surface(width, height, color):
surface = pygame.Surface((width, height), pygame.SRCALPHA)
surface.fill(color)
return surface
@staticmethod
def create_line_surface(start_pos, end_pos, color, width=1):
surface = pygame.Surface((max(start_pos[0], end_pos[0]) + width, max(start_pos[1], end_pos[1]) + width), pygame.SRCALPHA)
pygame.draw.line(surface, color, start_pos, end_pos, width)
return surface
@staticmethod
def create_ellipse_surface(rect, color):
surface = pygame.Surface((rect.width, rect.height), pygame.SRCALPHA)
pygame.draw.ellipse(surface, color, (0, 0, rect.width, rect.height))
return surface
@staticmethod
def create_arc_surface(rect, color, start_angle, stop_angle, width=0):
surface = pygame.Surface((rect.width, rect.height), pygame.SRCALPHA)
pygame.draw.arc(surface, color, (0, 0, rect.width, rect.height), math.radians(start_angle), math.radians(stop_angle), width)
return surface